Search code examples
cachingnestjs

Nestjs cache-manager set


Does the Nestjs cache-manager set command replace or append the given values to the key if the key already exists ? The documentation is not very clear on this


Solution

  • If the key already exists, cache-maanger replace that key. It does not append a new value.

    That's true I've not read any docs that explicitly says .set override previous data, but you can take a look to the package tests.

    I've downloaded repository and I've created a new test like this:

    it('should override key', async () => {
      cache = await caching(async (arg?: MemoryConfig) => memoryStore(arg));
      await cache.set(key, value);
      await sleep(20);
      await expect(cache.get(key)).resolves.toEqual(value);
      const newValue = 'newValue'
      await cache.set(key, newValue);
      await sleep(20)
      await expect(cache.get(key)).resolves.toEqual(newValue);
    })
    

    I've overwritten the key and the result is the new one, is not appended.