新增查看redis内容接口
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
This commit is contained in:
@@ -172,4 +172,56 @@ async def recommend(
|
||||
return [path]
|
||||
except Exception as e:
|
||||
logger.error("新版推荐接口失败 [user=%s, category=%s]: %s", user_id, category, e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/redis/user_pref")
|
||||
async def get_all_user_preferences():
|
||||
"""
|
||||
获取所有以 user_pref 为前缀的 Redis key 信息,按 account_id 分组
|
||||
"""
|
||||
try:
|
||||
from app.service.utils.redis_utils import Redis
|
||||
from app.service.recommendation_system.config import REDIS_KEY_USER_PREF_PREFIX
|
||||
import json
|
||||
|
||||
# 扫描所有匹配 user_pref:* 的 key
|
||||
pattern = f"{REDIS_KEY_USER_PREF_PREFIX}:*"
|
||||
keys = Redis.scan_keys(pattern)
|
||||
|
||||
# 按 account_id 分组
|
||||
result = {}
|
||||
for key in keys:
|
||||
# 解析 key 格式: user_pref:{account_id}:{category}
|
||||
parts = key.split(':')
|
||||
if len(parts) >= 3:
|
||||
account_id = parts[1]
|
||||
category = parts[2]
|
||||
|
||||
# 读取对应的值
|
||||
value = Redis.read(key)
|
||||
if value:
|
||||
try:
|
||||
vector = json.loads(value)
|
||||
if account_id not in result:
|
||||
result[account_id] = {}
|
||||
result[account_id][category] = {
|
||||
'key': key,
|
||||
'preference_vector': vector,
|
||||
'vector_length': len(vector)
|
||||
}
|
||||
except json.JSONDecodeError:
|
||||
# 如果 JSON 解析失败,保存原始值
|
||||
if account_id not in result:
|
||||
result[account_id] = {}
|
||||
result[account_id][category] = {
|
||||
'key': key,
|
||||
'raw_value': value,
|
||||
'error': 'JSON decode failed'
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error("获取用户偏好数据失败: %s", e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
Reference in New Issue
Block a user