Scoped cache
provide.foundation.utils.scoped_cache
¶
TODO: Add module docstring.
Classes¶
ContextScopedCache
¶
Bases: Generic[K, V]
Thread-safe, async-safe cache scoped to context managers.
Unlike global LRU caches (for memoization), this provides isolated cache instances per execution context - ideal for recursive operations that need temporary storage without memory leaks.
The cache uses ContextVar for automatic thread/async isolation, and context managers for automatic cleanup. Nested contexts reuse the parent's cache to maintain consistency within an operation.
Examples:
>>> cache = ContextScopedCache[str, int]("user_ids")
>>>
>>> with cache.scope():
... cache.set("alice", 1)
... cache.set("bob", 2)
... print(cache.get("alice")) # 1
...
>>> # Cache is automatically cleared when exiting scope
>>> with cache.scope():
... print(cache.get("alice")) # None (fresh scope)
Nested contexts reuse parent cache:
>>> with cache.scope():
... cache.set("key", "outer")
... with cache.scope():
... print(cache.get("key")) # "outer" (same cache)
... cache.set("key", "inner")
... print(cache.get("key")) # "inner" (modified in nested scope)
Initialize a context-scoped cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier for the cache (used in ContextVar name) |
'cache'
|
Source code in provide/foundation/utils/scoped_cache.py
Functions¶
clear
¶
Clear current context's cache.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside a cache scope |
Source code in provide/foundation/utils/scoped_cache.py
contains
¶
Check if key exists in current context's cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
Cache key to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key exists in cache |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside a cache scope |
Source code in provide/foundation/utils/scoped_cache.py
get
¶
Get value from current context's cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
Cache key |
required |
default
|
V | None
|
Value to return if key not found |
None
|
Returns:
| Type | Description |
|---|---|
V | None
|
Cached value or default |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside a cache scope |
Source code in provide/foundation/utils/scoped_cache.py
is_active
¶
Check if cache context is currently active.
Returns:
| Type | Description |
|---|---|
bool
|
True if inside a cache scope, False otherwise |
scope
¶
Create an isolated cache scope.
If a cache context already exists (nested call), reuses the existing cache. Otherwise, creates a new cache and cleans it up on exit.
Yields:
| Type | Description |
|---|---|
Generator[None]
|
None (use cache methods within the context) |
Note
Cleanup is guaranteed even on errors.
Source code in provide/foundation/utils/scoped_cache.py
set
¶
Set value in current context's cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
Cache key |
required |
value
|
V
|
Value to cache |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside a cache scope |
Source code in provide/foundation/utils/scoped_cache.py
size
¶
Get number of items in current context's cache.
Returns:
| Type | Description |
|---|---|
int
|
Number of cached items |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside a cache scope |