node-lru-cache icon indicating copy to clipboard operation
node-lru-cache copied to clipboard

has() and getRemainingTTL() contradicting each other.

Open eakl opened this issue 1 year ago • 0 comments

   describe('LRU Cache', () => {
      const clock = new Clock()
      const cache = new Cache({ max: 5, ttl: 10 })

      beforeEach(() => {
         clock.enter()
         clock.advance(1)
      })

      afterEach(() => {
         clock.exit()
      })

      it('test', () => {
         cache.set('key', 'value', { ttl: 10 })
         expect(cache.has('key')).toBe(true) // pass

         clock.advance(9)
         expect(cache.has('key')).toBe(true) // pass
         expect(cache.getRemainingTTL('key')).toBe(1) // pass

         clock.advance(1)
         expect(cache.has('key')).toBe(false) // FAILS. the key is still in cache and not stale
         expect(cache.getRemainingTTL('key')).toBe(0) // SUCCESS. the key is stale
      })
})

getRemainingTTL() says:

Return the number of ms left in the item's TTL. If item is not in cache, returns 0. Returns Infinity if item is in cache without a defined TTL.

has() says:

Will return false if the item is stale, even though it is technically in the cache.

According to getRemainingTTL, item is not in the cache. But according to has, it is.

eakl avatar Aug 12 '24 10:08 eakl