From c954d59cf180824223f846550c097adce75355eb Mon Sep 17 00:00:00 2001 From: Andrei Date: Thu, 13 Oct 2016 00:06:16 -0500 Subject: [PATCH] Add redis provider --- disco/api/http.py | 1 + disco/bot/providers/redis.py | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 disco/bot/providers/redis.py diff --git a/disco/api/http.py b/disco/api/http.py index 88dbc50..675b0be 100644 --- a/disco/api/http.py +++ b/disco/api/http.py @@ -127,6 +127,7 @@ class HTTPClient(LoggingClass): self.limiter = RateLimiter() self.headers = { 'Authorization': 'Bot ' + token, + 'X-Disable-Ratelimits': '1', } def __call__(self, route, args=None, **kwargs): diff --git a/disco/bot/providers/redis.py b/disco/bot/providers/redis.py new file mode 100644 index 0000000..d0c2e5b --- /dev/null +++ b/disco/bot/providers/redis.py @@ -0,0 +1,41 @@ +from __future__ import absolute_import + +import redis + +from itertools import izip + +from disco.util.serializer import Serializer +from .base import BaseProvider, SEP_SENTINEL + + +class RedisProvider(BaseProvider): + def __init__(self, config): + self.config = config + + def load(self): + self.redis = redis.Redis( + host=self.config.get('host', 'localhost'), + port=self.config.get('port', 6379), + db=self.config.get('db', 0)) + + def exists(self, key): + return self.db.exists(key) + + def keys(self, other): + count = other.count(SEP_SENTINEL) + 1 + for key in self.db.scan_iter(u'{}*'.format(other)): + if key.count(SEP_SENTINEL) == count: + yield key + + def get_many(self, keys): + for key, value in izip(keys, self.db.mget(keys)): + yield (key, Serializer.loads(self.format, value)) + + def get(self, key): + return Serializer.loads(self.format, self.db.get(key)) + + def set(self, key, value): + self.db.set(key, Serializer.dumps(self.format, value)) + + def delete(self, key, value): + self.db.delete(key)