| 1 | const FALLBACK_INITIAL_COUNT = 5_000 |
| 2 | |
| 3 | export 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 | |
| 28 | export async function starCount(currentCount) { |
| 29 | const ttl = 3600 // 1 hour |