/**
 * 短 TTL 内存缓存：用于留言数量、分析数据等低频变化接口。
 */

export function createTimedCache(ttlMs) {
  const entries = new Map();

  return {
    get(key, now = Date.now()) {
      const entry = entries.get(key);
      if (!entry) return null;
      if (now - entry.savedAt > ttlMs) {
        entries.delete(key);
        return null;
      }
      return entry.value;
    },

    set(key, value, now = Date.now()) {
      entries.set(key, { value, savedAt: now });
      return value;
    },

    delete(key) {
      entries.delete(key);
    },

    clear() {
      entries.clear();
    },
  };
}
