vomebook commited on
Commit
31eab5a
·
1 Parent(s): 3f3e666

Upload 2 files

Browse files
Files changed (2) hide show
  1. static/app.js +29 -0
  2. static/sw.js +1 -1
static/app.js CHANGED
@@ -30,6 +30,7 @@ const STATE = {
30
  suppressAutoLoad: false,
31
  searchController: null,
32
  searchSequence: 0,
 
33
  searchUrlWithoutPreview: null,
34
  highlightQuery: null,
35
  highlightPatterns: [],
@@ -39,6 +40,7 @@ const STATE = {
39
 
40
  const DOM = {};
41
  const HISTORY_KEY = "vomebook_search_history";
 
42
 
43
  function themeIcon(isDark) {
44
  const shape = isDark
@@ -193,6 +195,29 @@ const API = {
193
  },
194
  };
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  function cacheDom() {
197
  DOM.headerTitle = $("#header-title");
198
  DOM.headerLogo = $("#header-logo");
@@ -518,7 +543,11 @@ async function pathSearch(signal) {
518
  search_paths: STATE.searchPaths,
519
  fulltext: STATE.fulltext,
520
  };
 
 
 
521
  const data = await API.search(body, STATE.source, signal);
 
522
  return { data, page };
523
  }
524
 
 
30
  suppressAutoLoad: false,
31
  searchController: null,
32
  searchSequence: 0,
33
+ searchCache: new Map(),
34
  searchUrlWithoutPreview: null,
35
  highlightQuery: null,
36
  highlightPatterns: [],
 
40
 
41
  const DOM = {};
42
  const HISTORY_KEY = "vomebook_search_history";
43
+ const SEARCH_CACHE_LIMIT = 60;
44
 
45
  function themeIcon(isDark) {
46
  const shape = isDark
 
195
  },
196
  };
197
 
198
+ function stableStringify(value) {
199
+ if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
200
+ if (value && typeof value === "object") {
201
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
202
+ }
203
+ return JSON.stringify(value);
204
+ }
205
+
206
+ function getCachedSearch(key) {
207
+ if (!STATE.searchCache.has(key)) return null;
208
+ const value = STATE.searchCache.get(key);
209
+ STATE.searchCache.delete(key);
210
+ STATE.searchCache.set(key, value);
211
+ return value;
212
+ }
213
+
214
+ function setCachedSearch(key, value) {
215
+ STATE.searchCache.set(key, value);
216
+ while (STATE.searchCache.size > SEARCH_CACHE_LIMIT) {
217
+ STATE.searchCache.delete(STATE.searchCache.keys().next().value);
218
+ }
219
+ }
220
+
221
  function cacheDom() {
222
  DOM.headerTitle = $("#header-title");
223
  DOM.headerLogo = $("#header-logo");
 
543
  search_paths: STATE.searchPaths,
544
  fulltext: STATE.fulltext,
545
  };
546
+ const cacheKey = `${STATE.source || ""}\n${stableStringify(body)}`;
547
+ const cached = getCachedSearch(cacheKey);
548
+ if (cached) return { data: cached, page, cached: true };
549
  const data = await API.search(body, STATE.source, signal);
550
+ setCachedSearch(cacheKey, data);
551
  return { data, page };
552
  }
553
 
static/sw.js CHANGED
@@ -1,4 +1,4 @@
1
- const CACHE_NAME = "vomebook-search-v2";
2
 
3
  const PRECACHE_URLS = [
4
  "/",
 
1
+ const CACHE_NAME = "vomebook-search-v1.0.0-beta";
2
 
3
  const PRECACHE_URLS = [
4
  "/",