Peach: Local-first video metadata scraper and media manager.
fix(web): persist taste dashboard cache
longmeidaofc6494345a1c
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.pyindex 5392102cf..3512f5664 100644--- a/tests/test_web_ui.py+++ b/tests/test_web_ui.py@@ -1470,22 +1470,27 @@ class WebUiSourceTests(unittest.TestCase):if "sans-serif" in line or "monospace" in line:self.assertIn("YaHei", line, f"app.css:{i} 字体栈缺 CJK 兜底:{line.strip()[:90]}")- def test_taste_dashboard_is_cached_within_the_session(self):- """口味仪表按窗口做会话内缓存,进页不再每次等「正在分析…」。+ def test_taste_dashboard_is_persisted_and_refreshed_without_blocking(self):+ """口味仪表跨页面刷新复用旧结果,过期更新也不阻塞打开页面。- 口味数据来自浏览器历史聚合,会话里几乎不变;每次切页都打接口只剩- 等待。命中缓存直接渲染;换窗口、导入、移除数据源和显式「读取」- 才打接口,并把返回写回缓存。请求带序号:慢响应返回时人已经换到- 别的窗口或页面,旧数据不能覆盖新渲染。+ 口味数据来自浏览器历史聚合,24 小时内无需重读。过期时仍先显示+ 持久缓存,再后台更新;请求带序号,慢响应不能覆盖别的窗口或页面。"""- self.assertPageContains("let tasteWindow='all',tasteCache=new Map(),tasteRequest=0;")- self.assertPageContains("const cached=tasteCache.get(tasteWindow);")+ self.assertPageContains("const TASTE_CACHE_KEY='peach-taste-dashboard-v2',TASTE_CACHE_FRESH_MS=24*60*60*1000;")+ self.assertPageContains("let tasteWindow='all',tasteCache=readTasteCache(),tasteRequest=0;")+ self.assertPageContains("localStorage.getItem(TASTE_CACHE_KEY)")+ self.assertPageContains("localStorage.setItem(TASTE_CACHE_KEY,JSON.stringify(Object.fromEntries(tasteCache)))")+ self.assertPageContains("const cachedEntry=tasteCache.get(tasteWindow),cached=cachedEntry?.dashboard;")+ self.assertPageContains("const cacheFresh=cached&&Date.now()-cachedEntry.at<TASTE_CACHE_FRESH_MS;")self.assertPageContains("if(cached)renderTaste(cached);")- self.assertPageContains("tasteCache.set(tasteWindow,data);")- self.assertPageContains("if(request===tasteRequest&&surfaceCurrent(surface))renderTaste(data)")+ self.assertPageContains("if(!cacheFresh)")+ self.assertPageContains("void api('/api/taste?window='+requestedWindow).then(data=>")+ self.assertPageContains("tasteCacheSet(requestedWindow,data);")+ self.assertPageContains("if(request===tasteRequest&&tasteWindow===requestedWindow&&surfaceCurrent(surface))renderTaste(data)")+ self.assertPageContains("if(!cached&&request===tasteRequest&&surfaceCurrent(surface))")# 三个写路径都更新缓存,别让缓存变陈旧。- self.assertPageContains("tasteCache.set(tasteWindow,result.dashboard);renderTaste(result.dashboard)")- self.assertPageContains("tasteWindow='all';tasteCache.set('all',payload.dashboard);renderTaste(payload.dashboard)")+ self.assertPageContains("tasteCacheSet(tasteWindow,result.dashboard);renderTaste(result.dashboard)")+ self.assertPageContains("tasteWindow='all';tasteCacheSet('all',payload.dashboard);renderTaste(payload.dashboard)")def test_no_page_grows_its_own_back_control(self):"""索引页原本有个返回按钮,现在顶栏入口本身就是返回路径。diff --git a/web/app.js b/web/app.jsindex 049fb2ba6..e1807525b 100644--- a/web/app.js+++ b/web/app.js@@ -1172,10 +1172,24 @@ async function openResourceSync(push=true){await openStats(false,true);}-/* 口味仪表按窗口缓存:数据来自浏览器历史的聚合,会话里几乎不变,- 每次进页都打接口只会让人盯着「正在分析…」。命中缓存直接渲染;- 换窗口、导入、移除数据源、显式「读取」才真正打接口并更新缓存。 */-let tasteWindow='all',tasteCache=new Map(),tasteRequest=0;+/* 口味仪表按窗口持久缓存:刷新页面也先显示上次结果。24 小时内不重读;+ 过期后仍先显示旧结果,再在后台更新。导入、移除数据源和显式「读取」+ 会立即写回缓存。缓存只含页面已经展示的聚合结果,不含原始历史。 */+const TASTE_CACHE_KEY='peach-taste-dashboard-v2',TASTE_CACHE_FRESH_MS=24*60*60*1000;+const TASTE_CACHE_WINDOWS=new Set(['all','365d','90d']);+function readTasteCache(){+ try{+ const stored=JSON.parse(localStorage.getItem(TASTE_CACHE_KEY)||'{}');+ return new Map(Object.entries(stored).filter(([window,entry])=>+ TASTE_CACHE_WINDOWS.has(window)&&entry&&Number.isFinite(Number(entry.at))&&+ entry.dashboard&&typeof entry.dashboard==='object'))+ }catch(_error){return new Map()}+}+let tasteWindow='all',tasteCache=readTasteCache(),tasteRequest=0;+function tasteCacheSet(window,dashboard){+ tasteCache.set(window,{at:Date.now(),dashboard});+ try{localStorage.setItem(TASTE_CACHE_KEY,JSON.stringify(Object.fromEntries(tasteCache)))}catch(_error){}+}const tasteDate=value=>value?new Date(value).toLocaleDateString('zh-CN'):'—';const tasteHours=seconds=>seconds>=3600?(seconds/3600).toFixed(1)+' 小时':Math.round(seconds/60)+' 分钟';const tasteRanking=(title,rows,kind,empty='暂无足够证据',panel='tastepanel-half',visual='')=>`<section class="tastepanel ${panel}"><h3>${title}</h3>@@ -1248,20 +1262,20 @@ function renderTaste(d){button.innerHTML=`${spinnerHtml('正在读取')}<span>读取中…</span>`;stateEl.innerHTML=loadingDotsHtml('正在读取 Peach 所在主机的浏览器…');try{const result=await api('/api/taste/refresh',{method:'POST',body:JSON.stringify({window:tasteWindow})});- tasteCache.set(tasteWindow,result.dashboard);renderTaste(result.dashboard)}+ tasteCacheSet(tasteWindow,result.dashboard);renderTaste(result.dashboard)}catch(error){stateEl.textContent=error.message||'读取失败';button.disabled=false;button.removeAttribute('aria-busy');button.innerHTML=oldButton}};root.querySelector('[data-taste-import]').onclick=()=>file.click();file.onchange=async()=>{const selected=file.files[0];if(!selected)return;stateEl.textContent=`正在导入 ${selected.name}…`;try{const response=await fetch('/api/taste/import',{method:'POST',headers:{'Content-Type':'application/octet-stream','X-Peach-Filename':encodeURIComponent(selected.name)},body:selected});const payload=await response.json().catch(()=>null);if(!response.ok)throw new Error(payload?.error||`导入失败(${response.status})`);- tasteWindow='all';tasteCache.set('all',payload.dashboard);renderTaste(payload.dashboard)}catch(error){stateEl.textContent=error.message||'导入失败'}};+ tasteWindow='all';tasteCacheSet('all',payload.dashboard);renderTaste(payload.dashboard)}catch(error){stateEl.textContent=error.message||'导入失败'}};root.querySelectorAll('[data-taste-kind]').forEach(button=>button.onclick=()=>openTasteSignal(button.dataset.tasteKind,button.dataset.tasteName));root.querySelectorAll('[data-taste-remove]').forEach(button=>button.onclick=async()=>{if(!confirm('从口味分析中移除这个数据源?原始导出文件会保留。'))return;button.disabled=true;stateEl.textContent='正在移除…';try{const result=await api('/api/taste/source',{method:'POST',body:JSON.stringify({operation:'remove',source_key:button.dataset.tasteRemove,window:tasteWindow})});- tasteCache.set(tasteWindow,result.dashboard);renderTaste(result.dashboard)}+ tasteCacheSet(tasteWindow,result.dashboard);renderTaste(result.dashboard)}catch(error){stateEl.textContent=error.message||'移除失败';button.disabled=false}});}async function openTaste(push=true){@@ -1272,21 +1286,24 @@ async function openTaste(push=true){const surface=claimSurface('/taste');$('#stats').hidden=false;$('#index').hidden=true;$('#grid').innerHTML='';$('#count').textContent='';$('#loadSentinel').hidden=true;$('#shortsSec').hidden=true;buildManageBar();- const cached=tasteCache.get(tasteWindow);+ const cachedEntry=tasteCache.get(tasteWindow),cached=cachedEntry?.dashboard;+ const cacheFresh=cached&&Date.now()-cachedEntry.at<TASTE_CACHE_FRESH_MS;if(cached)renderTaste(cached);else $('#stats').innerHTML=`<div class="tastepage"><div class="skeletonpanel"><span class="skeleton" style="width:38%"></span><span class="skeleton" style="width:100%"></span><span class="skeleton" style="width:100%"></span><span class="skeleton" style="width:72%"></span></div></div>`;- if(!cached){+ if(!cacheFresh){const request=++tasteRequest;- try{const data=await api('/api/taste?window='+tasteWindow);- tasteCache.set(tasteWindow,data);- if(request===tasteRequest&&surfaceCurrent(surface))renderTaste(data)}- catch(error){- if(request===tasteRequest&&surfaceCurrent(surface))$('#stats').innerHTML=- `<div class="tastepage">${noteHtml(error.message||'分析未取得',{variant:'error',label:'分析未取得'})}</div>`}+ const requestedWindow=tasteWindow;+ void api('/api/taste?window='+requestedWindow).then(data=>{+ tasteCacheSet(requestedWindow,data);+ if(request===tasteRequest&&tasteWindow===requestedWindow&&surfaceCurrent(surface))renderTaste(data)+ }).catch(error=>{+ if(!cached&&request===tasteRequest&&surfaceCurrent(surface))$('#stats').innerHTML=+ `<div class="tastepage">${noteHtml(error.message||'分析未取得',{variant:'error',label:'分析未取得'})}</div>`+ })}window.scrollTo({top:0,behavior:'smooth'});}