Create storage/cache.js
Browse files- storage/cache.js +22 -0
storage/cache.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const cache = {};
|
| 2 |
+
|
| 3 |
+
function set(key, value, ttl = 5000) {
|
| 4 |
+
cache[key] = {
|
| 5 |
+
value,
|
| 6 |
+
expiry: Date.now() + ttl
|
| 7 |
+
};
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
function get(key) {
|
| 11 |
+
const item = cache[key];
|
| 12 |
+
if (!item) return null;
|
| 13 |
+
|
| 14 |
+
if (Date.now() > item.expiry) {
|
| 15 |
+
delete cache[key];
|
| 16 |
+
return null;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
return item.value;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
module.exports = { set, get };
|