Skip to content

Commit bf68d36

Browse files
committed
fix(multi-caching): minor issues
1 parent a9f1956 commit bf68d36

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

src/multi-caching.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Cache, Milliseconds } from './caching';
22

3-
export type MultiCache = Omit<Cache, 'store'> & {
4-
mset(args: [string, unknown][], ttl?: Milliseconds): Promise<void>;
5-
mget(...args: string[]): Promise<unknown[]>;
6-
mdel(...args: string[]): Promise<void>;
7-
};
3+
export type MultiCache = Omit<Cache, 'store'> &
4+
Pick<Cache['store'], 'mset' | 'mget' | 'mdel'>;
85

96
/**
107
* Module that lets you specify a hierarchy of caches.

test/multi-caching.test.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ describe('multiCaching', () => {
2121
let defaultTtl: number;
2222
let key: string;
2323

24+
async function multiMset() {
25+
const keys = [faker.datatype.string(20), faker.datatype.string(20)];
26+
const values = [faker.datatype.string(), faker.datatype.string()];
27+
await multiCache.mset(
28+
[
29+
[keys[0], values[0]],
30+
[keys[1], values[1]],
31+
],
32+
defaultTtl,
33+
);
34+
return { keys, values };
35+
}
36+
2437
beforeEach(async () => {
2538
ttl = 100;
2639
defaultTtl = 5000;
@@ -93,15 +106,7 @@ describe('multiCaching', () => {
93106

94107
describe('mset()', () => {
95108
it('lets us set multiple keys in all caches', async () => {
96-
const keys = [faker.datatype.string(20), faker.datatype.string(20)];
97-
const values = [faker.datatype.string(), faker.datatype.string()];
98-
await multiCache.mset(
99-
[
100-
[keys[0], values[0]],
101-
[keys[1], values[1]],
102-
],
103-
defaultTtl,
104-
);
109+
const { keys, values } = await multiMset();
105110
await expect(memoryCache.get(keys[0])).resolves.toEqual(values[0]);
106111
await expect(memoryCache2.get(keys[0])).resolves.toEqual(values[0]);
107112
await expect(memoryCache3.get(keys[0])).resolves.toEqual(values[0]);
@@ -113,31 +118,28 @@ describe('multiCaching', () => {
113118

114119
describe('mget()', () => {
115120
it('lets us get multiple keys', async () => {
116-
const keys = [faker.datatype.string(20), faker.datatype.string(20)];
117-
const values = [faker.datatype.string(), faker.datatype.string()];
121+
const { keys, values } = await multiMset();
118122
await multiCache.set(keys[0], values[0], defaultTtl);
119123
await memoryCache3.set(keys[1], values[1], defaultTtl);
120124
await expect(multiCache.mget(...keys)).resolves.toStrictEqual(values);
121125
});
126+
127+
it('lets us get multiple undefined', async () => {
128+
const len = 4;
129+
await multiMset();
130+
const args = faker.datatype.array(len).map((x) => '' + x);
131+
await expect(multiCache.mget(...args)).resolves.toStrictEqual(
132+
new Array(len).fill(undefined),
133+
);
134+
});
122135
});
123136

124137
describe('mdel()', () => {
125138
it('lets us delete multiple keys', async () => {
126-
const keys = [faker.datatype.string(20), faker.datatype.string(20)];
127-
const values = [faker.datatype.string(), faker.datatype.string()];
128-
await multiCache.mset(
129-
[
130-
[keys[0], values[0]],
131-
[keys[1], values[1]],
132-
],
133-
defaultTtl,
134-
);
135-
await expect(multiCache.mget(...keys)).resolves.toStrictEqual(values);
139+
const { keys } = await multiMset();
136140
await multiCache.mdel(...keys);
137-
await expect(multiCache.mget(...keys)).resolves.toStrictEqual([
138-
undefined,
139-
undefined,
140-
]);
141+
await expect(memoryCache.get(keys[0])).resolves.toBeUndefined();
142+
await expect(memoryCache.get(keys[1])).resolves.toBeUndefined();
141143
});
142144
});
143145

0 commit comments

Comments
 (0)