From 6bcfd1edc80875dfca7b157f3922a869ae71d8b1 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Mon, 26 Sep 2022 16:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20upgrade=20lru-cache=20to?= =?UTF-8?q?=20v7=20(#90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +-- src/__tests__/test-cacheAdapterEnhancer.ts | 8 ++++---- src/__tests__/test-retryAdapterEnhancer.ts | 4 ++-- src/__tests__/test-throttleAdapterEnhancer.ts | 4 ++-- src/cacheAdapterEnhancer.ts | 14 +++----------- src/index.ts | 3 ++- src/throttleAdapterEnhancer.ts | 5 ++--- src/utils/getDefaultLruCache.ts | 19 +++++++++++++++++++ 8 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 src/utils/getDefaultLruCache.ts diff --git a/package.json b/package.json index ed9882b..10a8780 100755 --- a/package.json +++ b/package.json @@ -39,8 +39,7 @@ "dist" ], "dependencies": { - "@types/lru-cache": "^5.1.0", - "lru-cache": "^6.0.0", + "lru-cache": "^7.14.0", "tslib": "^2.1.0", "util": "^0.12.3" }, diff --git a/src/__tests__/test-cacheAdapterEnhancer.ts b/src/__tests__/test-cacheAdapterEnhancer.ts index 81849e6..42d9e66 100644 --- a/src/__tests__/test-cacheAdapterEnhancer.ts +++ b/src/__tests__/test-cacheAdapterEnhancer.ts @@ -120,7 +120,7 @@ test('request will refresh the cache with forceUpdate config', async t => { const adapterCb = spy(); const mockedAdapter = genMockAdapter(adapterCb); - const cache = new LRUCache(); + const cache = new LRUCache({ max: 100 }); const http = axios.create({ adapter: cacheAdapterEnhancer(mockedAdapter, { enabledByDefault: true, cacheFlag: 'cache', defaultCache: cache }), }); @@ -150,12 +150,12 @@ test('use a custom cache with request individual config', async t => { adapter: cacheAdapterEnhancer(mockedAdapter), }); - const cache1 = new LRUCache(); - const cache2 = new LRUCache(); + const cache1 = new LRUCache({ max: 100 }); + const cache2 = new LRUCache({ max: 100 }); await Promise.all([http.get('/users', { cache: cache1 } as any), http.get('/users', { cache: cache2 } as any)]); t.is(adapterCb.callCount, 2); - cache2.reset(); + cache2.clear(); await Promise.all([http.get('/users', { cache: cache1 } as any), http.get('/users', { cache: cache2 } as any)]); t.is(adapterCb.callCount, 3); diff --git a/src/__tests__/test-retryAdapterEnhancer.ts b/src/__tests__/test-retryAdapterEnhancer.ts index a7bd4db..cbdd854 100644 --- a/src/__tests__/test-retryAdapterEnhancer.ts +++ b/src/__tests__/test-retryAdapterEnhancer.ts @@ -61,7 +61,7 @@ test('should throw an exception while request still failed after retry', async ( try { await http.get('/test'); - } catch (e) { + } catch (e: any) { t.is(e.url, '/test'); t.is(spyFn.callCount, defaultTimes + 1); } @@ -81,7 +81,7 @@ test('should retry with special times for the custom config request', async (t) const customRetryTimes = 4; try { await http.get('/test', { retryTimes: customRetryTimes }); - } catch (e) { + } catch (e: any) { t.is(e.url, '/test'); t.is(spyFn.callCount, customRetryTimes + 1); } diff --git a/src/__tests__/test-throttleAdapterEnhancer.ts b/src/__tests__/test-throttleAdapterEnhancer.ts index beb7f14..46f374c 100644 --- a/src/__tests__/test-throttleAdapterEnhancer.ts +++ b/src/__tests__/test-throttleAdapterEnhancer.ts @@ -102,7 +102,7 @@ test('use a custom cache for throttle enhancer', async t => { const adapterCb = spy(); const mockedAdapter = genMockAdapter(adapterCb); - const cache = new LRUCache(); + const cache = new LRUCache({ max: 100 }); const http = axios.create({ adapter: throttleAdapterEnhancer(mockedAdapter, { cache }), }); @@ -115,7 +115,7 @@ test('use a custom cache for throttle enhancer', async t => { t.is(onSuccess.callCount, 2); t.is(adapterCb.callCount, 1); - cache.del('/users'); + cache.delete('/users'); await Promise.all([ http.get('/users').then(onSuccess), http.get('/users').then(onSuccess), diff --git a/src/cacheAdapterEnhancer.ts b/src/cacheAdapterEnhancer.ts index 5e5a827..b94e474 100644 --- a/src/cacheAdapterEnhancer.ts +++ b/src/cacheAdapterEnhancer.ts @@ -5,8 +5,8 @@ */ import { AxiosAdapter, AxiosPromise } from 'axios'; -import LRUCache from 'lru-cache'; import buildSortedURL from './utils/buildSortedURL'; +import getDefaultLruCache, { ICacheLike } from './utils/getDefaultLruCache'; import isCacheLike from './utils/isCacheLike'; declare module 'axios' { @@ -19,14 +19,6 @@ declare module 'axios' { const FIVE_MINUTES = 1000 * 60 * 5; const CAPACITY = 100; -export interface ICacheLike { - get(key: string): T | undefined; - - set(key: string, value: T, maxAge?: number): boolean; - - del(key: string): void; -} - export type Options = { enabledByDefault?: boolean, cacheFlag?: string, @@ -38,7 +30,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt const { enabledByDefault = true, cacheFlag = 'cache', - defaultCache = new LRUCache({ maxAge: FIVE_MINUTES, max: CAPACITY }), + defaultCache = getDefaultLruCache({ ttl: FIVE_MINUTES, max: CAPACITY }), } = options; return config => { @@ -50,7 +42,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt if (method === 'get' && useCache) { - // if had provide a specified cache, then use it instead + // if had provided a specified cache, then use it instead const cache: ICacheLike = isCacheLike(useCache) ? useCache : defaultCache; // build the index according to the url and params diff --git a/src/index.ts b/src/index.ts index 6db0a2b..ebf95a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,9 +5,10 @@ */ import Cache from 'lru-cache'; -import cacheAdapterEnhancer, { ICacheLike } from './cacheAdapterEnhancer'; +import cacheAdapterEnhancer from './cacheAdapterEnhancer'; import retryAdapterEnhancer from './retryAdapterEnhancer'; import throttleAdapterEnhancer from './throttleAdapterEnhancer'; +import { ICacheLike } from './utils/getDefaultLruCache'; export { Cache, diff --git a/src/throttleAdapterEnhancer.ts b/src/throttleAdapterEnhancer.ts index ee1ee10..3b745a7 100644 --- a/src/throttleAdapterEnhancer.ts +++ b/src/throttleAdapterEnhancer.ts @@ -5,9 +5,8 @@ */ import { AxiosAdapter, AxiosPromise, AxiosRequestConfig } from 'axios'; -import LRUCache from 'lru-cache'; -import { ICacheLike } from './cacheAdapterEnhancer'; import buildSortedURL from './utils/buildSortedURL'; +import getDefaultLruCache, { ICacheLike } from './utils/getDefaultLruCache'; export type RecordedCache = { timestamp: number; @@ -21,7 +20,7 @@ export type Options = { export default function throttleAdapterEnhancer(adapter: AxiosAdapter, options: Options = {}): AxiosAdapter { - const { threshold = 1000, cache = new LRUCache({ max: 10 }) } = options; + const { threshold = 1000, cache = getDefaultLruCache({ max: 10 }) } = options; const recordCacheWithRequest = (index: string, config: AxiosRequestConfig) => { diff --git a/src/utils/getDefaultLruCache.ts b/src/utils/getDefaultLruCache.ts new file mode 100644 index 0000000..a9577e0 --- /dev/null +++ b/src/utils/getDefaultLruCache.ts @@ -0,0 +1,19 @@ +import { AxiosPromise } from 'axios'; +import LRUCache from 'lru-cache'; + +export interface ICacheLike { + get(key: string): T | undefined; + + set(key: string, value: T): void; + + del(key: string): void; +} + +export default function getDefaultLruCache(options: LRUCache.Options): ICacheLike { + const defaultLruCache = new LRUCache(options); + return { + get: defaultLruCache.get.bind(defaultLruCache), + set: defaultLruCache.set.bind(defaultLruCache), + del: defaultLruCache.delete.bind(defaultLruCache), + }; +}