Initial Redislite implementation with limits

This commit is contained in:
Jeremy Zhang 2017-04-21 18:06:19 -07:00
parent acbb7ff3a9
commit 71f4785d13
4 changed files with 29 additions and 0 deletions

2
.gitignore vendored
View File

@ -90,3 +90,5 @@ ENV/
# Project specifc # Project specifc
config.py config.py
redislite.db
redislite.db.settings

View File

@ -15,6 +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.secret_key = config['app-secret'] app.secret_key = config['app-secret']
db.init_app(app) db.init_app(app)

View File

@ -5,3 +5,4 @@ from guilds import Guilds
from unauthenticated_users import UnauthenticatedUsers from unauthenticated_users import UnauthenticatedUsers
from unauthenticated_bans import UnauthenticatedBans from unauthenticated_bans import UnauthenticatedBans
from authenticated_users import AuthenticatedUsers from authenticated_users import AuthenticatedUsers
from custom_redislite import LimitsRedisLite

View File

@ -0,0 +1,25 @@
import urlparse
from limits.storage import Storage
from redislite import Redis
class LimitsRedisLite(Storage): # For Python Limits
STORAGE_SCHEME = "redislite"
def __init__(self, uri, **options):
self.redis_instance = Redis(urlparse.urlparse(uri).netloc)
def check(self):
return True
def get_expiry(self, key):
return self.redis_instance.ttl(key)
def incr(self, key, expiry, elastic_expiry=False):
if self.redis_instance.exists(key):
self.redis_instance.set(key, int(self.redis_instance.get(key))+1)
else:
self.redis_instance.set(key, 1)
self.redis_instance.expire(key, expiry)
return
def get(self, key):
return int(self.redis_instance.get(key))