Skip to content

Commit d6c2ed5

Browse files
authored
fix(memory): passing 0 to ttl argument does not work (jaredwray#282)
1 parent 0bcb151 commit d6c2ed5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/stores/memory.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
3636
const lruOpts = {
3737
...args,
3838
max: args?.max || 500,
39-
ttl: args?.ttl ? args.ttl : 0,
39+
ttl: args?.ttl !== undefined ? args.ttl : 0,
4040
};
4141

4242
const lruCache = new LRUCache<string, unknown>(lruOpts);
@@ -49,7 +49,7 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
4949
keys: async () => [...lruCache.keys()],
5050
mget: async (...args) => args.map((x) => lruCache.get(x)),
5151
async mset(args, ttl?) {
52-
const opt = { ttl: ttl ? ttl : lruOpts.ttl } as const;
52+
const opt = { ttl: ttl !== undefined ? ttl : lruOpts.ttl } as const;
5353
for (const [key, value] of args) {
5454
if (!isCacheable(value))
5555
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
@@ -69,7 +69,7 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
6969
throw new Error(`no cacheable value ${JSON.stringify(value)}`);
7070
if (shouldCloneBeforeSet) value = clone(value);
7171

72-
const ttl = opt ? opt : lruOpts.ttl;
72+
const ttl = opt !== undefined ? opt : lruOpts.ttl;
7373

7474
lruCache.set(key, value, { ttl });
7575
},

test/stores/memory.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ describe('memory store', () => {
2424
await expect(store.get('foo')).resolves.toEqual('bar');
2525
});
2626

27+
it('when ttl arg is passed 0', async () => {
28+
const store = memoryStore({ ttl: 1 });
29+
await store.set('foo', 'bar', 0);
30+
await sleep(20);
31+
await expect(store.get('foo')).resolves.toEqual('bar');
32+
});
33+
2734
it('cache record should be expired', async () => {
2835
await store.set('foo', 'bar', 1);
2936
await sleep(20);

0 commit comments

Comments
 (0)