Repository navigation
Expand file tree
/
Copy pathcache.js
More file actions
96 lines (84 loc) · 2.5 KB
/
Copy pathcache.js
File metadata and controls
96 lines (84 loc) · 2.5 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
const METADATA_TTL = 30 * 24 * 60 * 60 * 1000; // 30 days
const METADATA_MAX_SIZE = 10000;
const metadataCacheKey = (machineIdentifier, ratingKey) => JSON.stringify([
'media',
String(machineIdentifier),
String(ratingKey),
]);
const roomMetadataCacheKey = (room) => JSON.stringify(['room', String(room)]);
const roomPosterMetadataCacheKey = (room, revision) => JSON.stringify([
'room-poster',
String(room),
String(revision),
]);
function resolveRoomPosterMetadata({
room,
revision,
getCurrentMetadata,
getSnapshotMetadata,
}) {
const snapshot = getSnapshotMetadata(roomPosterMetadataCacheKey(room, revision));
if (snapshot) return snapshot;
const current = getCurrentMetadata(roomMetadataCacheKey(room));
return current?.roomPreviewRevision === String(revision) ? current : null;
}
function createCache({
nowFn = Date.now,
ttlMs = METADATA_TTL,
maxSize = METADATA_MAX_SIZE,
} = {}) {
if (!Number.isSafeInteger(ttlMs) || ttlMs <= 0) {
throw new TypeError('Cache TTL must be a positive safe integer');
}
if (!Number.isSafeInteger(maxSize) || maxSize <= 0) {
throw new TypeError('Cache size must be a positive safe integer');
}
const metadataCache = new Map();
function setMetadata(key, value) {
// Lazy TTL eviction: purge expired entries on write to bound memory growth
const now = nowFn();
for (const [k, v] of metadataCache) {
if (now - v.cachedAt > ttlMs) {
metadataCache.delete(k);
} else {
break; // oldest entries are first; stop at first non-expired
}
}
// If updating an existing key, delete first so re-set moves it to the end (LRU order)
if (metadataCache.has(key)) {
metadataCache.delete(key);
} else if (metadataCache.size >= maxSize) {
// Evict oldest entry if at capacity
const oldestKey = metadataCache.keys().next().value;
metadataCache.delete(oldestKey);
}
metadataCache.set(key, { ...value, cachedAt: now });
}
function getMetadata(key) {
const entry = metadataCache.get(key);
if (!entry) return null;
if (nowFn() - entry.cachedAt > ttlMs) {
metadataCache.delete(key);
return null;
}
return entry;
}
function deleteMetadata(key) {
return metadataCache.delete(key);
}
return {
setMetadata,
getMetadata,
deleteMetadata,
metadataCache,
};
}
module.exports = {
createCache,
metadataCacheKey,
roomMetadataCacheKey,
roomPosterMetadataCacheKey,
resolveRoomPosterMetadata,
METADATA_TTL,
METADATA_MAX_SIZE,
};