-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy paththumbnail-cache.ts
More file actions
194 lines (170 loc) · 4.94 KB
/
Copy paththumbnail-cache.ts
File metadata and controls
194 lines (170 loc) · 4.94 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* LRU cache for thumbnail ImageBitmaps
* Provides GPU-ready bitmaps for canvas rendering with 50MB memory limit
* Throttled loading to prevent overwhelming the browser
*/
export interface CacheEntry {
bitmap: ImageBitmap;
width: number;
height: number;
size: number; // decoded bytes (w * h * 4)
}
interface QueuedLoad {
volumeUuid: string;
file: File;
resolve: (entry: CacheEntry) => void;
reject: (error: Error) => void;
}
class ThumbnailCache {
private cache = new Map<string, CacheEntry>(); // volume_uuid -> entry
private pending = new Map<string, Promise<CacheEntry>>(); // coalesce concurrent requests
private totalBytes = 0;
private readonly maxBytes = 50 * 1024 * 1024; // 50MB
// Throttling
private queue: QueuedLoad[] = [];
private activeLoads = 0;
private readonly maxConcurrent = 6; // Limit concurrent createImageBitmap calls
/**
* Get or load a thumbnail bitmap
* Coalesces concurrent requests for the same thumbnail
*/
async get(volumeUuid: string, file: File): Promise<CacheEntry> {
// Check cache first
const existing = this.cache.get(volumeUuid);
if (existing) {
this.touch(volumeUuid);
return existing;
}
// Join existing load if in progress
const pendingLoad = this.pending.get(volumeUuid);
if (pendingLoad) {
return pendingLoad;
}
// Create promise and queue the load
const loadPromise = new Promise<CacheEntry>((resolve, reject) => {
this.queue.push({ volumeUuid, file, resolve, reject });
});
this.pending.set(volumeUuid, loadPromise);
this.processQueue();
try {
return await loadPromise;
} finally {
this.pending.delete(volumeUuid);
}
}
/**
* Process queued loads with concurrency limit
* Uses FILO (stack) ordering - most recent requests processed first
* This prioritizes currently visible items over items that scrolled past
*/
private processQueue(): void {
while (this.queue.length > 0 && this.activeLoads < this.maxConcurrent) {
const item = this.queue.pop()!; // FILO: pop from end (most recent)
this.activeLoads++;
this.load(item.volumeUuid, item.file)
.then(item.resolve)
.catch(item.reject)
.finally(() => {
this.activeLoads--;
this.processQueue();
});
}
}
/**
* Check if a thumbnail is cached (without loading)
*/
has(volumeUuid: string): boolean {
return this.cache.has(volumeUuid);
}
/**
* Get cached entry synchronously (returns undefined if not cached)
*/
getSync(volumeUuid: string): CacheEntry | undefined {
const entry = this.cache.get(volumeUuid);
if (entry) {
this.touch(volumeUuid);
}
return entry;
}
/**
* Invalidate a specific cache entry (e.g., when cover is edited)
*/
invalidate(volumeUuid: string): void {
const entry = this.cache.get(volumeUuid);
if (entry) {
entry.bitmap.close();
this.totalBytes -= entry.size;
this.cache.delete(volumeUuid);
}
// Also remove from pending if in progress
this.pending.delete(volumeUuid);
// Remove from queue if waiting
this.queue = this.queue.filter((item) => item.volumeUuid !== volumeUuid);
}
/**
* Clear entire cache
*/
clear(): void {
for (const entry of this.cache.values()) {
entry.bitmap.close();
}
this.cache.clear();
this.totalBytes = 0;
}
/**
* Get cache statistics
*/
getStats(): { count: number; totalBytes: number; maxBytes: number; utilization: string } {
return {
count: this.cache.size,
totalBytes: this.totalBytes,
maxBytes: this.maxBytes,
utilization: ((this.totalBytes / this.maxBytes) * 100).toFixed(1) + '%'
};
}
/**
* Load and decode a thumbnail
*/
private async load(volumeUuid: string, file: File): Promise<CacheEntry> {
const bitmap = await createImageBitmap(file);
const size = bitmap.width * bitmap.height * 4; // RGBA
// Evict if needed before adding
while (this.totalBytes + size > this.maxBytes && this.cache.size > 0) {
this.evictLRU();
}
const entry: CacheEntry = {
bitmap,
width: bitmap.width,
height: bitmap.height,
size
};
this.cache.set(volumeUuid, entry);
this.totalBytes += size;
return entry;
}
/**
* Move entry to end of Map (most recently used)
*/
private touch(volumeUuid: string): void {
const entry = this.cache.get(volumeUuid);
if (entry) {
this.cache.delete(volumeUuid);
this.cache.set(volumeUuid, entry);
}
}
/**
* Evict least recently used entry (first in Map)
*/
private evictLRU(): void {
const firstKey = this.cache.keys().next().value;
if (firstKey) {
const entry = this.cache.get(firstKey);
if (entry) {
entry.bitmap.close(); // Release GPU memory
this.totalBytes -= entry.size;
this.cache.delete(firstKey);
}
}
}
}
export const thumbnailCache = new ThumbnailCache();