Created
April 29, 2014 21:05
-
-
Save tylertreat/40c9d17a68dd915a7369 to your computer and use it in GitHub Desktop.
Python Redis Cache
This file contains 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 cPickle | |
import logging | |
import redis | |
TIMEOUT = 60 * 60 | |
_redis = None | |
def _get_redis(): | |
"""Return a StrictRedis instance. Initialize one first if necessary.""" | |
global _redis | |
if not _redis: | |
_redis = redis.StrictRedis() | |
return _redis | |
def set(key, value, timeout=TIMEOUT): | |
"""Cache the given key-value pair. | |
Args: | |
key: the cache key. | |
value: the value to cache. | |
timeout: the expiration timeout for the cache entry in seconds, | |
defaults to 1 minute. | |
""" | |
value = cPickle.dumps(value) | |
_get_redis().pipeline().set(key, value).expire(key, timeout).execute() | |
def get(key): | |
"""Retrieve the value associated with the given key from the cache. | |
Args: | |
the cache key to fetch. | |
Returns: | |
the cached value or None if it doesn't exist. | |
""" | |
value = _get_redis().get(key) | |
value = (value and cPickle.loads(value)) or None | |
if value is not None: | |
logging.debug('Cache hit for %s' % key) | |
return value | |
def flush(pattern='', batch=1000): | |
"""Flush the cache by evicting all entries or only entries whose keys | |
match a given pattern. | |
Args: | |
pattern: evict only the keys that match this pattern. | |
batch: delete the keys in groups of this size. | |
""" | |
red = _get_redis() | |
keys = red.keys('%s*' % pattern) | |
[red.delete(*keys[i:i + batch]) for i in xrange(0, len(keys), batch)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment