Manage temporary files properly.

This commit is contained in:
Alberto Planas 2013-08-06 16:25:02 +02:00
parent 88d2a9ae03
commit 8fc0f125db

View File

@ -234,10 +234,10 @@ def memoize(ttl=None):
""" """
# Configuration variables # Configuration variables
TMPDIR = '/tmp' # Where the cache files are stored TMPDIR = '~/.cr-cache' # Where the cache files are stored
SLOTS = 4096 # Number of slots in the cache file SLOTS = 4096 # Number of slots in the cache file
NCLEAN = 1024 # Number of slots to remove when limit reached NCLEAN = 1024 # Number of slots to remove when limit reached
TIMEOUT = 60*60*2 # Time to live for every cache slot (seconds) TIMEOUT = 60*60*2 # Time to live for every cache slot (seconds)
def _memoize(f): def _memoize(f):
def _clean_cache(): def _clean_cache():
@ -262,7 +262,10 @@ def memoize(ttl=None):
_clean_cache() _clean_cache()
return value return value
cache_name = os.path.join(TMPDIR, f.__name__) cache_dir = os.path.expanduser(TMPDIR)
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
cache_name = os.path.join(cache_dir, f.__name__)
cache = shelve.open(cache_name, protocol=-1) cache = shelve.open(cache_name, protocol=-1)
return _f return _f