-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,12 @@ | ||
import { createStore, get, set, del, clear, } from 'idb-keyval'; | ||
import type { Cache } from 'swr'; | ||
|
||
const store = typeof window !== 'undefined' | ||
? createStore('bloom-db', 'messages') | ||
: null; | ||
|
||
// In-memory cache for synchronous operations | ||
const memoryCache = new Map<string, any>(); | ||
|
||
export const clearSWRCache = async () => { | ||
memoryCache.clear(); | ||
if (store && typeof window !== 'undefined') { | ||
try { | ||
await clear(store); | ||
} catch (error) { | ||
console.error('Error clearing cache:', error); | ||
} | ||
} | ||
}; | ||
export const cacheProvider = (cache: Readonly<Cache<any>>) => ({ | ||
get: (key: string) => memoryCache.get(key), | ||
set: (key: string, value: any) => memoryCache.set(key, value), | ||
delete: (key: string) => memoryCache.delete(key), | ||
keys: () => Array.from(memoryCache.keys())[Symbol.iterator]() | ||
}); | ||
|
||
export const cacheProvider = (cache: Readonly<Cache<any>>) => { | ||
return { | ||
get: (key: string) => { | ||
return memoryCache.get(key); | ||
}, | ||
set: (key: string, value: any) => { | ||
memoryCache.set(key, value); | ||
// Persist to IndexedDB in background | ||
if (store) { | ||
set(key, value, store).catch(console.error); | ||
} | ||
}, | ||
delete: (key: string) => { | ||
memoryCache.delete(key); | ||
// Delete from IndexedDB in background | ||
if (store) { | ||
del(key, store).catch(console.error); | ||
} | ||
}, | ||
keys: () => { | ||
return memoryCache.keys(); | ||
} | ||
} | ||
} | ||
export const clearSWRCache = () => memoryCache.clear(); |