Cache Port¶
cache_port
¶
Cache port for abstract caching operations.
Defines the CachePort protocol that application code depends on,
decoupling caching from any specific implementation (in-memory, Redis, etc.).
Responsibilities
- Store and retrieve cached values by key.
- Check cache existence and clear entries.
- Support optional TTL (time-to-live) for entries.
Non-Responsibilities
- Eviction policies (LRU, LFU) — handled by infrastructure.
- Distributed locking or consistency guarantees.
- Serialization of cache values.
CachePort
¶
Bases: OutboundPort[KeyType, ValueType], Protocol
Protocol for caching operations.
Any object with the async get, set, delete, exists,
and clear methods satisfies this protocol.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
KeyType
|
The type of cache keys (typically str). |
required | |
ValueType
|
The type of cached values. |
required |
Source code in src/forging_blocks/application/ports/outbound/cache_port.py
get(key: KeyType) -> ValueType | None
async
¶
Retrieve a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
KeyType
|
The cache key. |
required |
Returns:
| Type | Description |
|---|---|
ValueType | None
|
The cached value, or |
set(key: KeyType, value: ValueType, ttl: float | None = None) -> None
async
¶
Store a value in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
KeyType
|
The cache key. |
required |
value
|
ValueType
|
The value to cache. |
required |
ttl
|
float | None
|
Optional time-to-live in seconds. |
None
|
Source code in src/forging_blocks/application/ports/outbound/cache_port.py
delete(key: KeyType) -> None
async
¶
Remove a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
KeyType
|
The cache key. No-op if the key does not exist. |
required |
exists(key: KeyType) -> bool
async
¶
Check whether a key exists in the cache and has not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
KeyType
|
The cache key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|