Created
April 8, 2019 02:22
-
-
Save whalesalad/35f66f52fea3df4813c42447d4c27438 to your computer and use it in GitHub Desktop.
work in progress!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import supycache | |
from supycache.backends.base import BaseCache | |
from service.persistence import Redis | |
class SupyCacheRedisBackend(BaseCache): | |
def __init__(self, conn, prefix=None, config=None): | |
self.conn = conn | |
self.location = 'supycache' | |
self.config = config or {} | |
@classmethod | |
def encode(cls, data): | |
if not data: | |
return None | |
return pickle.dumps(data) | |
@classmethod | |
def decode(cls, raw): | |
if not raw: | |
return None | |
return pickle.loads(raw) | |
@property | |
def h(self): | |
"""Returns the hash key for storing cached information.""" | |
return self.location | |
def get(self, key): | |
raw = self.conn.hget(self.h, key) | |
return SupyCacheRedisBackend.decode(raw) | |
def set(self, key, value): | |
encoded = SupyCacheRedisBackend.encode(value) | |
return self.conn.hset(self.h, key, encoded) | |
def delete(self, key): | |
return self.conn.hdel(self.h, key) | |
def clear(self, key): | |
return self.conn.delete(self.h) | |
def get_supycache_backend(): | |
return SupyCacheRedisBackend(conn=Redis.get_connection()) | |
supycache.set_default_backend(get_supycache_backend()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment