← Усі задачіNode.js · Middle

Кеш із часом життя (TTL)

Напишіть createTtlCache({ ttlMs, now }) з методами get, set(key, value, ttl?), has, delete і size. Запис живе ttl мілісекунд (за замовчуванням ttlMs) від моменту останнього set; у момент now() >= expiresAt він уже прострочений.

Прострочені записи не повертаються й видаляються під час звернення до них, а size() рахує лише живі. has має працювати й для значень 0, null чи "". Час береться з now(), щоб кеш можна було тестувати без очікування.

Тести (5)

  1. let t = 0;
    const cache = createTtlCache({ ttlMs: 1000, now: () => t });
    cache.set('a', 1);
    expect(cache.get('a')).toBe(1);
    t = 999;
    expect(cache.get('a')).toBe(1);
    t = 1000;
    expect(cache.get('a')).toBe(undefined);
  2. let t = 0;
    const cache = createTtlCache({ ttlMs: 1000, now: () => t });
    cache.set('short', 'x', 100);
    cache.set('long', 'y');
    t = 500;
    expect(cache.has('short')).toBe(false);
    expect(cache.has('long')).toBe(true);
  3. let t = 0;
    const cache = createTtlCache({ ttlMs: 1000, now: () => t });
    cache.set('a', 1);
    t = 800;
    cache.set('a', 2);
    t = 1500;
    expect(cache.get('a')).toBe(2);
  4. let t = 0;
    const cache = createTtlCache({ ttlMs: 100, now: () => t });
    cache.set('a', 1); cache.set('b', 2); cache.set('c', 3, 1000);
    expect(cache.size()).toBe(3);
    t = 200;
    expect(cache.size()).toBe(1);
  5. let t = 0;
    const cache = createTtlCache({ ttlMs: 1000, now: () => t });
    cache.set('zero', 0);
    cache.set('empty', null);
    expect(cache.has('zero')).toBe(true);
    expect(cache.has('empty')).toBe(true);
    expect(cache.has('missing')).toBe(false);
    expect(cache.delete('zero')).toBe(true);
    expect(cache.has('zero')).toBe(false);