1
0
Fork 0
forked from github/pelican

Add a caching mechnism

This commit is contained in:
Alexis Métaireau 2013-09-25 23:35:36 +02:00
commit 2b87eb7af6
4 changed files with 66 additions and 5 deletions

37
pelican/cache.py Normal file
View file

@ -0,0 +1,37 @@
import os.path
import hashlib
import pickle
import logging
logger = logging.getLogger(__name__)
class CachedReader(object):
def __init__(self, reader, cache_path):
self._reader = reader
self._cache_path = cache_path
def process_metadata(self, *args, **kwargs):
return self._reader.process_metadata(*args, **kwargs)
def read(self, path):
mtime = os.stat(path).st_mtime
m = hashlib.md5()
# We want to hash path + mtime
m.update(path)
m.update(str(mtime))
hash_ = m.hexdigest()
cache_file = os.path.join(self._cache_path, hash_)
if os.path.exists(cache_file):
logger.debug('reading {0} from cache'.format(path))
with open(cache_file) as f:
content, metadata = pickle.load(f)
else:
content, metadata = self._reader.read(path)
with open(cache_file, 'w+') as f:
pickle.dump((content, metadata), f)
logger.debug('stored {0} in the cache'.format(path))
return content, metadata