-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_cache.py
More file actions
85 lines (66 loc) · 2.24 KB
/
basic_cache.py
File metadata and controls
85 lines (66 loc) · 2.24 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Cache module for saving and loading data to and from a file.
"""
import json
import logging
from pathlib import Path
from dir_and_file_functions import ensure_dir
logger = logging.getLogger(__name__)
def load_cache(path: Path = Path("cache.json")) -> dict:
"""
Loads a cache from the given path.
Args:
path (typing.Union[pathlib.Path, str], optional): The path of the cache file to load. Defaults to "cache.json".
Returns:
dict: The loaded cache.
"""
path = Path(path)
logger.debug(f"Loading cache file {json.dumps(str(path))}...")
data = {}
if path.exists():
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
logger.debug("Loaded cache.")
except json.JSONDecodeError:
logger.exception("Failed to decode cache file. Using empty cache.")
except OSError:
logger.exception("Failed to read cache file. Using empty cache.")
else:
logger.info("Cache file does not exist. Using empty cache.")
return data
def validate_cache(data: dict) -> None:
"""
Validates the given cache data.
Args:
cache (dict): The cache data to validate.
"""
logger.debug("Validating cache...")
for file_path in list(data["files"].keys()):
if not Path(file_path).exists():
logger.debug("Removing non-existent file %s from cache.", json.dumps(str(file_path)))
del data["files"][file_path]
logger.debug("Cache validated successfully.")
def save_cache(data: dict, path: Path = Path("cache.json")) -> None:
"""
Saves the given cache data to the specified path.
Args:
data (dict): The cache data to save.
path (str | Path, optional): The path of the cache file to save. Defaults to "cache.json".
"""
path = Path(path)
logger.debug(f"Saving cache to {json.dumps(str(path))}...")
try:
ensure_dir(path)
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
logger.debug("Saved cache.")
except Exception:
logger.exception("Failed to save cache.")
raise
# Example usage
cache = load_cache()
cache["a"] = "a"
cache["b"] = 2
cache["c"] = ["c"]
save_cache(cache)