Skip to content

Instantly share code, notes, and snippets.

@0x9900
Last active July 8, 2022 01:26
Show Gist options
  • Save 0x9900/b61a4bd83e8a522120cc to your computer and use it in GitHub Desktop.
Save 0x9900/b61a4bd83e8a522120cc to your computer and use it in GitHub Desktop.
Python filecache
class FileCache(object):
use_cache = True
def __init__(self, expire=CACHE_EXPIRATION):
self._expire = expire
def __call__(self, func):
"""Simple cache decorator. Cache the values in a file in `/var/tmp`"""
@wraps(func)
def wrapped_function(*args):
assert not LG.debug("%r", func.__name__)
filename = self.mk_cache_filename(func.__name__)
if not self.use_cache:
assert not LG.debug("Cache disabled")
# invalidating the cache
try:
os.unlink(filename)
except OSError:
pass
return func(*args)
try:
if os.path.exists(filename):
fstat = os.stat(filename)
if fstat.st_mtime + self._expire < time.time():
os.unlink(filename)
except IOError as err:
LG.warning('NO CACHING: File cache error: %s', err)
return func(*args)
try:
with open(filename) as fdcache:
result = cPickle.load(fdcache)
except KeyError as err:
LG.warning('Cache file error: %s', err)
except IOError as err:
LG.warning('Cache file %s error. re-creating it', filename)
else:
LG.info('%s information read from cache', func.__name__)
return result
result = func(*args)
assert not LG.info("Caching %r - %s", func.__name__, result)
with open(filename, 'w') as fdcache:
cPickle.dump(result, fdcache)
return result
return wrapped_function
@staticmethod
def mk_cache_filename(name):
return '/tmp/.hcache-' + md5(str(name)).hexdigest()
@staticmethod
def disable():
FileCache.use_cache = False
@staticmethod
def enable():
FileCache.use_cache = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment