-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.py
34 lines (26 loc) · 805 Bytes
/
cache.py
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
import os
import pickle
CACHE_DIR = './cache'
def listcache(q=None):
objnames = [fname.split('.')[0] for fname in os.listdir(CACHE_DIR) if '.pkl' in fname]
if q:
objnames = [objname for objname in objnames if q in objname]
return objnames
def findcache(q=None):
caches = listcache(q)
if len(caches) > 0:
return caches[0]
return None
def writecache(objname, obj):
with open(f'{CACHE_DIR}/{objname}.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def readcache(objname):
try:
with open(f'{CACHE_DIR}/{objname}.pkl', 'rb') as f:
obj = pickle.load(f)
return obj
except Exception as e:
print(e)
return None
def purgecache(objname):
return os.remove(f'{CACHE_DIR}/{objname}.pkl')