debug:推荐接口返回redis值
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped

This commit is contained in:
litianxiang
2026-01-12 13:01:26 +08:00
parent 74406f9be4
commit 18979af604

View File

@@ -178,47 +178,23 @@ async def recommend(
@router.get("/redis/user_pref")
async def get_all_user_preferences():
"""
获取所有以 user_pref 为前缀的 Redis key 信息,按 account_id 分组
获取所有以 user_pref 为前缀的 Redis key 信息
"""
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 分组
# 直接返回所有 key 和原始 value
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'
}
# 读取对应的值
value = Redis.read(key)
if value:
result[key] = value
return result