forked from malaohu/OneList--
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcache.py
More file actions
31 lines (23 loc) · 701 Bytes
/
dcache.py
File metadata and controls
31 lines (23 loc) · 701 Bytes
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
import diskcache
import pickle
import hashlib
r = diskcache.Cache('tmp')
class Cache:
CACHED_SECONDS = 768
@classmethod
def get(cls, path):
if cls.has(path):
return pickle.loads(r.get(cls._get_key(path)))
return False
@classmethod
def has(cls, path):
return r.get(cls._get_key(path)) is not None
@classmethod
def set(cls, path, entity, expire=CACHED_SECONDS):
return r.set(cls._get_key(path), pickle.dumps(entity), expire)
@classmethod
def rem(cls, path):
return r.delete(cls._get_key(path))
@staticmethod
def _get_key(path):
return 'onelist:' + hashlib.md5(path.encode()).hexdigest()