Initial commit MKD fixes
This commit is contained in:
17
hooks/useCachedFetch.ts
Executable file
17
hooks/useCachedFetch.ts
Executable file
@@ -0,0 +1,17 @@
|
||||
/** Читает кеш из localStorage */
|
||||
export function readCache<T>(key: string, fallback: T): T {
|
||||
try {
|
||||
const raw = localStorage.getItem(key);
|
||||
if (!raw) return fallback;
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
/** Сохраняет в кеш */
|
||||
export function saveCache(key: string, data: unknown): void {
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify(data));
|
||||
} catch (_) {}
|
||||
}
|
||||
Reference in New Issue
Block a user