Source

heechee / heechee / webdav / caching / rediscache.py

Full commit
import logging
import redis

try:
    from hashlib import md5
except ImportError:
    from md5 import md5

class RedisCache(object):
    """
    Simple Redis cache.
    """
    def __init__(self, server, db=0):
        self._cache = redis.Redis(host=server, db=db)
    
    def __getitem__(self, key):
        value = self._cache.get(key)
        if value is not None:
            logging.debug("cache HIT: %r" % key)
            return value
        else:
            logging.debug("cache MISS: %r" % key)
            raise KeyError("No such key %r" % key)
    
    def __setitem__(self, key, value):
        logging.debug("cache SET: %r (%i)" % (key, len(value)))
        self._cache.set(key, value)