-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
37 lines (32 loc) · 831 Bytes
/
cache.js
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
// cache.js
class NCache {
constructor() {
this.cache = new Map();
}
// Set a key-value pair in the cache with TTL (time-to-live) in seconds
set(key, value, ttl = 0) {
const expirationTime = ttl > 0 ? Date.now() + ttl * 1000 : 0;
this.cache.set(key, { value, expirationTime });
return true;
}
// Get the value of a key from the cache
get(key) {
const item = this.cache.get(key);
if (item && (!item.expirationTime || item.expirationTime > Date.now())) {
return item.value;
}
this.cache.delete(key); // Clear expired item from the cache
return null;
}
// Remove a key from the cache
delete(key) {
this.cache.delete(key);
return true;
}
// Clear all items from the cache
clear() {
this.cache.clear();
return true;
}
}
module.exports = NCache;