2026-06-12 01:58:46 +02:00
|
|
|
# retoor <retoor@molodetz.nl>
|
|
|
|
|
|
2026-05-23 06:20:27 +02:00
|
|
|
import time
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TTLCache:
|
|
|
|
|
def __init__(self, ttl: int, max_size: int = 0):
|
|
|
|
|
self.ttl = ttl
|
|
|
|
|
self.max_size = max_size
|
|
|
|
|
self._store = OrderedDict()
|
|
|
|
|
|
feat: add router table entries for uploads, media, zips, forks, tools, proxy, push, docs, openai, devii, xmlrpc, devrant, dbapi, pubsub and add type annotations to cache, config, responses, schemas, seo, and stealth modules
2026-06-16 14:05:44 +02:00
|
|
|
def get(self, key: str):
|
2026-05-23 06:20:27 +02:00
|
|
|
entry = self._store.get(key)
|
|
|
|
|
if entry is None:
|
|
|
|
|
return None
|
|
|
|
|
value, expiry = entry
|
|
|
|
|
if time.time() >= expiry:
|
|
|
|
|
self._store.pop(key, None)
|
|
|
|
|
return None
|
|
|
|
|
self._store.move_to_end(key)
|
|
|
|
|
return value
|
|
|
|
|
|
feat: add router table entries for uploads, media, zips, forks, tools, proxy, push, docs, openai, devii, xmlrpc, devrant, dbapi, pubsub and add type annotations to cache, config, responses, schemas, seo, and stealth modules
2026-06-16 14:05:44 +02:00
|
|
|
def set(self, key: str, value) -> None:
|
2026-05-23 06:20:27 +02:00
|
|
|
self._store[key] = (value, time.time() + self.ttl)
|
|
|
|
|
self._store.move_to_end(key)
|
|
|
|
|
if self.max_size and len(self._store) > self.max_size:
|
|
|
|
|
self._store.popitem(last=False)
|
|
|
|
|
|
feat: add router table entries for uploads, media, zips, forks, tools, proxy, push, docs, openai, devii, xmlrpc, devrant, dbapi, pubsub and add type annotations to cache, config, responses, schemas, seo, and stealth modules
2026-06-16 14:05:44 +02:00
|
|
|
def pop(self, key: str) -> None:
|
2026-05-23 06:20:27 +02:00
|
|
|
self._store.pop(key, None)
|
|
|
|
|
|
feat: add router table entries for uploads, media, zips, forks, tools, proxy, push, docs, openai, devii, xmlrpc, devrant, dbapi, pubsub and add type annotations to cache, config, responses, schemas, seo, and stealth modules
2026-06-16 14:05:44 +02:00
|
|
|
def clear(self) -> None:
|
2026-05-23 06:20:27 +02:00
|
|
|
self._store.clear()
|
|
|
|
|
|
feat: add router table entries for uploads, media, zips, forks, tools, proxy, push, docs, openai, devii, xmlrpc, devrant, dbapi, pubsub and add type annotations to cache, config, responses, schemas, seo, and stealth modules
2026-06-16 14:05:44 +02:00
|
|
|
def items(self) -> list:
|
2026-05-23 06:20:27 +02:00
|
|
|
now = time.time()
|
2026-06-09 18:48:08 +02:00
|
|
|
return [
|
|
|
|
|
(key, value) for key, (value, expiry) in self._store.items() if now < expiry
|
|
|
|
|
]
|