Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created August 23, 2012 23:36
Show Gist options
  • Save revolunet/3443562 to your computer and use it in GitHub Desktop.
Save revolunet/3443562 to your computer and use it in GitHub Desktop.
REDIS file caching
import hashlib
from redis import Redis
#
# automatically cache file access to a redis instance
# check if file changed via md5
#
class RedisFileManager(object):
""" cache file access to a redis instance """
def __init__(self, *args, **kwargs):
self.redis = Redis(*args, **kwargs)
def get(self, path):
file_key = self._key(path)
contents = self.redis.get(file_key)
if contents is None:
# cache new file in redis
contents = open(path, 'rb').read()
self.redis.set(file_key, contents)
return contents
def delete(self, path):
self.redis.delete(self._key(path))
def _key(self, path):
return 'file:%s' % self._md5sum(path)
def _md5sum(self, path):
""" get file md5 quickly """
md5 = hashlib.md5()
with open(path,'rb') as f:
for chunk in iter(lambda: f.read(128*md5.block_size), b''):
md5.update(chunk)
return md5.hexdigest()
srv = RedisFileManager(host='redishost.com', port=6379, password='myPassWord')
data = srv.get('/path/to/local/file')
print "GOT CONTENT", len(data)
@serpentblade
Copy link

If you have to get the md5 checksum of the file to generate its lookup key, wouldn't it be faster to just read the file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment