upd
This commit is contained in:
parent
f4e3fae4fb
commit
f30ccf3622
@ -1,10 +1,11 @@
|
|||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
from typing import Dict, Tuple, Any
|
||||||
|
|
||||||
class TranscriptionDict:
|
class TranscriptionDict:
|
||||||
def __init__(self, max_size=1000):
|
def __init__(self, max_size=1000):
|
||||||
self.max_size = max_size
|
self.max_size = max_size
|
||||||
self._dict = {}
|
self._dict: Dict[str, Tuple[str, float]] = {}
|
||||||
|
|
||||||
# Start a thread to clean expired items every 3 minutes
|
# Start a thread to clean expired items every 3 minutes
|
||||||
self._clean_thread = threading.Thread(target=self._clean_expired_items_periodically)
|
self._clean_thread = threading.Thread(target=self._clean_expired_items_periodically)
|
||||||
@ -15,10 +16,10 @@ class TranscriptionDict:
|
|||||||
return len(self._dict)
|
return len(self._dict)
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
return self._dict[key]
|
return self._dict[key]['value']
|
||||||
|
|
||||||
def set(self, key, value):
|
def set(self, key, value):
|
||||||
self._dict[key] = value
|
self._dict[key] = {'value': value, 'timestamp': time.time()}
|
||||||
if len(self._dict) > self.max_size:
|
if len(self._dict) > self.max_size:
|
||||||
self._clean_expired_items()
|
self._clean_expired_items()
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ class TranscriptionDict:
|
|||||||
expired_items = [k for k, v in self._dict.items() if now - v[1] >= 180] # 180 seconds = 3 minutes
|
expired_items = [k for k, v in self._dict.items() if now - v[1] >= 180] # 180 seconds = 3 minutes
|
||||||
for key in expired_items:
|
for key in expired_items:
|
||||||
del self._dict[key]
|
del self._dict[key]
|
||||||
|
print(f"Cleaned {len(expired_items)} expired items from the cache")
|
||||||
|
|
||||||
def _clean_expired_items_periodically(self):
|
def _clean_expired_items_periodically(self):
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user