Spaces:
Sleeping
Sleeping
Update cache.py
Browse files
cache.py
CHANGED
|
@@ -1,72 +1,62 @@
|
|
| 1 |
-
import time
|
| 2 |
-
import uuid
|
| 3 |
-
from typing import Dict, Tuple, Optional
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
self.
|
| 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 |
-
self._cache
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if extraction_id in self._id_mapping:
|
| 64 |
-
file_hash = self._id_mapping[extraction_id]
|
| 65 |
-
if file_hash in self._cache:
|
| 66 |
-
_, timestamp = self._cache[file_hash]
|
| 67 |
-
return time.time() - timestamp < self._expiry_seconds
|
| 68 |
-
return False
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Singleton instance
|
| 72 |
-
textract_cache = TextractCache()
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import uuid
|
| 3 |
+
from typing import Dict, Tuple, Optional
|
| 4 |
+
|
| 5 |
+
class TextractCache:
|
| 6 |
+
def __init__(self, expiry_seconds: int = 3600):
|
| 7 |
+
self._cache: Dict[str, Tuple[str, float]] = {}
|
| 8 |
+
self._id_mapping: Dict[str, str] = {}
|
| 9 |
+
self._expiry_seconds = expiry_seconds
|
| 10 |
+
|
| 11 |
+
def get(self, key: str) -> Optional[str]:
|
| 12 |
+
"""Get cached text if it exists and hasn't expired"""
|
| 13 |
+
|
| 14 |
+
if key in self._id_mapping:
|
| 15 |
+
key = self._id_mapping[key]
|
| 16 |
+
|
| 17 |
+
if key in self._cache:
|
| 18 |
+
text, timestamp = self._cache[key]
|
| 19 |
+
if time.time() - timestamp < self._expiry_seconds:
|
| 20 |
+
return text
|
| 21 |
+
else:
|
| 22 |
+
del self._cache[key]
|
| 23 |
+
self._id_mapping = {
|
| 24 |
+
k: v for k, v in self._id_mapping.items() if v != key
|
| 25 |
+
}
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
def set(self, key: str, text: str) -> str:
|
| 29 |
+
"""Cache extracted text with current timestamp and return a unique ID"""
|
| 30 |
+
self._cache[key] = (text, time.time())
|
| 31 |
+
|
| 32 |
+
extraction_id = str(uuid.uuid4())
|
| 33 |
+
self._id_mapping[extraction_id] = key
|
| 34 |
+
|
| 35 |
+
return extraction_id
|
| 36 |
+
|
| 37 |
+
def clear(self, key: str = None) -> None:
|
| 38 |
+
"""Clear a specific entry or the entire cache"""
|
| 39 |
+
if key is not None:
|
| 40 |
+
if key in self._id_mapping:
|
| 41 |
+
file_hash = self._id_mapping[key]
|
| 42 |
+
if file_hash in self._cache:
|
| 43 |
+
del self._cache[file_hash]
|
| 44 |
+
del self._id_mapping[key]
|
| 45 |
+
elif key in self._cache:
|
| 46 |
+
del self._cache[key]
|
| 47 |
+
self._id_mapping = {
|
| 48 |
+
k: v for k, v in self._id_mapping.items() if v != key
|
| 49 |
+
}
|
| 50 |
+
else:
|
| 51 |
+
self._cache.clear()
|
| 52 |
+
self._id_mapping.clear()
|
| 53 |
+
|
| 54 |
+
def is_valid_id(self, extraction_id: str) -> bool:
|
| 55 |
+
"""Check if an extraction ID exists and is valid"""
|
| 56 |
+
if extraction_id in self._id_mapping:
|
| 57 |
+
file_hash = self._id_mapping[extraction_id]
|
| 58 |
+
if file_hash in self._cache:
|
| 59 |
+
_, timestamp = self._cache[file_hash]
|
| 60 |
+
return time.time() - timestamp < self._expiry_seconds
|
| 61 |
+
return False
|
| 62 |
+
textract_cache = TextractCache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|