ruff fix
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""缓存调度器模块"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .cache_scheduler import start_cache_scheduler, stop_cache_scheduler
|
||||
|
||||
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
|
||||
from app.config import settings
|
||||
from app.dependencies.database import get_db, get_redis
|
||||
from app.dependencies.database import get_redis
|
||||
from app.dependencies.fetcher import get_fetcher
|
||||
from app.log import logger
|
||||
from app.scheduler.user_cache_scheduler import (
|
||||
@@ -44,10 +44,10 @@ class CacheScheduler:
|
||||
"""运行调度器主循环"""
|
||||
# 启动时立即执行一次预热
|
||||
await self._warmup_cache()
|
||||
|
||||
|
||||
# 启动时执行一次排行榜缓存刷新
|
||||
await self._refresh_ranking_cache()
|
||||
|
||||
|
||||
# 启动时执行一次用户缓存预热
|
||||
await self._warmup_user_cache()
|
||||
|
||||
@@ -55,14 +55,16 @@ class CacheScheduler:
|
||||
ranking_cache_counter = 0
|
||||
user_cache_counter = 0
|
||||
user_cleanup_counter = 0
|
||||
|
||||
|
||||
# 从配置文件获取间隔设置
|
||||
check_interval = 5 * 60 # 5分钟检查间隔
|
||||
beatmap_cache_interval = 30 * 60 # 30分钟beatmap缓存间隔
|
||||
ranking_cache_interval = settings.ranking_cache_refresh_interval_minutes * 60 # 从配置读取
|
||||
ranking_cache_interval = (
|
||||
settings.ranking_cache_refresh_interval_minutes * 60
|
||||
) # 从配置读取
|
||||
user_cache_interval = 15 * 60 # 15分钟用户缓存预加载间隔
|
||||
user_cleanup_interval = 60 * 60 # 60分钟用户缓存清理间隔
|
||||
|
||||
|
||||
beatmap_cache_cycles = beatmap_cache_interval // check_interval
|
||||
ranking_cache_cycles = ranking_cache_interval // check_interval
|
||||
user_cache_cycles = user_cache_interval // check_interval
|
||||
@@ -90,12 +92,12 @@ class CacheScheduler:
|
||||
if ranking_cache_counter >= ranking_cache_cycles:
|
||||
await self._refresh_ranking_cache()
|
||||
ranking_cache_counter = 0
|
||||
|
||||
|
||||
# 用户缓存预加载
|
||||
if user_cache_counter >= user_cache_cycles:
|
||||
await self._preload_user_cache()
|
||||
user_cache_counter = 0
|
||||
|
||||
|
||||
# 用户缓存清理
|
||||
if user_cleanup_counter >= user_cleanup_cycles:
|
||||
await self._cleanup_user_cache()
|
||||
@@ -129,15 +131,14 @@ class CacheScheduler:
|
||||
logger.info("Starting ranking cache refresh...")
|
||||
|
||||
redis = get_redis()
|
||||
|
||||
|
||||
# 导入排行榜缓存服务
|
||||
# 使用独立的数据库会话
|
||||
from app.dependencies.database import with_db
|
||||
from app.service.ranking_cache_service import (
|
||||
get_ranking_cache_service,
|
||||
schedule_ranking_refresh_task,
|
||||
)
|
||||
|
||||
# 使用独立的数据库会话
|
||||
from app.dependencies.database import with_db
|
||||
async with with_db() as session:
|
||||
await schedule_ranking_refresh_task(session, redis)
|
||||
|
||||
@@ -171,6 +172,7 @@ class CacheScheduler:
|
||||
# Beatmap缓存调度器(保持向后兼容)
|
||||
class BeatmapsetCacheScheduler(CacheScheduler):
|
||||
"""谱面集缓存调度器 - 为了向后兼容"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
"""
|
||||
用户缓存预热任务调度器
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from app.config import settings
|
||||
from app.database import User
|
||||
from app.database.score import Score
|
||||
from app.dependencies.database import get_db, get_redis
|
||||
from app.dependencies.database import get_redis
|
||||
from app.log import logger
|
||||
from app.service.user_cache_service import get_user_cache_service
|
||||
|
||||
@@ -25,16 +25,17 @@ async def schedule_user_cache_preload_task():
|
||||
|
||||
try:
|
||||
logger.info("Starting user cache preload task...")
|
||||
|
||||
|
||||
redis = get_redis()
|
||||
cache_service = get_user_cache_service(redis)
|
||||
|
||||
|
||||
# 使用独立的数据库会话
|
||||
from app.dependencies.database import with_db
|
||||
|
||||
async with with_db() as session:
|
||||
# 获取最近24小时内活跃的用户(提交过成绩的用户)
|
||||
recent_time = datetime.now(UTC) - timedelta(hours=24)
|
||||
|
||||
|
||||
active_user_ids = (
|
||||
await session.exec(
|
||||
select(Score.user_id, func.count().label("score_count"))
|
||||
@@ -44,7 +45,7 @@ async def schedule_user_cache_preload_task():
|
||||
.limit(settings.user_cache_max_preload_users) # 使用配置中的限制
|
||||
)
|
||||
).all()
|
||||
|
||||
|
||||
if active_user_ids:
|
||||
user_ids = [row[0] for row in active_user_ids]
|
||||
await cache_service.preload_user_cache(session, user_ids)
|
||||
@@ -62,17 +63,18 @@ async def schedule_user_cache_warmup_task():
|
||||
"""定时用户缓存预热任务 - 预加载排行榜前100用户"""
|
||||
try:
|
||||
logger.info("Starting user cache warmup task...")
|
||||
|
||||
|
||||
redis = get_redis()
|
||||
cache_service = get_user_cache_service(redis)
|
||||
|
||||
|
||||
# 使用独立的数据库会话
|
||||
from app.dependencies.database import with_db
|
||||
|
||||
async with with_db() as session:
|
||||
# 获取全球排行榜前100的用户
|
||||
from app.database.statistics import UserStatistics
|
||||
from app.models.score import GameMode
|
||||
|
||||
|
||||
for mode in GameMode:
|
||||
try:
|
||||
top_users = (
|
||||
@@ -83,15 +85,15 @@ async def schedule_user_cache_warmup_task():
|
||||
.limit(100)
|
||||
)
|
||||
).all()
|
||||
|
||||
|
||||
if top_users:
|
||||
user_ids = list(top_users)
|
||||
await cache_service.preload_user_cache(session, user_ids)
|
||||
logger.info(f"Warmed cache for top 100 users in {mode}")
|
||||
|
||||
|
||||
# 避免过载,稍微延迟
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to warm cache for {mode}: {e}")
|
||||
continue
|
||||
@@ -106,13 +108,13 @@ async def schedule_user_cache_cleanup_task():
|
||||
"""定时用户缓存清理任务"""
|
||||
try:
|
||||
logger.info("Starting user cache cleanup task...")
|
||||
|
||||
|
||||
redis = get_redis()
|
||||
|
||||
|
||||
# 清理过期的用户缓存(Redis会自动处理TTL,这里主要记录统计信息)
|
||||
cache_service = get_user_cache_service(redis)
|
||||
stats = await cache_service.get_cache_stats()
|
||||
|
||||
|
||||
logger.info(f"User cache stats: {stats}")
|
||||
logger.info("User cache cleanup task completed successfully")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user