Created
August 17, 2011 20:37
-
-
Save bevenky/1152546 to your computer and use it in GitHub Desktop.
Redis Cache
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 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