When adding caching, ensure (1) correctness via validity/refresh and (2) safety via bounds/staleness resistance.
TTL - margin).def token_provider(*, token_duration: int = 3600):
_cached = [None]
_refresh_at = [0.0]
def generate() -> str:
# local signing; no network
return "bedrock-api-key-..."
def get() -> str:
import time
now = time.monotonic()
if _cached[0] is None or now >= _refresh_at[0]:
_cached[0] = generate()
_refresh_at[0] = now + max(token_duration - 60, token_duration * 0.9)
return _cached[0]
return get
functools.lru_cache(maxsize=...)) or a bounded LRU/size-capped cache.from functools import lru_cache
@lru_cache(maxsize=128)
def get_type_adapter(type_):
# expensive instantiation avoided
return make_adapter(type_)
api_key), either:
Enter the URL of a public GitHub repository