MCPcopy
hub / github.com/electric-sql/pglite / localStorageCache

Function localStorageCache

docs/components/starCount.ts:3–26  ·  view source on GitHub ↗
(
  key: string,
  ttl: number,
  valueCb: () => unknown,
)

Source from the content-addressed store, hash-verified

1const FALLBACK_INITIAL_COUNT = 5_000
2
3export async function localStorageCache(
4 key: string,
5 ttl: number,
6 valueCb: () => unknown,
7) {
8 const now = new Date().getTime()
9 const cachedItem = localStorage.getItem(key)
10
11 if (cachedItem) {
12 const cachedData = JSON.parse(cachedItem)
13 if (now < cachedData.expiry) {
14 return cachedData.value
15 }
16 }
17
18 const value = await valueCb()
19 const expiry = now + ttl * 1000
20 const dataToCache = {
21 value: value,
22 expiry: expiry,
23 }
24 localStorage.setItem(key, JSON.stringify(dataToCache))
25 return value
26}
27
28export async function starCount(currentCount) {
29 const ttl = 3600 // 1 hour

Callers 1

starCountFunction · 0.85

Calls 1

parseMethod · 0.80

Tested by

no test coverage detected