-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexplainCache.ts
More file actions
122 lines (99 loc) · 2.67 KB
/
explainCache.ts
File metadata and controls
122 lines (99 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { ExplanationResponse } from './prompts';
export const CACHE_KEY = 'ai-word-explanations';
const CACHE_VERSION = 1;
interface CacheEntry {
explanation: ExplanationResponse;
timestamp: number;
}
interface CacheData {
version: number;
entries: Record<string, CacheEntry>;
}
function normalizeKey(term: string): string {
return term.toLowerCase().trim();
}
function isBrowser(): boolean {
return typeof window !== 'undefined' && typeof localStorage !== 'undefined';
}
function isValidCacheData(data: unknown): data is CacheData {
return (
typeof data === 'object' &&
data !== null &&
'version' in data &&
typeof (data as CacheData).version === 'number' &&
'entries' in data &&
typeof (data as CacheData).entries === 'object' &&
(data as CacheData).entries !== null
);
}
function getDefaultCache(): CacheData {
return { version: CACHE_VERSION, entries: {} };
}
function readCache(): CacheData {
if (!isBrowser()) {
return getDefaultCache();
}
try {
const raw = localStorage.getItem(CACHE_KEY);
if (!raw) {
return getDefaultCache();
}
const data: unknown = JSON.parse(raw);
if (!isValidCacheData(data)) {
localStorage.removeItem(CACHE_KEY);
return getDefaultCache();
}
if (data.version !== CACHE_VERSION) {
localStorage.removeItem(CACHE_KEY);
return getDefaultCache();
}
return data;
} catch {
localStorage.removeItem(CACHE_KEY);
return getDefaultCache();
}
}
function writeCache(data: CacheData): void {
if (!isBrowser()) return;
try {
localStorage.setItem(CACHE_KEY, JSON.stringify(data));
} catch (error) {
console.warn('Failed to write to explanation cache:', error);
}
}
export function getCachedExplanation(term: string): ExplanationResponse | null {
const key = normalizeKey(term);
const cache = readCache();
const entry = cache.entries[key];
if (!entry) return null;
return entry.explanation;
}
export function setCachedExplanation(
term: string,
explanation: ExplanationResponse
): void {
const key = normalizeKey(term);
const cache = readCache();
cache.entries[key] = {
explanation,
timestamp: Date.now(),
};
writeCache(cache);
}
export function clearCache(): void {
if (!isBrowser()) return;
localStorage.removeItem(CACHE_KEY);
}
export function getCacheSize(): number {
const cache = readCache();
return Object.keys(cache.entries).length;
}
export function getCachedTerms(): string[] {
const cache = readCache();
return Object.keys(cache.entries);
}
export function hasCache(term: string): boolean {
const key = normalizeKey(term);
const cache = readCache();
return key in cache.entries;
}