objectstate.token_cache
Token-based cache invalidation service.
Provides a reusable abstraction for caching values that should be invalidated when a global token changes (e.g., when any form value changes).
Classes
|
Immutable cache key that can include multiple components. |
|
Simplified token cache for single values (no key needed). |
|
Generic token-based cache with automatic invalidation. |
- class objectstate.token_cache.CacheKey(components: Tuple[Any, ...])[source]
Immutable cache key that can include multiple components.
- class objectstate.token_cache.TokenCache(token_provider: Callable[[], int])[source]
Generic token-based cache with automatic invalidation.
The cache is invalidated when the token changes. This is useful for caching values that depend on global state (e.g., form values) that can change.
Example
# Create cache with token provider cache = TokenCache(lambda: ParameterFormManager._live_context_token_counter)
# Get or compute value value = cache.get_or_compute(
key=CacheKey.from_args(‘scope’, ‘param_name’), compute_fn=lambda: expensive_computation()
)
# Cache is automatically invalidated when token changes
- __init__(token_provider: Callable[[], int])[source]
Initialize token cache.
- Parameters:
token_provider – Function that returns the current token value
- get_or_compute(key: CacheKey, compute_fn: Callable[[], T]) T[source]
Get cached value or compute and cache it.
- Parameters:
key – Cache key
compute_fn – Function to compute value if cache miss
- Returns:
Cached or computed value
- class objectstate.token_cache.SingleValueTokenCache(token_provider: Callable[[], int])[source]
Simplified token cache for single values (no key needed).
Useful when you only need to cache one value that depends on a token.
Example
cache = SingleValueTokenCache(lambda: token_counter) value = cache.get_or_compute(lambda: expensive_computation())
- __init__(token_provider: Callable[[], int])[source]
Initialize single-value token cache.
- Parameters:
token_provider – Function that returns the current token value