Backend
mongo_ops.cache.backend
Summary
Cache backend abstraction.
Classes
CacheBackend
Bases: ABC
Abstract interface for cache backends.
Implementations store byte-encoded values keyed by string, track usage statistics, and manage their own lifecycle. The in-memory and Redis backends both implement this contract.
Functions
clear_pattern
abstractmethod
async
Remove all keys matching a glob pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern |
str
|
Glob-style pattern; a trailing |
required |
delete
abstractmethod
async
Remove a key from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
The cache key. |
required |
exists
abstractmethod
async
Check whether a key is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
The cache key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the key exists, False otherwise. |
get
abstractmethod
async
Fetch a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
The cache key. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
Optional[bytes]: The cached bytes, or None on a miss. |
get_stats
abstractmethod
async
Return a snapshot of cache statistics.
Returns:
| Name | Type | Description |
|---|---|---|
CacheStats |
CacheStats
|
A copy of the current stats counters. |
initialize
abstractmethod
async
Start background resources owned by the backend.
Should be called once during application startup, after the repositories are connected.
set
abstractmethod
async
Store a value in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
The cache key. |
required |
value |
bytes
|
The byte-encoded value to store. |
required |
ttl |
Optional[int]
|
Time-to-live in seconds. When None, the backend default applies. |
None
|
shutdown
abstractmethod
async
Stop and release background resources.
Should be called once during application shutdown.
CacheStats
dataclass
Snapshot of cache usage and activity counters.
Attributes:
| Name | Type | Description |
|---|---|---|
hits |
int
|
Number of get() calls that found a value. |
misses |
int
|
Number of get() calls that returned None. |
sets |
int
|
Number of values written to the cache. |
deletes |
int
|
Number of keys removed. |
current_size |
int
|
Number of entries currently held. |
max_size |
int
|
Maximum number of entries the cache allows (0 = unbounded). |
CircularReferenceError
Bases: ValueError
Raised when population detects a cycle in the reference graph.
Attributes:
| Name | Type | Description |
|---|---|---|
collection |
str
|
Collection where the cycle was detected. |
doc_id |
ObjectId
|
Document ID where the cycle was detected. |
path |
list[str]
|
Ordered labels describing the visited reference path. |
Initialize the error with cycle metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection |
str
|
Collection where the cycle was detected. |
required |
doc_id |
ObjectId
|
Document ID where the cycle was detected. |
required |
path |
list[str]
|
Ordered labels describing the visited reference path. |
required |