File size: 557 Bytes
d830fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import os
import pickle


CACHE_DIR = "/tmp/qa_cache"
os.makedirs(CACHE_DIR, exist_ok=True)


memory_cache = {}


def _key(text):
return hashlib.md5(text.encode()).hexdigest()


def get(text):
k = _key(text)
if k in memory_cache:
return memory_cache[k]
path = os.path.join(CACHE_DIR, k)
if os.path.exists(path):
with open(path, "rb") as f:
memory_cache[k] = pickle.load(f)
return memory_cache[k]
return None


def set(text, value):
k = _key(text)
memory_cache[k] = value
with open(os.path.join(CACHE_DIR, k), "wb") as f:
pickle.dump(value, f)