inizio modifiche backend x TTL sessioni
This commit is contained in:
@@ -61,3 +61,13 @@ async def delete_session_endpoint(
|
||||
redis_service.delete_session(user_id, session_id)
|
||||
return {"status": "deleted"}
|
||||
|
||||
# estensione TTL sessioni
|
||||
@router.post("/sessions/{session_id}/extend_ttl", response_model=dict)
|
||||
async def extend_ttl_endpoint(
|
||||
user_id: str = Query(..., description="User ID"),
|
||||
session_id: str = Path(..., description="Session ID")
|
||||
extra_seconds: int = Body(..., embed=True)
|
||||
):
|
||||
redis_service.extend_session_ttl(user_id, session_id, extra_seconds)
|
||||
return {"status": "ttl_extended"}
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@ r = redis.Redis(
|
||||
decode_responses=True
|
||||
)
|
||||
|
||||
# setup defaults
|
||||
DEFAULT_TTL_SECONDS = 30 * 24 * 3600 # 30 gg
|
||||
|
||||
# -------------------------
|
||||
# Chat message operations
|
||||
# -------------------------
|
||||
@@ -28,11 +31,11 @@ def save_chat(user_id: str, session_id: str, message: dict):
|
||||
"timestamp": datetime.utcnow().isoformat() + "Z"
|
||||
}
|
||||
r.rpush(key, json.dumps(enriched))
|
||||
r.expire(key, DEFAULT_TTL_SECONDS) # TTL anche per la history
|
||||
|
||||
# Update metadata after adding
|
||||
_update_session_stats(user_id, session_id)
|
||||
|
||||
|
||||
def get_chat(user_id: str, session_id: str, limit: Optional[int] = None):
|
||||
"""
|
||||
Retrieve messages for a user/session, sorted by timestamp.
|
||||
@@ -68,7 +71,6 @@ def create_session(user_id: str, first_message: str) -> dict:
|
||||
session_id = str(uuid.uuid4())
|
||||
created_at = datetime.utcnow().isoformat() + "Z"
|
||||
session_name = (first_message.strip()[:50] or "New Chat")
|
||||
|
||||
meta = {
|
||||
"session_id": session_id,
|
||||
"created_at": created_at,
|
||||
@@ -76,16 +78,15 @@ def create_session(user_id: str, first_message: str) -> dict:
|
||||
"message_count": 0,
|
||||
"history_size_bytes": 0
|
||||
}
|
||||
meta_key = f"chatSession:{user_id}:{session_id}"
|
||||
index_key = f"chatSessionsIndex:{user_id}"
|
||||
|
||||
# Save metadata
|
||||
r.set(f"chatSession:{user_id}:{session_id}", json.dumps(meta))
|
||||
|
||||
# Add to per-user index (sorted set with score = timestamp)
|
||||
r.zadd(f"chatSessionsIndex:{user_id}", {session_id: datetime.utcnow().timestamp()})
|
||||
r.set(meta_key, json.dumps(meta), ex=DEFAULT_TTL_SECONDS)
|
||||
r.zadd(index_key, {session_id: datetime.utcnow().timestamp()})
|
||||
r.expire(index_key, DEFAULT_TTL_SECONDS) # TTL anche per l’indice utente
|
||||
|
||||
return meta
|
||||
|
||||
|
||||
def get_sessions(user_id: str) -> List[dict]:
|
||||
"""
|
||||
Return all sessions for a user, newest first.
|
||||
@@ -124,6 +125,19 @@ def delete_session(user_id: str, session_id: str):
|
||||
r.delete(f"chatHistory:{user_id}:{session_id}")
|
||||
r.zrem(f"chatSessionsIndex:{user_id}", session_id)
|
||||
|
||||
def extend_session_ttl(user_id: str, session_id: str, extra_seconds: int):
|
||||
meta_key = f"chatSession:{user_id}:{session_id}"
|
||||
history_key = f"chatHistory:{user_id}:{session_id}"
|
||||
index_key = f"chatSessionsIndex:{user_id}"
|
||||
|
||||
for key in [meta_key, history_key, index_key]:
|
||||
if r.exists(key):
|
||||
current_ttl = r.ttl(key)
|
||||
if current_ttl > 0:
|
||||
r.expire(key, current_ttl + extra_seconds)
|
||||
else:
|
||||
r.expire(key, extra_seconds)
|
||||
|
||||
|
||||
# -------------------------
|
||||
# Internal helpers
|
||||
|
||||
Reference in New Issue
Block a user