export const createLocalStorageCache = () => { const cache = new WeakMap(); const updateLocalStorage = () => { const cacheData = JSON.stringify(Array.from(cache.entries())); localStorage.setItem('cache', cacheData); }; const initializeCacheFromLocalStorage = () => { const cacheData = localStorage.getItem('cache'); if (cacheData) { const entries = JSON.parse(cacheData); entries.forEach(([key, value]) => { cache.set(key, value); }); } }; const setCache = (key, value) => { cache.set(key, value); updateLocalStorage(); }; const getCache = (key) => cache.get(key); const removeCache = (key) => { cache.delete(key); updateLocalStorage(); }; const clearCache = () => { cache.clear(); updateLocalStorage(); }; const setCacheWithExpiry = (key, value, ttl) => { const now = new Date(); const item = { value, expiry: now.getTime() + ttl, }; setCache(key, item); }; const getCacheWithExpiry = (key) => { const item = getCache(key); if (!item) { return null; } const now = new Date(); if (now.getTime() > item.expiry) { removeCache(key); return null; } return item.value; }; initializeCacheFromLocalStorage(); return { setCache, getCache, removeCache, clearCache, setCacheWithExpiry, getCacheWithExpiry, }; };