-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_TimedCache.py
More file actions
64 lines (51 loc) · 1.95 KB
/
Copy pathmy_TimedCache.py
File metadata and controls
64 lines (51 loc) · 1.95 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
"""Custom implementation of a timed cache."""
import threading
import time
class TimedCache:
"""Timed cache class."""
def __init__(self):
"""Initialize cache and lock."""
self.cache = {}
self.lock = threading.Lock()
def set(self, key, value, ttl):
"""Set value with TTL."""
expiration_time = time.time() + ttl
with self.lock:
self.cache[key] = (value, expiration_time)
# Запускаем поток для удаления элемента по истечении времени
threading.Thread(target=self._remove_after_ttl, args=(key, ttl)).start()
def get(self, key):
"""Get value by key."""
with self.lock:
if key in self.cache:
value, expiration_time = self.cache[key]
if time.time() < expiration_time:
return value
else:
del self.cache[key]
return None
return None
def _remove_after_ttl(self, key, ttl):
"""Remove key after TTL."""
time.sleep(ttl)
with self.lock:
if key in self.cache:
value, expiration_time = self.cache[key]
if time.time() >= expiration_time:
del self.cache[key]
def __contains__(self, key):
"""Check if key is in cache and not expired."""
with self.lock:
if key in self.cache:
_, expiration_time = self.cache[key]
if time.time() < expiration_time:
return True
else:
del self.cache[key]
return False
# Пример использования
cache = TimedCache()
cache.set("foo", "bar", 5) # Устанавливаем значение с TTL 5 секунд
print(cache.get("foo")) # Вывод: bar
time.sleep(6)
print(cache.get("foo")) # Вывод: None (значение истекло)