-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachefileHandler.py
More file actions
50 lines (37 loc) · 1.26 KB
/
CachefileHandler.py
File metadata and controls
50 lines (37 loc) · 1.26 KB
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
45
46
47
48
49
import os
import pickle
from pathlib import Path
def make_cache_hashname(datas, header=None, tail=None):
import hashlib
tuples_byte = pickle.dumps(datas)
hash_handler = hashlib.sha3_224()
hash_handler.update(tuples_byte)
cache_filename = hash_handler.digest().hex()
if header is not None:
cache_filename = header + cache_filename
if tail is not None:
cache_filename = cache_filename + tail
return cache_filename
def save_cache(save_data, cache_filename, new_cache_dir=False):
import os
from pathlib import Path
if new_cache_dir:
cache_path = cache_filename
else:
cache_path = str(Path.home()) + "/ssddata/cache/" + cache_filename
with open(cache_path, "wb") as cache_file:
pickle.dump(save_data, cache_file)
def load_cache(cache_filename, new_cache_dir=False):
if new_cache_dir:
cache_path = cache_filename
else:
cache_path = str(Path.home()) + "/ssddata/cache/" + cache_filename
rt_data = None
print(cache_path)
if os.path.isfile(cache_path):
print("Cache file found")
with open(cache_path, "rb") as cache_file:
rt_data = pickle.load(cache_file)
else:
print("No Cache file found")
return rt_data