Memory Cache¶
memory_cache ¶
Memory Cache - LRU Cache Implementation.
Provides Least Recently Used (LRU) cache with TTL support for general-purpose caching.
Classes:
| Name | Description |
|---|---|
MemoryCache | In-memory LRU cache with TTL and max size enforcement |
Classes¶
MemoryCache ¶
In-memory LRU cache implementation.
Provides LRU eviction policy, TTL support, max size enforcement, and basic thread-safe operations.
Attributes:
| Name | Type | Description |
|---|---|---|
max_size | int | Maximum number of entries in cache |
default_ttl | int | Default TTL in seconds (0 = no expiration) |
_cache | OrderedDict | Ordered dictionary storing cached values |
_expiry | dict[str, Optional[datetime]] | Expiry timestamps for cache entries |
Methods:
| Name | Description |
|---|---|
get | Get value from cache |
set | Set value in cache |
delete | Delete entry from cache |
clear | Clear all cache entries |
exists | Check if key exists in cache |
size | Get current cache size |
get_stats | Get cache statistics |
Initialize memory cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size | int | Maximum number of cache entries. | 1000 |
default_ttl | int | Default TTL in seconds (0 = no expiration). | 0 |
Source code in src/infrastructure/cache/memory_cache.py
Functions¶
get ¶
Get value from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
Optional[Any] | Cached value or None if not found/expired |
Source code in src/infrastructure/cache/memory_cache.py
set ¶
Set value in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
value | Any | Value to cache | required |
ttl | Optional[int] | TTL in seconds (if None, uses default_ttl) | None |
Source code in src/infrastructure/cache/memory_cache.py
delete ¶
Delete entry from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if deleted, False if key not found |
Source code in src/infrastructure/cache/memory_cache.py
clear ¶
Clear all cache entries.
Source code in src/infrastructure/cache/memory_cache.py
exists ¶
Check if key exists in cache (and not expired).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Cache key | required |
Returns:
| Type | Description |
|---|---|
bool | True if exists and not expired |
Source code in src/infrastructure/cache/memory_cache.py
size ¶
Get current cache size.
Returns:
| Type | Description |
|---|---|
int | Number of entries in cache |
get_stats ¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict | Statistics including size, max_size, usage percentage |