diff --git a/packages/cache-manager/src/index.ts b/packages/cache-manager/src/index.ts index bc735f52..c81b4031 100644 --- a/packages/cache-manager/src/index.ts +++ b/packages/cache-manager/src/index.ts @@ -91,6 +91,13 @@ export const createCache = (options?: CreateCacheOptions) => { promises.push(stores.map(async store => store.set(item.key, item.value, item.ttl))); } + if (nonBlocking) { + // eslint-disable-next-line @typescript-eslint/no-floating-promises + Promise.all(promises); + eventEmitter.emit('mset', {list}); + return list; + } + await Promise.all(promises); eventEmitter.emit('mset', {list}); return list; diff --git a/packages/cache-manager/test/mset.test.ts b/packages/cache-manager/test/mset.test.ts index 2058ea5f..329b3bf2 100644 --- a/packages/cache-manager/test/mset.test.ts +++ b/packages/cache-manager/test/mset.test.ts @@ -4,6 +4,7 @@ import { } from 'vitest'; import {faker} from '@faker-js/faker'; import {createCache} from '../src/index.js'; +import {sleep} from './sleep.js'; describe('mset', () => { let keyv: Keyv; @@ -26,4 +27,18 @@ describe('mset', () => { await expect(cache.mset(list)).resolves.toEqual(list); await expect(cache.get(list[0].key)).resolves.toEqual(list[0].value); }); + + it('should mset non-blocking', async () => { + const secondKeyv = new Keyv(); + const cache = createCache({stores: [keyv, secondKeyv], nonBlocking: true}); + const list = [ + {key: faker.string.alpha(20), value: faker.string.sample()}, + {key: faker.string.alpha(20), value: faker.string.sample()}, + {key: faker.string.alpha(20), value: faker.string.sample()}, + ]; + + await cache.mset(list); + await sleep(10); + await expect(cache.get(list[0].key)).resolves.toEqual(list[0].value); + }); });