Skip to content

Commit

Permalink
cache-manager - adding in nonBlocking to mset (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray authored Nov 11, 2024
1 parent 29f00a2 commit 836f245
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/cache-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions packages/cache-manager/test/mset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
});

0 comments on commit 836f245

Please sign in to comment.