Skip to content

armgit / peach

Public Imported from GitHub
0

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

perf(web): 开机预热每页都读的聚合:形状名单、作品分组快照、统计与推荐池

longmeidaod95aaa2aa3ff
diff --git a/src/peach/api.py b/src/peach/api.py
index 269bf1218..7d05470fc 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -200,8 +200,11 @@ def _restore_task_center(contract) -> None:
#: 开机在后台先算一遍的聚合:口味、复核、垃圾复核、关注的全部条目与筛选项各要几秒到
#: 十几秒,都按账本版本号缓存(`WebContract.cached_until_changed`),算好一次就一直用到
-#: 账本或文件真的变了。
-WARM_AGGREGATES = (("/api/taste", {"window": "all"}), ("/api/review", {}), ("/api/ads", {}),
+#: 账本或文件真的变了。排在前面的是每页都碰到的几份:形状名单、作品分组快照(目录、
+#: 索引、筛选项都建在它上面)、统计与搜索推荐池,重启后第一个打开页面的人不再等它们一起冷算。
+WARM_AGGREGATES = (("/api/entity/shapes", {}), ("/api/items", {"limit": "60"}), ("/api/stats", {}),
+ ("/api/search/recommendations", {}),
+ ("/api/taste", {"window": "all"}), ("/api/review", {}), ("/api/ads", {}),
("/api/follow", {}))
diff --git a/tests/test_ledger_revision.py b/tests/test_ledger_revision.py
index 5f7f17f40..8a7ddf534 100644
--- a/tests/test_ledger_revision.py
+++ b/tests/test_ledger_revision.py
@@ -188,6 +188,18 @@ class LedgerRevisionTests(unittest.TestCase):
self.assertEqual(self.contract.cached_until_changed("catalog", self.compute, tables=("asset",)), 3)
self.assertEqual(self.contract.stale_refreshing, set())
+ def test_startup_warms_the_aggregates_every_page_reads(self):
+ """重启后第一个打开页面的人不等形状名单、作品分组快照与推荐池一起冷算:开机就先算好。"""
+ import asyncio
+ from peach import api
+ insert_asset(self.db_path, 1)
+ # 只看每页都碰到的那几份;口味、复核、关注那几份另有后台线程,不在这条测试里起。
+ pages = [entry for entry in api.WARM_AGGREGATES if entry[0] not in {"/api/taste", "/api/review", "/api/ads", "/api/follow"}]
+ with mock.patch.object(api, "WARM_AGGREGATES", tuple(pages)):
+ asyncio.run(api._warm_ledger_aggregates(True, self.contract, asyncio.Event()))
+ for key in ("entity-shapes", "catalog-group-rows", "search-recommendations:"):
+ self.assertIn(key, self.contract.cache)
+
def test_a_commit_during_a_versioned_computation_keeps_the_result(self):
def derive():
self.calls += 1