-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
30 lines (22 loc) · 839 Bytes
/
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
import time
from constants import cfg
class Cache:
# Save cache data into a dictionary
def __init__(self):
self.__cache_dict = {}
def check_cache(self, query):
if self.__cache_dict.get(query):
d = self.__cache_dict.get(query)
if round(time.time()) - d[1] < cfg.get_caching_time():
print('Data found in cache... \n Data is valid')
return d[0]
else:
print('Data found in cache... \n Data is older than ' + str(cfg.get_caching_time()) + \
' seconds, requesting new data...')
return None
else:
print('Data not found in cache, requesting new data...')
return None
def add_cache(self, key, data):
self.__cache_dict[key] = data
cache = Cache()