Skip to content

Commit

Permalink
⬆️ upgrade lru-cache to v7 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Sep 26, 2022
1 parent 49e91e4 commit 6bcfd1e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/test-cacheAdapterEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, AxiosPromise>();
const cache = new LRUCache<string, AxiosPromise>({ max: 100 });
const http = axios.create({
adapter: cacheAdapterEnhancer(mockedAdapter, { enabledByDefault: true, cacheFlag: 'cache', defaultCache: cache }),
});
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/test-retryAdapterEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/test-throttleAdapterEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, RecordedCache>();
const cache = new LRUCache<string, RecordedCache>({ max: 100 });
const http = axios.create({
adapter: throttleAdapterEnhancer(mockedAdapter, { cache }),
});
Expand All @@ -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),
Expand Down
14 changes: 3 additions & 11 deletions src/cacheAdapterEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand All @@ -19,14 +19,6 @@ declare module 'axios' {
const FIVE_MINUTES = 1000 * 60 * 5;
const CAPACITY = 100;

export interface ICacheLike<T> {
get(key: string): T | undefined;

set(key: string, value: T, maxAge?: number): boolean;

del(key: string): void;
}

export type Options = {
enabledByDefault?: boolean,
cacheFlag?: string,
Expand All @@ -38,7 +30,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
const {
enabledByDefault = true,
cacheFlag = 'cache',
defaultCache = new LRUCache<string, AxiosPromise>({ maxAge: FIVE_MINUTES, max: CAPACITY }),
defaultCache = getDefaultLruCache<AxiosPromise>({ ttl: FIVE_MINUTES, max: CAPACITY }),
} = options;

return config => {
Expand All @@ -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<AxiosPromise> = isCacheLike(useCache) ? useCache : defaultCache;

// build the index according to the url and params
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/throttleAdapterEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +20,7 @@ export type Options = {

export default function throttleAdapterEnhancer(adapter: AxiosAdapter, options: Options = {}): AxiosAdapter {

const { threshold = 1000, cache = new LRUCache<string, RecordedCache>({ max: 10 }) } = options;
const { threshold = 1000, cache = getDefaultLruCache<RecordedCache>({ max: 10 }) } = options;

const recordCacheWithRequest = (index: string, config: AxiosRequestConfig) => {

Expand Down
19 changes: 19 additions & 0 deletions src/utils/getDefaultLruCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AxiosPromise } from 'axios';
import LRUCache from 'lru-cache';

export interface ICacheLike<T> {
get(key: string): T | undefined;

set(key: string, value: T): void;

del(key: string): void;
}

export default function getDefaultLruCache<T>(options: LRUCache.Options<any, any>): ICacheLike<T> {
const defaultLruCache = new LRUCache<string, T>(options);
return {
get: defaultLruCache.get.bind(defaultLruCache),
set: defaultLruCache.set.bind(defaultLruCache),
del: defaultLruCache.delete.bind(defaultLruCache),
};
}

0 comments on commit 6bcfd1e

Please # to comment.