mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-05 23:56:36 +08:00
backend 去ai化
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
"""
|
||||
用户偏好模块:兴趣关键词的增删查、基于关键词的个性化事件推荐
|
||||
"""
|
||||
import time
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
@@ -20,7 +17,6 @@ from app.services.matching_service import recommend_events_for_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# --- 轻量级接口缓存配置 ---
|
||||
_RECOMMEND_CACHE: Dict[str, Tuple[float, Any]] = {}
|
||||
CACHE_TTL_SECONDS = 60
|
||||
|
||||
@@ -29,7 +25,6 @@ def _invalidate_user_cache(user_id: int):
|
||||
keys_to_delete = [k for k in _RECOMMEND_CACHE.keys() if k.startswith(f"{user_id}:")]
|
||||
for k in keys_to_delete:
|
||||
_RECOMMEND_CACHE.pop(k, None)
|
||||
# ---------------------------
|
||||
|
||||
def _ensure_self_access(path_user_id: int, current_user: AppUser) -> None:
|
||||
"""校验路径 user_id 是否为当前登录用户本人。"""
|
||||
@@ -93,7 +88,7 @@ def create_user_preference(
|
||||
)
|
||||
|
||||
db.refresh(db_obj)
|
||||
_invalidate_user_cache(user_id) # 失效推荐缓存
|
||||
_invalidate_user_cache(user_id)
|
||||
return db_obj
|
||||
|
||||
|
||||
@@ -122,7 +117,7 @@ def delete_user_preference(
|
||||
|
||||
db.delete(preference)
|
||||
db.commit()
|
||||
_invalidate_user_cache(user_id) # 失效推荐缓存
|
||||
_invalidate_user_cache(user_id)
|
||||
return None
|
||||
|
||||
|
||||
@@ -143,7 +138,6 @@ def recommend_events(
|
||||
"""基于用户兴趣词推荐事件(精确匹配 + 语义匹配)。"""
|
||||
_ensure_self_access(user_id, current_user)
|
||||
|
||||
# 推荐结果缓存,避免频繁调用匹配服务
|
||||
cache_key = f"{user_id}:{min_hot}:{hours}:{limit}:{semantic_threshold}:{sort_by}"
|
||||
current_time = time.time()
|
||||
|
||||
@@ -151,7 +145,6 @@ def recommend_events(
|
||||
expire_time, cached_data = _RECOMMEND_CACHE[cache_key]
|
||||
if current_time < expire_time:
|
||||
return cached_data
|
||||
# -----------------------
|
||||
|
||||
matched = recommend_events_for_user(
|
||||
db,
|
||||
@@ -189,10 +182,8 @@ def recommend_events(
|
||||
|
||||
# 写入缓存,超过 2000 条时清空防止内存膨胀
|
||||
if len(_RECOMMEND_CACHE) > 2000:
|
||||
# 防止内存无限增长
|
||||
_RECOMMEND_CACHE.clear()
|
||||
|
||||
_RECOMMEND_CACHE[cache_key] = (current_time + CACHE_TTL_SECONDS, response)
|
||||
# ------------------
|
||||
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user