Skip to content

Commit a3a4d0b

Browse files
authored
test(memory store): sizeCalculation (jaredwray#491)
feat(memory store): calculatedSize
1 parent 0f8afcb commit a3a4d0b

File tree

4 files changed

+99
-36
lines changed

4 files changed

+99
-36
lines changed

src/stores/memory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type MemoryStore = Store & {
2626
get size(): number;
2727
dump: LRU['dump'];
2828
load: LRU['load'];
29+
calculatedSize: LRU['calculatedSize'];
2930
};
3031
export type MemoryCache = Cache<MemoryStore>;
3132

@@ -77,6 +78,9 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
7778

7879
lruCache.set(key, value, { ttl });
7980
},
81+
get calculatedSize() {
82+
return lruCache.calculatedSize;
83+
},
8084
/**
8185
* This method is not available in the caching modules.
8286
*/

test/caching.test.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('caching', () => {
2020
describe('get() and set()', () => {
2121
beforeEach(async () => {
2222
cache = await caching('memory');
23-
key = faker.random.alpha(20);
24-
value = faker.datatype.string();
23+
key = faker.string.alpha(20);
24+
value = faker.string.sample();
2525
});
2626

2727
it('lets us set and get data in cache', async () => {
@@ -60,10 +60,10 @@ describe('caching', () => {
6060
const store = 'memory';
6161

6262
beforeEach(async () => {
63-
key = faker.datatype.string(20);
64-
value = faker.datatype.string();
65-
key2 = faker.datatype.string(20);
66-
value2 = faker.datatype.string();
63+
key = faker.string.sample(20);
64+
value = faker.string.sample();
65+
key2 = faker.string.sample(20);
66+
value2 = faker.string.sample();
6767

6868
cache = await caching(store, {
6969
ttl: defaultTtl,
@@ -104,8 +104,8 @@ describe('caching', () => {
104104
describe('del()', function () {
105105
beforeEach(async () => {
106106
cache = await caching('memory');
107-
key = faker.datatype.string(20);
108-
value = faker.datatype.string();
107+
key = faker.string.sample(20);
108+
value = faker.string.sample();
109109
await cache.set(key, value, defaultTtl);
110110
});
111111

@@ -121,8 +121,8 @@ describe('caching', () => {
121121

122122
beforeEach(async () => {
123123
cache = await caching('memory');
124-
key2 = faker.datatype.string(20);
125-
value2 = faker.datatype.string();
124+
key2 = faker.string.sample(20);
125+
value2 = faker.string.sample();
126126
await cache.store.mset(
127127
[
128128
[key, value],
@@ -152,11 +152,11 @@ describe('caching', () => {
152152

153153
beforeEach(async () => {
154154
cache = await caching('memory');
155-
key = faker.datatype.string(20);
156-
value = faker.datatype.string();
155+
key = faker.string.sample(20);
156+
value = faker.string.sample();
157157
await cache.set(key, value);
158-
key2 = faker.datatype.string(20);
159-
value2 = faker.datatype.string();
158+
key2 = faker.string.sample(20);
159+
value2 = faker.string.sample();
160160
await cache.set(key2, value2);
161161
});
162162

@@ -178,9 +178,8 @@ describe('caching', () => {
178178
savedKeys = (
179179
await Promise.all(
180180
Array.from({ length: keyCount }).map(async (_, i) => {
181-
const key =
182-
(i % 3 === 0 ? 'prefix' : '') + faker.datatype.string(20);
183-
value = faker.datatype.string();
181+
const key = (i % 3 === 0 ? 'prefix' : '') + faker.string.sample(20);
182+
value = faker.string.sample();
184183
await cache.set(key, value);
185184
return key;
186185
}),

test/multi-caching.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('multiCaching', () => {
2222
let key: string;
2323

2424
async function multiMset() {
25-
const keys = [faker.datatype.string(20), faker.datatype.string(20)];
26-
const values = [faker.datatype.string(), faker.datatype.string()];
25+
const keys = [faker.string.sample(20), faker.string.sample(20)];
26+
const values = [faker.string.sample(), faker.string.sample()];
2727
await multiCache.mset(
2828
[
2929
[keys[0], values[0]],
@@ -48,16 +48,16 @@ describe('multiCaching', () => {
4848
ttl,
4949
});
5050

51-
key = faker.datatype.string(20);
51+
key = faker.string.sample(20);
5252
});
5353

5454
describe('get(), set(), del(), reset(), mget(), mset()', () => {
5555
let value: string;
5656

5757
beforeEach(() => {
5858
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]);
59-
key = faker.datatype.string(20);
60-
value = faker.datatype.string();
59+
key = faker.string.sample(20);
60+
value = faker.string.sample();
6161
});
6262

6363
describe('set()', () => {
@@ -127,7 +127,7 @@ describe('multiCaching', () => {
127127
it('lets us get multiple undefined', async () => {
128128
const len = 4;
129129
await multiMset();
130-
const args = faker.datatype.array(len).map((x) => '' + x);
130+
const args = new Array(len).fill('').map(() => faker.string.sample());
131131
await expect(multiCache.mget(...args)).resolves.toStrictEqual(
132132
new Array(len).fill(undefined),
133133
);

test/stores/memory.test.ts

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,66 @@ describe('memory store', () => {
3838
});
3939
});
4040

41+
describe('sizeCalculation', function () {
42+
let store: MemoryStore;
43+
44+
beforeEach(() => {
45+
store = memoryStore({
46+
ttl: 0,
47+
maxSize: 10,
48+
sizeCalculation: (v, k) => JSON.stringify(v).length + k.length,
49+
});
50+
});
51+
52+
it('calculatedSize and sizeCalcuation must be the same', async function () {
53+
await store.set('foo', 'bar');
54+
55+
expect(store.calculatedSize).toEqual(
56+
JSON.stringify('bar').length + 'foo'.length,
57+
);
58+
});
59+
60+
it('should cache new value and drop the old one(s) if maxSize is reached', async function () {
61+
const key = 'foo';
62+
const value = 'bar';
63+
await store.set(key, value);
64+
65+
const cache = await store.get(key);
66+
expect(cache).toEqual(value);
67+
68+
const newKey = 'foo2';
69+
const newValue = 'bar2';
70+
71+
await store.set(newKey, newValue);
72+
73+
const first = await store.get(key);
74+
const second = await store.get(newKey);
75+
76+
expect(first).toBeUndefined();
77+
expect(second).toEqual(newValue);
78+
});
79+
80+
it('should not cache something greater than maxSize', async function () {
81+
const key = 'foo';
82+
const value = 'bar'.repeat(5);
83+
await store.set(key, value);
84+
85+
const cache = await store.get(key);
86+
expect(cache).toBeUndefined();
87+
});
88+
89+
it('should throw if invalid sizeCalculation function is passed', () => {
90+
expect(() =>
91+
memoryStore({
92+
// @ts-expect-error testing if this actually throws
93+
sizeCalculation: () => {
94+
return 'invalid-type';
95+
},
96+
}),
97+
).toThrow();
98+
});
99+
});
100+
41101
describe('keyCount', function () {
42102
let memoryCache: MemoryStore;
43103

@@ -62,7 +122,7 @@ describe('memory store', () => {
62122
describe('when cache misses', () => {
63123
let key: string;
64124
beforeEach(() => {
65-
key = faker.datatype.string();
125+
key = faker.string.sample();
66126
});
67127

68128
function getCachedObject() {
@@ -215,10 +275,10 @@ describe('memory store', () => {
215275
const defaultTtl = 100;
216276

217277
beforeEach(async () => {
218-
key = faker.datatype.string(20);
219-
value = faker.datatype.string();
220-
key2 = faker.datatype.string(20);
221-
value2 = faker.datatype.string();
278+
key = faker.string.sample(20);
279+
value = faker.string.sample();
280+
key2 = faker.string.sample(20);
281+
value2 = faker.string.sample();
222282

223283
cache = await caching('memory', {
224284
shouldCloneBeforeSet: false,
@@ -267,10 +327,10 @@ describe('memory store', () => {
267327
let value2: string;
268328

269329
beforeEach(function () {
270-
key1 = faker.datatype.string(20);
271-
value1 = faker.datatype.string();
272-
key2 = faker.datatype.string(20);
273-
value2 = faker.datatype.string();
330+
key1 = faker.string.sample(20);
331+
value1 = faker.string.sample();
332+
key2 = faker.string.sample(20);
333+
value2 = faker.string.sample();
274334
});
275335

276336
it('lets us dump data', () => {
@@ -295,10 +355,10 @@ describe('memory store', () => {
295355
let data: Parameters<typeof memoryCache.load>[number];
296356

297357
beforeEach(function () {
298-
key1 = faker.datatype.string(20);
299-
value1 = faker.datatype.string();
300-
key2 = faker.datatype.string(20);
301-
value2 = faker.datatype.string();
358+
key1 = faker.string.sample(20);
359+
value1 = faker.string.sample();
360+
key2 = faker.string.sample(20);
361+
value2 = faker.string.sample();
302362
data = [
303363
[key1, { value: value1 }],
304364
[key2, { value: value2 }],

0 commit comments

Comments
 (0)