Skip to content

Instantly share code, notes, and snippets.

@bevenky
Created August 17, 2011 20:37
Show Gist options
  • Save bevenky/1152546 to your computer and use it in GitHub Desktop.
Save bevenky/1152546 to your computer and use it in GitHub Desktop.
Redis Cache
import redis
class ResourceCache(object):
"""Uses a local directory as a store for cached files.
"""
def __init__(self, redis_host='localhost', redis_port=6379, redis_db=0):
self.cache = redis.Redis(host=redis_host, port=redis_port, db=redis_db)
def get_resource_params(self, resource_key):
if self.cache.sismember("resource_key", resource_key):
resource_type = self.cache.hget("resource_key:%s" % resource_key, "resource_type")
etag = self.cache.hget("resource_key:%s" % resource_key, "etag")
last_modified = self.cache.hget("resource_key:%s" % resource_key, "last_modified")
return resource_key, resource_type, etag, last_modified
else:
return None, None, None, None
def update_resource_params(self, resource_key, resource_type, etag, last_modified):
if etag is None:
etag = ""
if last_modified is None:
last_modified = ""
self.cache.sadd("resource_key", resource_key)
self.cache.hset("resource_key:%s" % resource_key, "resource_type", resource_type)
self.cache.hset("resource_key:%s" % resource_key, "etag", etag)
self.cache.hset("resource_key:%s" % resource_key, "last_modified", last_modified)
def delete_resource(self, resource_key):
if self.cache.sismembers("resource_key", resource_key):
self.cache.srem("resource_key", resource_key)
self.cache.delete("resource_key:%s" % resource_key)
if __name__ == "__main__":
r = ResourceCache()
#~r.update_resource_params('EEW34BT-Eb-twoXwfl_S9A==', 'mp3',
#~'flask-1306167922.0-20375-2401376856',
#~'Mon, 23 May 2011 16:25:22 GMT')
r.update_resource_params('EEW34BT-Eb-twoXwfl_S9A==', 'mp3',
None,
None)
print r.get_resource_params('EEW34BT-Eb-twoXwfl_S9A==')
print r.get_resource_params('EEW34BT-Eb-twoXwfl_XS9A==')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment