-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_store.py
41 lines (29 loc) · 1.01 KB
/
redis_store.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
31
32
33
34
35
36
37
38
39
40
41
from datetime import datetime, timedelta
from dataclasses import dataclass
from typing import Any
@dataclass
class ValueWithExpiry:
value: Any
expiresAt: datetime.time = 0
def is_expired(v: ValueWithExpiry) -> bool:
if v.expiresAt == 0: return False
return v.expiresAt < datetime.now()
class Storage:
def __init__(self):
self.map = dict()
def set(self, key: str, value: str):
self.map[key] = ValueWithExpiry(value)
def set_with_expiry(self, key: str, value: str, expiry: timedelta):
expires = datetime.now() + timedelta(milliseconds=expiry)
self.map[key] = ValueWithExpiry(
value=value,
expiresAt=expires
)
def get(self, key):
valueWithExpiry: ValueWithExpiry = self.map.get(key, None)
if not valueWithExpiry:
return "", False
if is_expired(valueWithExpiry):
del self.map[key]
return "", False
return valueWithExpiry.value, True