-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-store.py
More file actions
33 lines (28 loc) · 1.03 KB
/
session-store.py
File metadata and controls
33 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import redis
import json
from datetime import datetime, timedelta
class SessionStore:
def __init__(self, redis_host='localhost', redis_port=6379):
self.redis = redis.Redis(host=redis_host, port=redis_port)
def create_session(self, container_id: str, user_id: str,
token: str, ttl_hours: int = 4):
"""Store session info"""
session_data = {
'container_id': container_id,
'user_id': user_id,
'token': token,
'created_at': str(datetime.now())
}
# Store with expiration
self.redis.setex(
f"session:{container_id}",
timedelta(hours=ttl_hours),
json.dumps(session_data)
)
def get_session(self, container_id: str):
"""Retrieve session"""
data = self.redis.get(f"session:{container_id}")
return json.loads(data) if data else None
def delete_session(self, container_id: str):
"""Delete session"""
self.redis.delete(f"session:{container_id}")