use lscache library for local storage caching

its nice cause it has eviction strategies. updated the hunk summaries to
use this, with a 24 hour time to live, so that we dont leak
This commit is contained in:
Kiril Videlov 2023-07-01 22:39:57 +03:00 committed by Kiril Videlov
parent 2f7b6a5ded
commit 635486f815
3 changed files with 20 additions and 19 deletions

View File

@ -49,6 +49,7 @@
"@types/crypto-js": "^4.1.1",
"@types/diff": "^5.0.2",
"@types/diff-match-patch": "^1.0.32",
"@types/lscache": "^1.3.1",
"@types/marked": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.59.7",
@ -66,6 +67,7 @@
"histoire": "^0.16.1",
"inter-ui": "^3.19.3",
"leven": "^4.0.0",
"lscache": "^1.3.2",
"marked": "^5.0.2",
"mm-jsr": "^3.0.2",
"nanoevents": "^7.0.1",

View File

@ -97,6 +97,9 @@ devDependencies:
'@types/diff-match-patch':
specifier: ^1.0.32
version: 1.0.32
'@types/lscache':
specifier: ^1.3.1
version: 1.3.1
'@types/marked':
specifier: ^5.0.0
version: 5.0.0
@ -148,6 +151,9 @@ devDependencies:
leven:
specifier: ^4.0.0
version: 4.0.0
lscache:
specifier: ^1.3.2
version: 1.3.2
marked:
specifier: ^5.0.2
version: 5.0.2
@ -1346,6 +1352,10 @@ packages:
resolution: {integrity: sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==}
dev: true
/@types/lscache@1.3.1:
resolution: {integrity: sha512-psd3P6tJ9Ydte9AWWOOmRq9wq3unFCqQymp0iIgath87KBQCg31r9sxbv9ruDzOFICxZMazeEj5t+9C/7RPxrA==}
dev: true
/@types/markdown-it@12.2.3:
resolution: {integrity: sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==}
dependencies:
@ -3537,6 +3547,10 @@ packages:
resolution: {integrity: sha512-Pn9cox5CsMYngeDbmChANltQl+5pi6XmTrraMSzhPmMBbmgcxmqWry0U3PGapCU1yB4/LqCcom7qhHZiF/jGfQ==}
dev: true
/lscache@1.3.2:
resolution: {integrity: sha512-CBZT/pDcaK3I3XGwDLaszDe8hj0pCgbuxd3W79gvHApBSdKVXvR9fillbp6eLvp7dLgtaWm3a1mvmhAqn9uCXQ==}
dev: true
/magic-string@0.26.7:
resolution: {integrity: sha512-hX9XH3ziStPoPhJxLq1syWuZMxbDvGNbVchfrdCtanC7D13888bMFow61x8axrx+GfHLtVeAx2kxL7tTGRl+Ow==}
engines: {node: '>=12'}

View File

@ -1,31 +1,16 @@
import { get, writable } from '@square/svelte-store';
import { browser } from '$app/environment';
import { CloudApi } from '$lib/api';
import lscache from 'lscache';
const cloud = CloudApi();
export const store = writable<Map<string, string>>(
(browser && new Map(Object.entries(JSON.parse(localStorage.getItem('hunkSummaries') || '{}')))) ||
new Map<string, string>()
);
store.subscribe((val: Map<string, string>) => {
if (browser) {
localStorage.setItem('hunkSummaries', JSON.stringify(Object.fromEntries(val)));
}
});
export async function summarizeHunk(diff: string): Promise<string> {
const cache = get(store);
const diffHash = hash(diff);
if (cache.has(diffHash)) {
return cache.get(diffHash) as string;
if (lscache.get(diffHash)) {
return lscache.get(diffHash);
}
const rsp = await cloud.summarize.hunk({ hunk: diff });
cache.set(diffHash, rsp.message);
store.set(cache);
lscache.set(diffHash, rsp.message, 1440); // 1 day ttl
return rsp.message;
}