-
Notifications
You must be signed in to change notification settings - Fork 28
/
cache.py
44 lines (33 loc) · 1.16 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
"""`Cache` offers put/get inferface for a simple key-value store on disk."""
import os
import pickle
import logging
CACHE_DIR = "./cache/"
LOGGER = logging.getLogger(__name__)
def put(key, obj):
"""Puts a key value pair into the store."""
LOGGER.debug("Putting a new cache with key %s", key)
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
with open(CACHE_DIR + key + ".pkl", 'wb') as fout:
pickle.dump(obj, fout, pickle.HIGHEST_PROTOCOL)
def get(key):
"""Gets the value stored using the given key."""
LOGGER.debug("Getting a cache with key %s", key)
try:
with open(CACHE_DIR + key + ".pkl", 'rb') as fout:
return pickle.load(fout)
except FileNotFoundError:
LOGGER.debug("No cache file found")
return None
def get_hash(links, nodes):
"""Gets the hash value of the given links and nodes."""
import hashlib
__hash = 0.0
for link in links:
__hash += link.__hash__()
__hash += link.__hash__()
for node in nodes:
__hash += node.__hash__()
__hash += node.__hash__()
return hashlib.md5(('%d' % __hash).encode('utf-8')).hexdigest()