-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.py
38 lines (29 loc) · 1.06 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
import logging
import os
import pickle
import shutil
import util
def _get_path(namespace, key=None):
ns_cache_path = os.path.join(util.get_root_fs(), 'cache', namespace)
os.makedirs(ns_cache_path, exist_ok=True)
if key:
return os.path.join(str(ns_cache_path), f'{key}.pkl')
return ns_cache_path
def get(namespace, key):
try:
with open(_get_path(namespace, f'{key.strip().lower()}'), 'rb') as f:
value = pickle.load(f)
logging.info(f'cache hit for {key} in namespace: {namespace}, with value: {value}')
return value
except FileNotFoundError:
return None
def put(namespace, key, value):
with open(_get_path(namespace, f'{key.strip().lower()}'), 'wb') as f:
pickle.dump(value, f)
def clear(namespace):
namespace_path = _get_path(namespace)
if os.path.exists(namespace_path):
shutil.rmtree(namespace_path)
logging.info(f'cache for namespace {namespace} has been cleared.')
else:
logging.warning(f'cache for namespace {namespace} does not exist.')