Skip to content

Commit

Permalink
fix(memory): passing 0 to ttl argument does not work (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Dec 1, 2022
1 parent 0bcb151 commit d6c2ed5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/stores/memory.ts
Expand Up @@ -36,7 +36,7 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
const lruOpts = {
...args,
max: args?.max || 500,
ttl: args?.ttl ? args.ttl : 0,
ttl: args?.ttl !== undefined ? args.ttl : 0,
};

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

const ttl = opt ? opt : lruOpts.ttl;
const ttl = opt !== undefined ? opt : lruOpts.ttl;

lruCache.set(key, value, { ttl });
},
Expand Down
7 changes: 7 additions & 0 deletions test/stores/memory.test.ts
Expand Up @@ -24,6 +24,13 @@ describe('memory store', () => {
await expect(store.get('foo')).resolves.toEqual('bar');
});

it('when ttl arg is passed 0', async () => {
const store = memoryStore({ ttl: 1 });
await store.set('foo', 'bar', 0);
await sleep(20);
await expect(store.get('foo')).resolves.toEqual('bar');
});

it('cache record should be expired', async () => {
await store.set('foo', 'bar', 1);
await sleep(20);
Expand Down

0 comments on commit d6c2ed5

Please sign in to comment.