Skip to content

Commit

Permalink
Add session update and TTL update functions to session store
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Lee committed Mar 6, 2024
1 parent c10a115 commit ca54251
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
42 changes: 41 additions & 1 deletion app/auth/session_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def add(user_id: str, session_id: str, value, ttl: int) -> bool:
Returns:
bool: True if the session was successfully added to the store, False otherwise.
"""
print(f"Adding session '{user_id}:{session_id}' to session store, TTL: {ttl} seconds")
return await cache.add(key=session_id, value=value, namespace=user_id, ttl=ttl)

async def exists(user_id: str, session_id: str) -> bool:
Expand Down Expand Up @@ -97,6 +98,42 @@ async def retrieve_by_userid(
# Return the dictionary of sessions
return sessions

async def update(user_id: str, session_id: str, value, ttl: Optional[int] = None) -> bool:
"""
Update a user session in the cache store.
Args:
user_id (str): The ID of the user.
session_id (str): The ID of the user session.
value (Any): The value object.
ttl (Optional[int]): The time-to-live (TTL) for the user session in seconds. If not
provided, the TTL will not be updated.
Returns:
bool: True if the session was successfully updated, False otherwise.
"""
if ttl:
print(f"Updating session '{user_id}:{session_id}' in session store, TTL: {ttl} seconds")
return await cache.set(key=session_id, value=value, namespace=user_id, ttl=ttl)

print(f"Updating session '{user_id}:{session_id}' in session store")
return await cache.set(key=session_id, value=value, namespace=user_id)

async def update_ttl(user_id: str, session_id: str, ttl: int):
"""
Update the time-to-live (TTL) of a user session in the cache store.
Args:
user_id (str): The ID of the user.
session_id (str): The ID of the user session.
ttl (int): number of seconds for expiration. If 0, ttl is disabled
Returns:
bool: True if the session TTL was successfully updated, False otherwise.
"""
print(f"Updating TTL of session '{user_id}:{session_id}' in session store")
return await cache.expire(key=session_id, namespace=user_id, ttl=ttl)

async def update_last_activity(token_payload: dict) -> bool:
"""
Update the last activity timestamp of the user session.
Expand Down Expand Up @@ -148,13 +185,16 @@ async def remove(user_id: str, session_id: Optional[str] = None) -> int:
result = 0

if session_id:
print(f"Removing session '{user_id}:{session_id}' from session store")

# delete the specific user session of the user
result = await cache.delete(key=session_id, namespace=user_id)

else:
# delete all sessions of the user
sessions = await retrieve_by_userid(user_id=user_id, sort=False)
for session in sessions[user_id]:
print(f"Removing session '{user_id}:{session.session_id}' from session store")
result += await cache.delete(key=session.session_id, namespace=user_id)

return result
return result
14 changes: 9 additions & 5 deletions tests/auth/test_session_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,22 @@ async def test_expiration():
# Create a new session with a TTL of 3 seconds
await create_session_in_cache(user_id, test_session_id, ttl=3)

# Call the function with a user_id and session_id
# check is the session exists
result = await exists(user_id, test_session_id)

# Assert that the result is True, indicating the session exists
assert result is True

# Wait 2s
await asyncio.sleep(2)
# check is the session exists
result = await exists(user_id, test_session_id)
# Assert that the result is True, indicating the session still exists
assert result is True

# Wait for the session to expire
await asyncio.sleep(5)

# Call the function with a user_id and session_id
# check is the session exists
result = await exists(user_id, test_session_id)

# Assert that the result is False, indicating the session has expired
assert result is False

Expand Down

0 comments on commit ca54251

Please sign in to comment.