mirror of
https://github.com/elk-zone/elk
synced 2024-12-25 20:17:59 +09:00
29 lines
525 B
TypeScript
29 lines
525 B
TypeScript
|
export function useCacheStorage<T>(
|
||
|
key: string,
|
||
|
getter: () => T | Promise<T>,
|
||
|
TTL = 1000 * 60 * 60 * 12, // 12 hours
|
||
|
) {
|
||
|
const storage = useLocalStorage(key, {
|
||
|
time: 0,
|
||
|
value: null as T | null,
|
||
|
})
|
||
|
|
||
|
if (storage.value.time + TTL < Date.now()) {
|
||
|
Promise.resolve(getter()).then((v) => {
|
||
|
storage.value = {
|
||
|
time: Date.now(),
|
||
|
value: v,
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return computed({
|
||
|
get() {
|
||
|
return storage.value.value
|
||
|
},
|
||
|
set(v) {
|
||
|
storage.value.value = v
|
||
|
},
|
||
|
})
|
||
|
}
|