-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
from aioredis import Redis | ||
from app.models.user_model import User | ||
from app.schemas.common_schema import TokenType | ||
|
||
|
||
async def add_token_to_redis( | ||
redis_client: Redis, | ||
user: User, | ||
token: str, | ||
token_type: TokenType, | ||
expire_time: Optional[int] = None, | ||
): | ||
token_key = f"user:{user.id}:{token_type}" | ||
valid_tokens = await get_valid_tokens(redis_client,user.id,token_type) | ||
await redis_client.sadd(token_key, token) | ||
if not valid_tokens: | ||
await redis_client.expire(token_key, expire_time) | ||
|
||
|
||
async def get_valid_tokens(redis_client: Redis, user_id: UUID, token_type: TokenType): | ||
token_key = f"user:{user_id}:{token_type}" | ||
valid_tokens = await redis_client.smembers(token_key) | ||
return valid_tokens | ||
|
||
|
||
async def delete_tokens(redis_client: Redis, user: User, token_type: TokenType): | ||
token_key = f"user:{user.id}:{token_type}" | ||
valid_tokens = await redis_client.smembers(token_key) | ||
if valid_tokens is not None: | ||
await redis_client.delete(token_key) |