social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1import time
2from typing import Generic, TypeVar
3
4K = TypeVar("K")
5V = TypeVar("V")
6
7class TTLCache(Generic[K, V]):
8 def __init__(self, ttl_seconds: int = 3600) -> None:
9 self.ttl: int = ttl_seconds
10 self.__cache: dict[K, tuple[V, float]] = {}
11
12 def get(self, key: K) -> V | None:
13 if key in self.__cache:
14 value, timestamp = self.__cache[key]
15 if time.time() - timestamp < self.ttl:
16 return value
17 else:
18 del self.__cache[key]
19 return None
20
21 def set(self, key: K, value: V) -> None:
22 self.__cache[key] = (value, time.time())
23
24 def clear(self) -> None:
25 self.__cache.clear()