MemoryCache
In-memory cache storage for secrets.
Overview
This implementation stores secrets in a Python dictionary with optional TTL support. Expired entries are lazily cleaned up on access. The current time is injected via the dependency injection system, making it easy to mock time in tests.
Example Usage
from clearskies.secrets.cache_storage import MemoryCache
from clearskies.secrets.akeyless import Akeyless
# Create cache with default 5-minute TTL
cache = MemoryCache(default_ttl=300)
akeyless = Akeyless(
access_id="p-xxx",
cache_storage=cache,
)
# First call fetches from Akeyless and caches for 5 minutes
secret = akeyless.get("/path/to/secret")
# Subsequent calls within 5 minutes return cached value
secret = akeyless.get("/path/to/secret")
# Force refresh bypasses cache
secret = akeyless.get("/path/to/secret", refresh=True)
Testing with Mocked Time
The cache uses inject.Now() for time, which can be controlled via the DI container:
from clearskies.di import Di
from clearskies.secrets.cache_storage import MemoryCache
import datetime
di = Di()
di.set_now(datetime.datetime(2024, 1, 1, 12, 0, 0))
cache = MemoryCache(default_ttl=60)
cache.injectable_properties(di=di)
cache.set("/secret", "value")
# Advance time past TTL
di.set_now(datetime.datetime(2024, 1, 1, 12, 1, 1))
# Entry is now expired
assert cache.get("/secret") is None
default_ttl
Optional