Skip to content

In Memory

mongo_ops.cache.in_memory

Summary

In-memory cache backend with TTL-based eviction and encode/decode helpers.

Classes

InMemoryCacheBackend

1
2
3
4
5
InMemoryCacheBackend(
    max_entries: int = 10000,
    default_ttl: int = 300,
    cleanup_interval: int = 60,
)

Bases: CacheBackend

Cache backend backed by an in-memory dict with TTL expiry.

Entries are stored in an OrderedDict for LRU-compatible eviction and a min-heap of expiry timestamps drives periodic removal of stale entries.

Notes

Thread safety:

1
2
All operations take an asyncio lock; the backend is safe for
concurrent use within a single event loop.

Initialize the backend.

Parameters:

Name Type Description Default
max_entries int

Maximum number of entries before LRU eviction kicks in.

10000
default_ttl int

Default time-to-live for entries, in seconds.

300
cleanup_interval int

Seconds between periodic expired-entry sweeps.

60
Functions
clear_pattern async
clear_pattern(pattern: str) -> None

Remove all keys matching a glob pattern.

Parameters:

Name Type Description Default
pattern str

Glob-style pattern; a trailing * matches prefixes.

required
delete async
delete(key: str) -> None

Remove a key from the cache.

Parameters:

Name Type Description Default
key str

The cache key.

required
exists async
exists(key: str) -> bool

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 async
get(key: str) -> bytes | None

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 async
get_stats() -> CacheStats

Return a snapshot of cache statistics.

Returns:

Name Type Description
CacheStats CacheStats

A copy of the current stats counters.

initialize async
initialize() -> None

Start the periodic expired-entry cleanup task.

set async
set(key: str, value: bytes, ttl: int | None = None) -> None

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; defaults to the backend default.

None
shutdown async
shutdown() -> None

Cancel and await the cleanup task.

Functions

decode_value

decode_value(data: bytes) -> dict

Decode cache bytes back into a dict.

Parameters:

Name Type Description Default
data bytes

UTF-8 JSON bytes produced by encode_value().

required

Returns:

Name Type Description
dict dict

The decoded dictionary.

encode_value

encode_value(value: dict) -> bytes

Encode a dict into cache-ready bytes.

Non-serializable values (e.g., ObjectId) are coerced with str().

Parameters:

Name Type Description Default
value dict

The dictionary to encode.

required

Returns:

Name Type Description
bytes bytes

UTF-8 JSON bytes.