Initial commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
from classquiz.config import redis, settings
|
||||
from classquiz.db.models import User
|
||||
import ormar
|
||||
import uuid
|
||||
from typing import Union
|
||||
from json import loads, dumps
|
||||
|
||||
|
||||
async def cache_account(criteria: str, content: str) -> Union[User, None]:
|
||||
async def insert_into_redis(usermodel: User, key: str):
|
||||
await redis.set(key, usermodel.json(), ex=settings.cache_expiry)
|
||||
|
||||
if criteria == "email":
|
||||
try:
|
||||
res = await User.objects.get(email=content)
|
||||
except ormar.exceptions.NoMatch:
|
||||
return None
|
||||
await insert_into_redis(res, content)
|
||||
return res
|
||||
elif criteria == "username":
|
||||
try:
|
||||
res = await User.objects.get(username=content)
|
||||
except ormar.exceptions.NoMatch:
|
||||
return None
|
||||
await insert_into_redis(res, content)
|
||||
return res
|
||||
elif criteria == "id":
|
||||
|
||||
try:
|
||||
res = await User.objects.get(id=uuid.UUID(content))
|
||||
except ormar.exceptions.NoMatch:
|
||||
return None
|
||||
await insert_into_redis(res, content)
|
||||
return res
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
async def get_from_redis(key: str) -> Union[None, User]:
|
||||
user = await redis.get(key)
|
||||
if user is None:
|
||||
return None
|
||||
else:
|
||||
return User.parse_obj(loads(user))
|
||||
|
||||
|
||||
async def get_cache(criteria: str, content: str) -> User:
|
||||
cache = await get_from_redis(content)
|
||||
if cache is not None:
|
||||
return cache
|
||||
else:
|
||||
return await cache_account(criteria, content)
|
||||
Reference in New Issue
Block a user