diff --git a/standings/cache.py b/standings/cache.py index 4e46c0f..4a707d1 100644 --- a/standings/cache.py +++ b/standings/cache.py @@ -1,6 +1,8 @@ from hashlib import sha1 import logging import sqlite3 +import redis +import zlib SQLITE_PATH = '/tmp/eveapicache.sqlite3' @@ -81,3 +83,38 @@ class DbCacheHandler: self.disconnect() except sqlite3.Error as e: self.log.error("Error purging document cache: %s", e.args[0]) + + +class EVEAPIRedisCache(object): + + def __init__(self, redis): + self.redis = redis + + @staticmethod + def gen_key(host, path, params): + params = ''.join(['{}={}'.format(x, y) for x, y in params.items()]) + key_hash = ''.join((host, path, params)) + return 'eveapi_cache_{}'.format(sha1(key_hash).hexdigest()) + + def retrieve(self, host, path, params): + key = self.gen_key(host, path, params) + try: + val = self.redis.get(key) + except redis.RedisError: + logging.exception('Error retrieving an EVE API call to Redis') + val = None + pass + if val: + return zlib.decompress(val) + + def store(self, host, path, params, doc, obj): + key = self.gen_key(host, path, params) + cache_time = obj.cachedUntil - obj.currentTime + if cache_time > 0: + val = zlib.compress(doc, 9) + try: + self.redis.set(key, val) + self.redis.expire(key, cache_time) + except redis.RedisError: + logging.exception('Error storing an EVE API call to Redis') + pass