File size: 501 Bytes
bd11153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { CACHE_TTL_MS } from "../config.js";

const store = new Map();
const MAX = 200;

export function getCache(key) {
  const hit = store.get(key);
  if (!hit) return null;
  if (Date.now() > hit.expires) {
    store.delete(key);
    return null;
  }
  return hit.value;
}

export function setCache(key, value, ttl = CACHE_TTL_MS) {
  if (store.size >= MAX) {
    const firstKey = store.keys().next().value;
    store.delete(firstKey);
  }
  store.set(key, { value, expires: Date.now() + ttl });
}