-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-handler.js
More file actions
33 lines (29 loc) · 800 Bytes
/
cache-handler.js
File metadata and controls
33 lines (29 loc) · 800 Bytes
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
const cache = new Map();
module.exports = class CacheHandler {
constructor(options) {
this.options = options;
}
async get(key) {
// This could be stored anywhere, like durable storage
return cache.get(key);
}
async set(key, data, ctx) {
// This could be stored anywhere, like durable storage
cache.set(key, {
value: data,
lastModified: Date.now(),
tags: ctx.tags,
});
}
async revalidateTag(tags) {
// tags is either a string or an array of strings
tags = [tags].flat();
// Iterate over all entries in the cache
for (let [key, value] of cache) {
// If the value's tags include the specified tag, delete this entry
if (value.tags.some((tag) => tags.include(tag))) {
cache.delete(key);
}
}
}
};