-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
50 lines (41 loc) · 1.55 KB
/
cache.py
File metadata and controls
50 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
On disk cache decorator
found here: http://chase-seibert.github.io/blog/2011/11/23/pythondjango-disk-based-caching-decorator.html
example:
@cache_disk(seconds = 900, cache_folder="/tmp"):
def get_netflix_favorites(account_id):
... do somthing really expensive
return {
"account_id": account_id,
"data": {
... more stuff here
}
}
"""
import os
import hashlib
import time
import pickle
import logging
def cache_disk(seconds = 900, cache_folder="/tmp"):
def doCache(f):
def inner_function(*args, **kwargs):
# calculate a cache key based on the decorated method signature
key = hashlib.sha224(str(f.__module__) + str(f.__name__) + str(args) + str(kwargs)).hexdigest()
filepath = os.path.join(cache_folder, key)
# verify that the cached object exists and is less than $seconds old
if os.path.exists(filepath):
modified = os.path.getmtime(filepath)
age_seconds = time.time() - modified
if age_seconds < seconds:
logging.info('cache found - using %s'%filepath)
return pickle.load(open(filepath, "rb"))
else:
logging.info('cache found - but too old')
# call the decorated function...
result = f(*args, **kwargs)
# ... and save the cached object for next time
pickle.dump(result, open(filepath, "wb"), pickle.HIGHEST_PROTOCOL)
return result
return inner_function
return doCache