-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobal_cache.py
More file actions
45 lines (36 loc) · 1.32 KB
/
global_cache.py
File metadata and controls
45 lines (36 loc) · 1.32 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
class GlobalCache:
_instance = None
@staticmethod
def init():
if GlobalCache._instance is None:
GlobalCache._instance = GlobalCache()
@staticmethod
def contains(category, key):
return GlobalCache.get_instance()._contains(category, key)
@staticmethod
def get_instance():
if GlobalCache._instance is None:
GlobalCache.init()
return GlobalCache._instance
@staticmethod
def get(category, key):
return GlobalCache.get_instance()._get(category, key)
@staticmethod
def add(category, key, item):
return GlobalCache.get_instance()._add(category, key, item)
def __init__(self):
self.storage = dict()
def _contains(self, category, key):
if category not in self.storage:
return False
return key in self.storage[category]
def _add(self, category, key, item):
if category not in self.storage:
self.storage[category] = { key: item }
elif key not in self.storage[category]:
self.storage[category][key] = item
def _get(self, category, key):
# print(self.storage)
if self._contains(category, key):
return self.storage[category][key]
raise ValueError(f'GlobalCache does not contain category {category} and/or key {key}')