Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(web): 搜索推荐池不随提交通知清掉,后台任务运行时也按 5 分钟复用

longmeidao40902a0f0ec4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e692b039d..01640d604 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@ Removed / Fixed / Security。
- **馆藏**:入库、采集和 JAV 新作、发现页认女优与厂牌更快。
- **界面**:后台任务写入资料时和重启后,目录、索引与统计页打开不再等重算。
- **界面**:任务中心打开从约 1.7 秒缩短到约 0.1 秒。
+- **界面**:后台任务运行时,打开页面不再每次都重新计算搜索推荐。
- **关注**:新作列表不再显示「NOW PRINTING」占位图,还没出封面的未发售新作等官方出图后再列出。
## [0.55.1] - 2026-11-05
diff --git a/src/peach/web_search.py b/src/peach/web_search.py
index 708dcd1bd..22a6cdff7 100644
--- a/src/peach/web_search.py
+++ b/src/peach/web_search.py
@@ -59,8 +59,8 @@ def q_search_recommendations(contract, args) -> dict:
自己的取数抢服务进程。推荐只是随手一翻的入口,晚几分钟换池无妨。
"""
library = str(args.get("library") or "")
- pool = contract.cached(f"search-recommendations:{library}", lambda: _bundle(contract, library),
- ttl=RECOMMENDATION_TTL)
+ pool = contract.cached_for(f"search-recommendations:{library}", lambda: _bundle(contract, library),
+ ttl=RECOMMENDATION_TTL)
return _shuffled(pool)
diff --git a/src/peach/web_state.py b/src/peach/web_state.py
index 2e2c202af..1ef84e769 100644
--- a/src/peach/web_state.py
+++ b/src/peach/web_state.py
@@ -302,6 +302,15 @@ class WebContract:
self.aggregate_locks.pop(evicted, None)
return value
+ def cached_for(self, key, fn, *, ttl: float):
+ """只按时间过期、不随提交通知清掉的缓存,给允许旧上几分钟的结果用(搜索推荐池)。
+
+ `cached` 的键每次提交都被 `cache_bust` 清掉;后台任务每几秒提交一次时,这类结果等于没有缓存。
+ """
+ with self.cache_lock:
+ self.revision_cache_keys.add(key)
+ return self.cached(key, fn, ttl=ttl)
+
def cached_lru(self, key, fn, *, maxsize: int = 192, ttl: float = CACHE_TTL):
"""带 TTL 的 LRU 读缓存,给键空间不封闭的场景用(`/api/related` 按资产取键)。
diff --git a/tests/test_rm_web.py b/tests/test_rm_web.py
index 12d94d935..08c8c382c 100644
--- a/tests/test_rm_web.py
+++ b/tests/test_rm_web.py
@@ -175,8 +175,10 @@ class WebDataTests(unittest.TestCase):
self.assertTrue(all(len(word) > 1 for word in bundle["words"]))
def test_search_recommendations_reuse_one_pool_across_page_opens(self):
- """每开一页都预取推荐:池子复用,不在每次预取时重算;换的只是池里的顺序。"""
+ """每开一页都预取推荐:池子复用,不在每次预取时重算;换的只是池里的顺序。
+ 后台任务每几秒提交一次,提交通知也不清掉这一池。"""
first = rm_web.dispatch_api_get(self.contract, "/api/search/recommendations", {})
+ self.contract.cache_bust()
with mock.patch.object(web_search, "q_items", side_effect=AssertionError("推荐池被重算了")):
again = rm_web.dispatch_api_get(self.contract, "/api/search/recommendations", {})
self.assertEqual({row["id"] for row in again["items"]["items"]},