Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created April 8, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whalesalad/35f66f52fea3df4813c42447d4c27438 to your computer and use it in GitHub Desktop.
Save whalesalad/35f66f52fea3df4813c42447d4c27438 to your computer and use it in GitHub Desktop.
work in progress!
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