@@ -62,27 +62,28 @@ def generate_voucher_from_user(self, user:User) -> Any:
6262 cs .SESSION_SENSITIVE : sensitive_data ,
6363 cs .SESSION_LAST_SEEN : int (time .time ())
6464 }
65-
66- sogo_cache () .hashset (f"user_session:{ user_session_id } " , user_session , cs .TTL_1D )
65+ cache = sogo_cache ()
66+ cache .hashset (f"user_session:{ user_session_id } " , user_session , cs .TTL_1D )
6767 # Index the session in the sorted set so that we can paginate / sort
6868 # active sessions by last-activity without scanning all keys.
69- sogo_cache () .zset_add (
69+ cache .zset_add (
7070 cs .ZSET_USER_SESSIONS_ACTIVITY ,
7171 f"user_session:{ user_session_id } " ,
7272 int (time .time ()),
7373 )
7474 # Index the session by uid score so that sessions can be sorted / filtered by uid.
75- sogo_cache () .zset_add (
75+ cache .zset_add (
7676 cs .ZSET_USER_SESSIONS_UID ,
7777 f"user_session:{ user_session_id } " ,
7878 string_to_sort_score (user .uid ),
7979 )
8080 # Index the session by domain score so that sessions can be sorted / filtered by domain.
81- sogo_cache () .zset_add (
81+ cache .zset_add (
8282 cs .ZSET_USER_SESSIONS_DOMAIN ,
8383 f"user_session:{ user_session_id } " ,
8484 string_to_sort_score (user .domain ),
8585 )
86+ cache .close ()
8687
8788 #Generate the voucher
8889 voucher_payload = user .get_voucher_payload ()
@@ -201,16 +202,17 @@ def _get_user_session_from_payload(self, payload:dict) -> User:
201202 except ValueError as e :
202203 raise RequestException ("Session key from Voucher is not valid" ) from e
203204
204- user_session_data = sogo_cache ().hashget (f"user_session:{ session_id } " )
205+ cache = sogo_cache ()
206+ user_session_data = cache .hashget (f"user_session:{ session_id } " )
205207 if not user_session_data :
206208 # The hash has expired but sorted-set entries may linger – clean them up.
207- sogo_cache () .zset_remove (
209+ cache .zset_remove (
208210 cs .ZSET_USER_SESSIONS_ACTIVITY , f"user_session:{ session_id } "
209211 )
210- sogo_cache () .zset_remove (
212+ cache .zset_remove (
211213 cs .ZSET_USER_SESSIONS_UID , f"user_session:{ session_id } "
212214 )
213- sogo_cache () .zset_remove (
215+ cache .zset_remove (
214216 cs .ZSET_USER_SESSIONS_DOMAIN , f"user_session:{ session_id } "
215217 )
216218 logger_auth .info ("User session for %s is expired or does not exist" , voucher_user_uid )
@@ -238,28 +240,29 @@ def _get_user_session_from_payload(self, payload:dict) -> User:
238240 # Update the last activity timestamp in both the hash and the sorted set
239241 new_last_seen = int (time .time ())
240242 logger .debug ("Updating last_activity for session %s: %s -> %s" , session_id , user_session_data .get (cs .SESSION_LAST_SEEN ), new_last_seen )
241- sogo_cache () .hashset (
243+ cache .hashset (
242244 f"user_session:{ session_id } " ,
243245 {cs .SESSION_LAST_SEEN : new_last_seen },
244246 ttl = 0
245247 )
246- sogo_cache () .zset_add (
248+ cache .zset_add (
247249 cs .ZSET_USER_SESSIONS_ACTIVITY ,
248250 f"user_session:{ session_id } " ,
249251 new_last_seen ,
250252 )
251253 # Keep the uid score index in sync.
252- sogo_cache () .zset_add (
254+ cache .zset_add (
253255 cs .ZSET_USER_SESSIONS_UID ,
254256 f"user_session:{ session_id } " ,
255257 string_to_sort_score (user .uid ),
256258 )
257259 # Keep the domain score index in sync.
258- sogo_cache () .zset_add (
260+ cache .zset_add (
259261 cs .ZSET_USER_SESSIONS_DOMAIN ,
260262 f"user_session:{ session_id } " ,
261263 string_to_sort_score (user .domain ),
262264 )
263265 logger .info ("From voucher get user: %s" , user )
266+ cache .close ()
264267
265268 return user
0 commit comments