feat: 修复接口认证时查询用户信息没有缓存用户的职位信息的问题

This commit is contained in:
martsforever
2025-09-29 13:26:39 +08:00
parent 10f606bd15
commit 498ee970f5
4 changed files with 19 additions and 24 deletions
+6 -4
View File
@@ -1,3 +1,4 @@
import json
import uuid
from contextlib import asynccontextmanager
from typing import Annotated, AsyncContextManager
@@ -68,16 +69,17 @@ RedisClientDep = Annotated[redis.Redis, Depends(get_redis_client)]
# 从redis中获取key的缓存,如果没有值则执行默认值获取函数,并保存到redis中
async def get_redis_cache(key: str, default_value_getter):
async with redis_utils.get_redis_connection() as redis_client:
mapping = await redis_client.hgetall(key)
exists = bool(mapping)
# default_value_getter返回的字段可能嵌套多层对象,这里改成用json字符串缓存
json_string = await redis_client.get(key)
exists = bool(json_string)
if exists:
return mapping
return json.loads(json_string)
else:
value = await default_value_getter()
print("value", value)
if value is not None:
new_value = {k: v for k, v in value.items() if v is not None}
await redis_client.hset(key, mapping=new_value)
await redis_client.set(key, json.dumps(new_value, ensure_ascii=False))
return value