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

CacheKey(components)

Immutable cache key that can include multiple components.

SingleValueTokenCache(token_provider)

Simplified token cache for single values (no key needed).

TokenCache(token_provider)

Generic token-based cache with automatic invalidation.

class objectstate.token_cache.CacheKey(components: Tuple[Any, ...])[source]

Immutable cache key that can include multiple components.

components: Tuple[Any, ...]
classmethod from_args(*args) CacheKey[source]

Create cache key from variable arguments.

__init__(components: Tuple[Any, ...]) None
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

invalidate()[source]

Manually invalidate the entire cache.

get(key: CacheKey) T | None[source]

Get cached value without computing.

Parameters:

key – Cache key

Returns:

Cached value or None if not found or token changed

put(key: CacheKey, value: T)[source]

Put value in cache.

Parameters:
  • key – Cache key

  • value – Value to cache

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

get_or_compute(compute_fn: Callable[[], T]) T[source]

Get cached value or compute and cache it.

Parameters:

compute_fn – Function to compute value if cache miss

Returns:

Cached or computed value

invalidate()[source]

Manually invalidate the cache.