vickysrm commited on
Commit
f8ee24f
·
1 Parent(s): 6fe7f14

fix: Session store works without Redis

Browse files
Files changed (1) hide show
  1. services/session_store.py +36 -17
services/session_store.py CHANGED
@@ -1,24 +1,35 @@
1
- import redis
2
  import json
3
  import os
4
  from typing import Optional
5
 
6
- _client: Optional[redis.Redis] = None
 
 
 
7
 
8
- def get_redis() -> redis.Redis:
9
- global _client
10
- if _client is None:
11
- _client = redis.from_url(
12
- os.getenv("REDIS_URL", "redis://localhost:6379"),
13
- decode_responses=True,
14
- )
15
- return _client
 
 
 
 
 
 
 
 
 
 
16
 
17
  SESSION_TTL = 60 * 60 * 4 # 4 hours
18
 
19
 
20
  def create_session(session_id: str) -> dict:
21
- r = get_redis()
22
  session = {
23
  "id": session_id,
24
  "transcript": [],
@@ -29,19 +40,26 @@ def create_session(session_id: str) -> dict:
29
  "vectors": [],
30
  "active": True,
31
  }
32
- r.setex(f"session:{session_id}", SESSION_TTL, json.dumps(session))
 
 
 
33
  return session
34
 
35
 
36
  def get_session(session_id: str) -> Optional[dict]:
37
- r = get_redis()
38
- data = r.get(f"session:{session_id}")
39
- return json.loads(data) if data else None
 
 
40
 
41
 
42
  def update_session(session_id: str, session: dict):
43
- r = get_redis()
44
- r.setex(f"session:{session_id}", SESSION_TTL, json.dumps(session))
 
 
45
 
46
 
47
  def append_transcript(session_id: str, entry: dict):
@@ -73,3 +91,4 @@ def close_session(session_id: str):
73
  if session:
74
  session["active"] = False
75
  update_session(session_id, session)
 
 
 
1
  import json
2
  import os
3
  from typing import Optional
4
 
5
+ # Try to use Redis if available, otherwise fall back to in-memory store
6
+ _client = None
7
+ _in_memory_store: dict = {}
8
+ _use_redis = False
9
 
10
+ def _init_redis():
11
+ global _client, _use_redis
12
+ redis_url = os.getenv("REDIS_URL", "")
13
+ if not redis_url:
14
+ print("No REDIS_URL set — using in-memory session store.")
15
+ return
16
+ try:
17
+ import redis
18
+ _client = redis.from_url(redis_url, decode_responses=True, socket_connect_timeout=3)
19
+ _client.ping()
20
+ _use_redis = True
21
+ print(f"Connected to Redis at {redis_url[:30]}...")
22
+ except Exception as e:
23
+ print(f"Redis unavailable ({e}) — falling back to in-memory store.")
24
+ _client = None
25
+ _use_redis = False
26
+
27
+ _init_redis()
28
 
29
  SESSION_TTL = 60 * 60 * 4 # 4 hours
30
 
31
 
32
  def create_session(session_id: str) -> dict:
 
33
  session = {
34
  "id": session_id,
35
  "transcript": [],
 
40
  "vectors": [],
41
  "active": True,
42
  }
43
+ if _use_redis:
44
+ _client.setex(f"session:{session_id}", SESSION_TTL, json.dumps(session))
45
+ else:
46
+ _in_memory_store[session_id] = session
47
  return session
48
 
49
 
50
  def get_session(session_id: str) -> Optional[dict]:
51
+ if _use_redis:
52
+ data = _client.get(f"session:{session_id}")
53
+ return json.loads(data) if data else None
54
+ else:
55
+ return _in_memory_store.get(session_id)
56
 
57
 
58
  def update_session(session_id: str, session: dict):
59
+ if _use_redis:
60
+ _client.setex(f"session:{session_id}", SESSION_TTL, json.dumps(session))
61
+ else:
62
+ _in_memory_store[session_id] = session
63
 
64
 
65
  def append_transcript(session_id: str, entry: dict):
 
91
  if session:
92
  session["active"] = False
93
  update_session(session_id, session)
94
+