Reimplement redislite to limiter, hopefully everything works this time

This commit is contained in:
Jeremy Zhang 2017-04-22 06:35:26 -07:00
parent c497760f43
commit 4899c6d2d9
2 changed files with 8 additions and 7 deletions

View File

@ -15,7 +15,7 @@ app.config['SQLALCHEMY_DATABASE_URI'] = config['database-uri']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Suppress the warning/no need this on for now. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Suppress the warning/no need this on for now.
app.config['RATELIMIT_HEADERS_ENABLED'] = True app.config['RATELIMIT_HEADERS_ENABLED'] = True
app.config['SQLALCHEMY_POOL_RECYCLE'] = 250 app.config['SQLALCHEMY_POOL_RECYCLE'] = 250
#app.config['RATELIMIT_STORAGE_URL'] = 'redislite://redislite.db' app.config['RATELIMIT_STORAGE_URL'] = 'redislite://redislite.db'
app.secret_key = config['app-secret'] app.secret_key = config['app-secret']
db.init_app(app) db.init_app(app)

View File

@ -16,13 +16,14 @@ class LimitsRedisLite(Storage): # For Python Limits
def incr(self, key, expiry, elastic_expiry=False): def incr(self, key, expiry, elastic_expiry=False):
if not self.redis_instance.exists(key): if not self.redis_instance.exists(key):
self.redis_instance.set(key, 1) self.redis_instance.set(key, 1, ex=expiry)
self.redis_instance.expireat(key, int(time.time() + expiry))
else: else:
oldexp = self.get_expiry(key) oldexp = oldexp = self.get_expiry(key) - time.time()
self.redis_instance.set(key, int(self.redis_instance.get(key))+1) if oldexp <= 0:
self.redis_instance.expireat(key, int(time.time() + oldexp)) self.redis_instance.delete(key)
return return self.incr(key, expiry, elastic_expiry)
self.redis_instance.set(key, int(self.redis_instance.get(key))+1, ex=int(round(oldexp)))
return int(self.get(key))
def get(self, key): def get(self, key):
return int(self.redis_instance.get(key)) return int(self.redis_instance.get(key))