Skip to content

Commit 82f55fa

Browse files
Added expiration support for Redis (#3316)
## Summary Added expiration support for RedisMemory and RedisStorage classes. This allows automatic cleanup of stale data in Redis if users choose to enable expiration, improving memory management and long-term storage efficiency. ## Type of change - [ ] Bug fix - [ ] New feature - [ ] Breaking change - [X] Improvement - [ ] Model update - [ ] Other: --- ## Checklist - [ ] Code complies with style guidelines - [ ] Ran format/validation scripts (`./scripts/format.sh` and `./scripts/validate.sh`) - [ ] Self-review completed - [ ] Documentation updated (comments, docstrings) - [ ] Examples and guides: Relevant cookbook examples have been included or updated (if applicable) - [ ] Tested in clean environment - [ ] Tests added/updated (if applicable) --- ## Additional Notes Add any important context (deployment instructions, screenshots, security considerations, etc.) --------- Co-authored-by: Dirk Brand <dirkbrnd@gmail.com>
1 parent bf9f768 commit 82f55fa

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

libs/agno/agno/memory/v2/db/redis.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(
2020
port: int = 6379,
2121
db: int = 0,
2222
password: Optional[str] = None,
23+
expire: Optional[int] = None,
2324
):
2425
"""
2526
Initialize Redis memory store.
@@ -30,8 +31,10 @@ def __init__(
3031
port (int): Redis port number
3132
db (int): Redis database number
3233
password (Optional[str]): Redis password if authentication is required
34+
expire (Optional[int]): TTL (time to live) in seconds for Redis keys. None means no expiration.
3335
"""
3436
self.prefix = prefix
37+
self.expire = expire
3538
self.redis_client = Redis(
3639
host=host,
3740
port=port,
@@ -45,6 +48,7 @@ def __dict__(self) -> Dict[str, Any]:
4548
return {
4649
"name": "RedisMemoryDb",
4750
"prefix": self.prefix,
51+
"expire": self.expire,
4852
}
4953

5054
def _get_key(self, memory_id: str) -> str:
@@ -128,7 +132,11 @@ def upsert_memory(self, memory: MemoryRow) -> Optional[MemoryRow]:
128132

129133
# Save to Redis
130134
key = self._get_key(memory.id) # type: ignore
131-
self.redis_client.set(key, json.dumps(memory_data))
135+
if self.expire is not None:
136+
self.redis_client.set(key, json.dumps(memory_data), ex=self.expire)
137+
else:
138+
self.redis_client.set(key, json.dumps(memory_data))
139+
132140
return memory
133141

134142
except Exception as e:

libs/agno/agno/storage/redis.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
password: Optional[str] = None,
2727
mode: Optional[Literal["agent", "team", "workflow"]] = "agent",
2828
ssl: Optional[bool] = False,
29+
expire: Optional[int] = None,
2930
):
3031
"""
3132
Initialize Redis storage for sessions.
@@ -38,9 +39,11 @@ def __init__(
3839
password (Optional[str]): Redis password if authentication is required
3940
mode (Optional[Literal["agent", "team", "workflow"]]): Storage mode
4041
ssl (Optional[bool]): Whether to use SSL for Redis connection
42+
expire (Optional[int]): TTL (time to live) in seconds for Redis keys. None means no expiration.
4143
"""
4244
super().__init__(mode)
4345
self.prefix = prefix
46+
self.expire = expire
4447
self.redis_client = Redis(
4548
host=host,
4649
port=port,
@@ -267,7 +270,10 @@ def upsert(self, session: Session) -> Optional[Session]:
267270
data["created_at"] = data["updated_at"]
268271

269272
key = self._get_key(session.session_id)
270-
self.redis_client.set(key, self.serialize(data))
273+
if self.expire is not None:
274+
self.redis_client.set(key, self.serialize(data), ex=self.expire)
275+
else:
276+
self.redis_client.set(key, self.serialize(data))
271277
return session
272278
except Exception as e:
273279
logger.error(f"Error upserting session: {e}")

0 commit comments

Comments
 (0)