From 61268563619539e1803ad5cf6bab9a52165262e2 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 26 Jan 2026 10:41:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(cache):=20=E5=AE=9E=E7=8E=B0=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在文件上传成功后将文件加入缓存系统 - 添加 add_to_cache 方法支持本地文件缓存 - 实现原子操作确保缓存写入安全 - 集成锁机制防止并发冲突 - 自动触发缓存清理策略 - 记录详细的缓存操作日志 --- handlers/base.py | 5 ++++ services/cache.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/handlers/base.py b/handlers/base.py index bdabfe2..e573c3b 100644 --- a/handlers/base.py +++ b/handlers/base.py @@ -464,6 +464,11 @@ class BaseHandler(TaskHandler, ABC): if result: file_size = os.path.getsize(file_path) logger.info(f"[task:{task_id}] Uploaded: {file_path} ({file_size} bytes)") + + # 将上传成功的文件加入缓存 + if access_url: + self.material_cache.add_to_cache(access_url, file_path) + return access_url else: logger.error(f"[task:{task_id}] Upload failed: {file_path}") diff --git a/services/cache.py b/services/cache.py index 89dc7ae..913705b 100644 --- a/services/cache.py +++ b/services/cache.py @@ -251,6 +251,66 @@ class MaterialCache: finally: self._release_lock(lock_path) + def add_to_cache(self, url: str, source_path: str) -> bool: + """ + 将本地文件添加到缓存 + + Args: + url: 对应的 URL(用于生成缓存键) + source_path: 本地文件路径 + + Returns: + 是否成功 + """ + if not self.enabled: + return False + + if not os.path.exists(source_path): + logger.warning(f"Source file not found for cache: {source_path}") + return False + + cache_key = _extract_cache_key(url) + lock_path = self._acquire_lock(cache_key) + if not lock_path: + logger.warning(f"Cache lock unavailable for adding: {url[:80]}...") + return False + + try: + cache_path = self.get_cache_path(url) + + # 先复制到临时文件 + temp_cache_path = os.path.join( + self.cache_dir, + f"{cache_key}.{uuid.uuid4().hex}.adding" + ) + + shutil.copy2(source_path, temp_cache_path) + + # 原子替换 + os.replace(temp_cache_path, cache_path) + + # 更新访问时间 + os.utime(cache_path, None) + + logger.info(f"Added to cache: {url[:80]}... <- {source_path}") + + # 检查清理 + if self.max_size_bytes > 0: + self._cleanup_if_needed() + + return True + + except Exception as e: + logger.error(f"Failed to add to cache: {e}") + if 'temp_cache_path' in locals() and os.path.exists(temp_cache_path): + try: + os.remove(temp_cache_path) + except Exception: + pass + return False + finally: + self._release_lock(lock_path) + def _cleanup_if_needed(self) -> None: """ 检查并清理缓存(LRU 策略)