-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
41 lines (33 loc) · 988 Bytes
/
memory.py
File metadata and controls
41 lines (33 loc) · 988 Bytes
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
import redis
from config import set_memory
import datetime
class Memory:
def __init__(self) -> None:
self.data = {}
def set(self, id:str, data: str, ex:int):
self.data[id] = {
'data': data,
'ex': ex,
'created': datetime.datetime.now()
}
def get(self, id:str):
self.clear_memory()
return self.data[id]['data']
def is_expired(self, data) -> bool:
spend = datetime.datetime.now() - data['created']
if spend.seconds > data['ex']: return True
return False
def clear_memory(self):
keys = []
for k in self.data.keys():
if self.is_expired(self.data[k]):
keys.append(k)
for k in keys:
del self.data[k]
if set_memory == 'self':
memory = Memory()
elif set_memory == 'redis':
memory = redis.Redis(host='localhost', port=6379, db=0)
else:
print("ERROR! Settings memory in congig.py ")
exit(1)