Backends¶
AbstractBackend¶
memory_reuse.backends.base.AbstractBackend ¶
Bases: ABC
Interface that all cache backends must implement.
Every method is a coroutine so that network-bound backends (e.g. Redis) can be awaited without blocking the event loop, while the in-memory backend simply returns immediately.
Implementors should document their own connection-lifecycle behaviour (lazy vs eager connection, reconnect logic, etc.).
get
abstractmethod
async
¶
Retrieve the raw bytes stored under key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key to look up. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
The stored bytes, or |
bytes | None
|
expired. |
set
abstractmethod
async
¶
Store value under key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key. |
required |
value
|
bytes
|
Raw bytes to store (typically gzip-compressed JSON). |
required |
ttl
|
int | None
|
Time-to-live in seconds. |
None
|
delete
abstractmethod
async
¶
Remove the entry for key if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key to remove. A no-op if the key does not exist. |
required |
exists
abstractmethod
async
¶
Check whether a non-expired entry exists for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
otherwise. |
flush
abstractmethod
async
¶
Delete all entries managed by this backend instance.
Use with caution in production — this removes every cached value.
ping
abstractmethod
async
¶
Check backend connectivity.
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
otherwise. Should not raise. |
InMemoryBackend¶
memory_reuse.backends.memory.InMemoryBackend ¶
Bases: AbstractBackend
Fully in-memory cache backend — no external dependencies required.
Features:
- TTL — entries are lazily expired on access.
- LRU eviction — when
max_entriesis reached the least-recently-used entry is dropped to make room for the new one. - Thread-safe — an :class:
asyncio.Lockserialises all mutations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_entries
|
int
|
Maximum number of entries to hold before LRU eviction kicks in. Defaults to 10 000. |
_DEFAULT_MAX_ENTRIES
|
Example::
backend = InMemoryBackend(max_entries=500)
await backend.set("key", b"value", ttl=60)
data = await backend.get("key")
size
property
¶
Current number of entries in the store (including not-yet-evicted expired ones).
Returns:
| Type | Description |
|---|---|
int
|
Integer entry count. |
get
async
¶
Return stored bytes for key, or None on miss/expiry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to look up. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
Stored bytes or |
set
async
¶
Store value under key with an optional TTL.
If the store is at capacity, the LRU entry is evicted first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
bytes
|
Bytes to store. |
required |
ttl
|
int | None
|
Time-to-live in seconds. |
None
|
delete
async
¶
Remove the entry for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to remove. No-op if the key does not exist. |
required |
exists
async
¶
Return True if key exists and has not expired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Boolean existence flag. |
ping
async
¶
Always returns True — the in-memory backend is always available.
Returns:
| Type | Description |
|---|---|
bool
|
|
RedisBackend¶
Requires the redis extra (pip install "memory-reuse[redis]").
memory_reuse.backends.redis.RedisBackend ¶
Bases: AbstractBackend
Cache backend that stores data in Redis.
Requires the optional redis extra::
pip install memory-reuse[redis]
The connection is established lazily on the first operation.
Connection errors are converted to :exc:BackendConnectionError so
callers do not need to handle redis-specific exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Redis connection URL (e.g. |
required |
max_connections
|
int
|
Maximum size of the underlying connection pool. Defaults to 20. |
_MAX_CONNECTIONS
|
Example::
import os
backend = RedisBackend(url=os.environ["MEMORY_REUSE_REDIS_URL"])
await backend.set("key", b"value", ttl=300)
data = await backend.get("key")
get
async
¶
Retrieve raw bytes stored under key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
Stored bytes or |
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
set
async
¶
Store value under key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
bytes
|
Bytes to store. |
required |
ttl
|
int | None
|
Time-to-live in seconds. |
None
|
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
delete
async
¶
Remove the entry for key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to remove. |
required |
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
exists
async
¶
Check whether key exists in Redis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
flush
async
¶
Delete only this cache's keys from the current Redis database.
Unlike a raw FLUSHDB, this removes only keys under the configured
key prefix (<key_prefix>:*), using a non-blocking SCAN cursor and
batched DELETE / UNLINK so unrelated application keys — and the
keys of another cache instance sharing the same database — are never
touched. TTLs on surviving keys are unaffected.
When no key prefix was supplied to the backend this is a safe no-op (it
refuses to guess a scope rather than risk deleting unrelated data); use
:meth:flush_database for the explicit, unrestricted FLUSHDB.
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
flush_database
async
¶
Delete every key in the current Redis database via FLUSHDB.
Warning
This is the unrestricted, destructive flush — it removes all keys in
the connected database, including unrelated application data. Prefer
:meth:flush, which only clears this cache's namespace. Provided for
callers that genuinely own a dedicated database and want the fast,
atomic clear.
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
ping
async
¶
Check Redis connectivity.
Returns:
| Type | Description |
|---|---|
bool
|
|
close
async
¶
Close the connection pool gracefully.
Call this during application shutdown to release Redis connections.
acquire_lock
async
¶
Attempt to acquire a per-key lock with an expiry (SET NX PX).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The lock key. |
required |
token
|
str
|
A unique token identifying this holder, checked on release. |
required |
ttl_ms
|
int
|
Lock expiry in milliseconds so a crashed holder cannot deadlock other processes. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
On Redis connectivity failure. |
release_lock
async
¶
Release a per-key lock, but only if this holder still owns it.
Uses a delete-if-owner Lua script so a lock that already expired and was re-acquired by a different holder is never released by mistake. Failures are swallowed — a lock left to expire on its own is safe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The lock key. |
required |
token
|
str
|
The token supplied at acquire time. |
required |
AgentCoreBackend¶
Requires the agentcore extra (pip install "memory-reuse[agentcore]").
memory_reuse.backends.agentcore.AgentCoreSettings
dataclass
¶
AgentCoreSettings(region: str | None = None, memory_id: str | None = None, key_prefix: str = 'memory-reuse:')
Resolved connection settings for the AgentCore backend.
Attributes:
| Name | Type | Description |
|---|---|---|
region |
str | None
|
The AWS region hosting the AgentCore store. |
memory_id |
str | None
|
The AgentCore memory / store resource identifier. |
key_prefix |
str
|
Optional namespace prepended to every managed item id so
that |
memory_reuse.backends.agentcore.AgentCoreBackend ¶
Bases: AbstractBackend
:class:AbstractBackend backed by AWS AgentCore Memory.
Because storage is the managed AgentCore service, a value set in one
execution environment is visible to a get in another (Req 8.2).
Values are stored as raw bytes, base64-encoded on write and decoded on
read, so any 0..1_048_576-byte payload round-trips unchanged (Req 8.5).
A ttl greater than zero is recorded as an absolute expiry and filtered
lazily on read; a missing or zero ttl persists until the entry is
explicitly deleted or flushed (Req 8.3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
AgentCoreSettings
|
Resolved connection settings (region, memory id, key prefix). |
required |
client
|
Any | None
|
An already-constructed AgentCore client. When |
None
|
Example::
backend = AgentCoreBackend(
AgentCoreSettings(region="us-east-1", memory_id="mem-123")
)
await backend.set("key", b"value", ttl=300)
data = await backend.get("key")
get
async
¶
Return stored bytes for key or None on miss/expiry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to look up. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
The stored bytes, or |
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
If the AgentCore service is unreachable. |
set
async
¶
Store value under key with an optional TTL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
bytes
|
Raw bytes to store. |
required |
ttl
|
int | None
|
Time-to-live in seconds. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
BackendConnectionError
|
If the AgentCore service is unreachable. |
delete
async
¶
Remove the entry for key; a no-op when the key is absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to remove. |
required |
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
If the AgentCore service is unreachable. |
exists
async
¶
Return True only for an unexpired entry under key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
If the AgentCore service is unreachable. |
flush
async
¶
Remove every entry this backend manages (namespaced by prefix).
Raises:
| Type | Description |
|---|---|
BackendConnectionError
|
If the AgentCore service is unreachable. |
ping
async
¶
Return True when the AgentCore service is reachable.
Returns:
| Type | Description |
|---|---|
bool
|
|