Cache¶
cache ¶
Classes¶
Empty ¶
Sentinel type representing an empty/missing value.
Used to distinguish between None (a valid value) and a truly missing value. Always evaluates to False in boolean context.
Example
Placeholder ¶
Sentinel type representing a placeholder value.
Used to mark positions where a value will be provided later. Always evaluates to True in boolean context.
CacheMiss ¶
Sentinel type representing a cache miss.
Used to distinguish between cached None values and keys that don't exist. Always evaluates to False in boolean context.
Negative ¶
Sentinel type representing a negative cache entry.
Used to mark keys that are known to be absent, preventing cache penetration. Always evaluates to False in boolean context.
KeyBuilder ¶
Utility class for building cache keys with a consistent format.
Provides methods to construct namespaced cache keys and validate key formats.
Attributes:
| Name | Type | Description |
|---|---|---|
split_char | Character used to split parts of the key. | |
prefix | Prefix to prepend to all keys. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split_char | str | Separator character for key parts. Defaults to ":". | ':' |
prefix | str | Namespace prefix for all keys. Defaults to normalized project title. | replace(' ', '_') |
Example
Source code in audex/lib/cache/__init__.py
Functions¶
build ¶
Build a cache key by joining the prefix and parts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parts | str | Parts to include in the key. | () |
Returns:
| Type | Description |
|---|---|
str | The constructed cache key. |
Source code in audex/lib/cache/__init__.py
validate ¶
Validate if a given key starts with the defined prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | The cache key to validate. | required |
Returns:
| Type | Description |
|---|---|
bool | True if the key starts with the prefix, False otherwise. |
Source code in audex/lib/cache/__init__.py
KVCache ¶
Bases: LoggingMixin, AsyncContextMixin, ABC
Abstract base class for async key-value cache implementations.
Provides an async dictionary-like interface for caching with support for TTL, atomic operations (incr/decr), and key validation. Implementations can use in-memory storage, Redis, or other backends.
The cache supports: - Async dict operations (get, set, delete, etc.) - Time-to-live (TTL) for automatic expiration - Atomic increment/decrement operations - Key namespace management via KeyBuilder
Example
# Using a cache implementation
cache = await make_cache(config, logger)
# Basic operations
await cache.set("user:123", {"name": "Alice"})
user = await cache.get("user:123")
# With TTL
await cache.setx("session:abc", {"data": "..."}, ttl=3600)
# Atomic operations
await cache.incr("counter:visits")
await cache.decr("counter:remaining", amount=5)
Source code in audex/helper/mixin.py
Attributes¶
key_builder abstractmethod property ¶
key_builder: KeyBuilder
Get the KeyBuilder instance used for constructing cache keys.
Functions¶
get_item abstractmethod async ¶
set_item abstractmethod async ¶
del_item abstractmethod async ¶
iter_keys abstractmethod ¶
len abstractmethod async ¶
contains abstractmethod async ¶
get abstractmethod async ¶
Get an item from the cache, returning default if the key does not exist.
setdefault abstractmethod async ¶
clear abstractmethod async ¶
pop abstractmethod async ¶
popitem abstractmethod async ¶
set abstractmethod async ¶
setx abstractmethod async ¶
Set an item in the cache with the specified key, value, and optional TTL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
value | VT | Value to store. | required |
ttl | int | None | Time-to-live in seconds. None means use default TTL. | None |
Source code in audex/lib/cache/__init__.py
set_negative abstractmethod async ¶
Store cache miss marker to prevent cache penetration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
ttl abstractmethod async ¶
Get the time-to-live (TTL) for a specific key in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
Returns:
| Type | Description |
|---|---|
int | None | Remaining TTL in seconds, or None if no TTL is set. |
expire abstractmethod async ¶
Set the time-to-live (TTL) for a specific key in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
ttl | int | None | Time-to-live in seconds. None means use default TTL. | None |
Source code in audex/lib/cache/__init__.py
incr abstractmethod async ¶
Increment the integer value of a key by the given amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
amount | int | Amount to increment by. Defaults to 1. | 1 |
Returns:
| Type | Description |
|---|---|
int | The new value after incrementing. |
Source code in audex/lib/cache/__init__.py
decr abstractmethod async ¶
Decrement the integer value of a key by the given amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key. | required |
amount | int | Amount to decrement by. Defaults to 1. | 1 |
Returns:
| Type | Description |
|---|---|
int | The new value after decrementing. |
Source code in audex/lib/cache/__init__.py
keys abstractmethod async ¶
values abstractmethod async ¶
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true