mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-14 00:03:42 +08:00
59 lines
1.2 KiB
TypeScript
59 lines
1.2 KiB
TypeScript
import { useRef } from 'react';
|
|
|
|
interface SearchCache {
|
|
query: string;
|
|
results: any[];
|
|
availableSources: any[];
|
|
timestamp: number;
|
|
}
|
|
|
|
const CACHE_KEY = 'kvideo_search_cache';
|
|
const CACHE_DURATION = 10 * 60 * 1000; // 10 minutes
|
|
|
|
export function useSearchCache() {
|
|
const saveToCache = (
|
|
query: string,
|
|
results: any[],
|
|
sources: any[]
|
|
) => {
|
|
const cache: SearchCache = {
|
|
query,
|
|
results,
|
|
availableSources: sources,
|
|
timestamp: Date.now(),
|
|
};
|
|
|
|
try {
|
|
localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
|
|
|
|
} catch (error) {
|
|
console.error('Failed to save cache:', error);
|
|
}
|
|
};
|
|
|
|
const loadFromCache = (): SearchCache | null => {
|
|
try {
|
|
const cached = localStorage.getItem(CACHE_KEY);
|
|
if (!cached) return null;
|
|
|
|
const cache: SearchCache = JSON.parse(cached);
|
|
|
|
// Check if cache is still valid
|
|
if (Date.now() - cache.timestamp > CACHE_DURATION) {
|
|
localStorage.removeItem(CACHE_KEY);
|
|
return null;
|
|
}
|
|
|
|
return cache;
|
|
} catch (error) {
|
|
console.error('Failed to load cache:', error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
return {
|
|
saveToCache,
|
|
loadFromCache,
|
|
};
|
|
}
|