Skip to content

Commit

Permalink
fix: update lru-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zzau13 committed Apr 23, 2023
1 parent ca1b9bd commit 87efeff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,7 +42,7 @@
"license": "MIT",
"dependencies": {
"lodash.clonedeep": "^4.5.0",
"lru-cache": "~9.1.0"
"lru-cache": "~9.1.1"
},
"devDependencies": {
"@commitlint/cli": "17.6.1",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions src/stores/memory.ts
@@ -1,28 +1,31 @@
import LRUCache, { Options } from 'lru-cache';
import { LRUCache } from 'lru-cache';
import cloneDeep from 'lodash.clonedeep';

import { Config, Cache, Store } from '../caching';

type Lru = LRUCache<string, unknown>;

function clone<T>(object: T): T {
if (typeof object === 'object' && object !== null) {
return cloneDeep(object);
}
return object;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LRU = LRUCache<string, any, unknown>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Pre = LRUCache.OptionsTTLLimit<string, any, unknown>;
type Options = Omit<Pre, 'ttlAutopurge'> & Partial<Pick<Pre, 'ttlAutopurge'>>;
export type MemoryConfig = {
max?: number;
sizeCalculation?: (key: string, value: unknown) => number;
shouldCloneBeforeSet?: boolean;
} & Options<string, unknown> &
} & Options &
Config;

export type MemoryStore = Store & {
keyCount: () => number;
dump: Lru['dump'];
load: Lru['load'];
get size(): number;
dump: LRU['dump'];
load: LRU['load'];
};
export type MemoryCache = Cache<MemoryStore>;

Expand All @@ -34,12 +37,13 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
const isCacheable = args?.isCacheable ?? ((val) => val !== undefined);

const lruOpts = {
ttlAutopurge: true,
...args,
max: args?.max || 500,
ttl: args?.ttl !== undefined ? args.ttl : 0,
};

const lruCache = new LRUCache<string, unknown>(lruOpts);
const lruCache = new LRUCache(lruOpts);

return {
async del(key) {
Expand Down Expand Up @@ -76,14 +80,16 @@ export function memoryStore(args?: MemoryConfig): MemoryStore {
/**
* This method is not available in the caching modules.
*/
keyCount: () => lruCache.size,
get size() {
return lruCache.size;
},
/**
* This method is not available in the caching modules.
*/
dump: () => lruCache.dump(),
/**
* This method is not available in the caching modules.
*/
load: (...args: Parameters<Lru['load']>) => lruCache.load(...args),
load: (...args: Parameters<LRU['load']>) => lruCache.load(...args),
};
}
15 changes: 8 additions & 7 deletions test/stores/memory.test.ts
Expand Up @@ -47,11 +47,11 @@ describe('memory store', () => {
it('return total length of keys in cache', async () => {
memoryCache = memoryStore({ ttl: 10 });
await memoryCache.set('foo', 'bar');
await memoryCache.set('bar', 'foo');
expect(memoryCache.keyCount()).toEqual(2);
await memoryCache.set('bar', 'foo', 100);
expect(memoryCache.size).toEqual(2);
await sleep(20);
await expect(memoryCache.get('foo')).resolves.toBeUndefined();
expect(memoryCache.keyCount()).toEqual(1);
expect(memoryCache.size).toEqual(1);
});
});

Expand Down Expand Up @@ -89,10 +89,11 @@ describe('memory store', () => {
return cache.wrap(key, async () => () => 'foo');
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
function Thing() {}

Thing.prototype.f = () => 'foo';
class Thing {
f() {
return 'foo';
}
}

function getCachedObjectWithPrototype() {
return cache.wrap(key, async () => new Thing());
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"lib": ["esnext"],
"module": "commonjs",
"target": "ES2015",
"strict": true,
"esModuleInterop": true,
"sourceMap": true,
Expand Down

0 comments on commit 87efeff

Please sign in to comment.