Skip to content

Commit 6bcfd1e

Browse files
authored
⬆️ upgrade lru-cache to v7 (#90)
1 parent 49e91e4 commit 6bcfd1e

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
"dist"
4040
],
4141
"dependencies": {
42-
"@types/lru-cache": "^5.1.0",
43-
"lru-cache": "^6.0.0",
42+
"lru-cache": "^7.14.0",
4443
"tslib": "^2.1.0",
4544
"util": "^0.12.3"
4645
},

src/__tests__/test-cacheAdapterEnhancer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test('request will refresh the cache with forceUpdate config', async t => {
120120

121121
const adapterCb = spy();
122122
const mockedAdapter = genMockAdapter(adapterCb);
123-
const cache = new LRUCache<string, AxiosPromise>();
123+
const cache = new LRUCache<string, AxiosPromise>({ max: 100 });
124124
const http = axios.create({
125125
adapter: cacheAdapterEnhancer(mockedAdapter, { enabledByDefault: true, cacheFlag: 'cache', defaultCache: cache }),
126126
});
@@ -150,12 +150,12 @@ test('use a custom cache with request individual config', async t => {
150150
adapter: cacheAdapterEnhancer(mockedAdapter),
151151
});
152152

153-
const cache1 = new LRUCache();
154-
const cache2 = new LRUCache();
153+
const cache1 = new LRUCache({ max: 100 });
154+
const cache2 = new LRUCache({ max: 100 });
155155
await Promise.all([http.get('/users', { cache: cache1 } as any), http.get('/users', { cache: cache2 } as any)]);
156156
t.is(adapterCb.callCount, 2);
157157

158-
cache2.reset();
158+
cache2.clear();
159159
await Promise.all([http.get('/users', { cache: cache1 } as any), http.get('/users', { cache: cache2 } as any)]);
160160

161161
t.is(adapterCb.callCount, 3);

src/__tests__/test-retryAdapterEnhancer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test('should throw an exception while request still failed after retry', async (
6161

6262
try {
6363
await http.get('/test');
64-
} catch (e) {
64+
} catch (e: any) {
6565
t.is(e.url, '/test');
6666
t.is(spyFn.callCount, defaultTimes + 1);
6767
}
@@ -81,7 +81,7 @@ test('should retry with special times for the custom config request', async (t)
8181
const customRetryTimes = 4;
8282
try {
8383
await http.get('/test', { retryTimes: customRetryTimes });
84-
} catch (e) {
84+
} catch (e: any) {
8585
t.is(e.url, '/test');
8686
t.is(spyFn.callCount, customRetryTimes + 1);
8787
}

src/__tests__/test-throttleAdapterEnhancer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test('use a custom cache for throttle enhancer', async t => {
102102

103103
const adapterCb = spy();
104104
const mockedAdapter = genMockAdapter(adapterCb);
105-
const cache = new LRUCache<string, RecordedCache>();
105+
const cache = new LRUCache<string, RecordedCache>({ max: 100 });
106106
const http = axios.create({
107107
adapter: throttleAdapterEnhancer(mockedAdapter, { cache }),
108108
});
@@ -115,7 +115,7 @@ test('use a custom cache for throttle enhancer', async t => {
115115
t.is(onSuccess.callCount, 2);
116116
t.is(adapterCb.callCount, 1);
117117

118-
cache.del('/users');
118+
cache.delete('/users');
119119
await Promise.all([
120120
http.get('/users').then(onSuccess),
121121
http.get('/users').then(onSuccess),

src/cacheAdapterEnhancer.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
import { AxiosAdapter, AxiosPromise } from 'axios';
8-
import LRUCache from 'lru-cache';
98
import buildSortedURL from './utils/buildSortedURL';
9+
import getDefaultLruCache, { ICacheLike } from './utils/getDefaultLruCache';
1010
import isCacheLike from './utils/isCacheLike';
1111

1212
declare module 'axios' {
@@ -19,14 +19,6 @@ declare module 'axios' {
1919
const FIVE_MINUTES = 1000 * 60 * 5;
2020
const CAPACITY = 100;
2121

22-
export interface ICacheLike<T> {
23-
get(key: string): T | undefined;
24-
25-
set(key: string, value: T, maxAge?: number): boolean;
26-
27-
del(key: string): void;
28-
}
29-
3022
export type Options = {
3123
enabledByDefault?: boolean,
3224
cacheFlag?: string,
@@ -38,7 +30,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
3830
const {
3931
enabledByDefault = true,
4032
cacheFlag = 'cache',
41-
defaultCache = new LRUCache<string, AxiosPromise>({ maxAge: FIVE_MINUTES, max: CAPACITY }),
33+
defaultCache = getDefaultLruCache<AxiosPromise>({ ttl: FIVE_MINUTES, max: CAPACITY }),
4234
} = options;
4335

4436
return config => {
@@ -50,7 +42,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
5042

5143
if (method === 'get' && useCache) {
5244

53-
// if had provide a specified cache, then use it instead
45+
// if had provided a specified cache, then use it instead
5446
const cache: ICacheLike<AxiosPromise> = isCacheLike(useCache) ? useCache : defaultCache;
5547

5648
// build the index according to the url and params

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66

77
import Cache from 'lru-cache';
8-
import cacheAdapterEnhancer, { ICacheLike } from './cacheAdapterEnhancer';
8+
import cacheAdapterEnhancer from './cacheAdapterEnhancer';
99
import retryAdapterEnhancer from './retryAdapterEnhancer';
1010
import throttleAdapterEnhancer from './throttleAdapterEnhancer';
11+
import { ICacheLike } from './utils/getDefaultLruCache';
1112

1213
export {
1314
Cache,

src/throttleAdapterEnhancer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
*/
66

77
import { AxiosAdapter, AxiosPromise, AxiosRequestConfig } from 'axios';
8-
import LRUCache from 'lru-cache';
9-
import { ICacheLike } from './cacheAdapterEnhancer';
108
import buildSortedURL from './utils/buildSortedURL';
9+
import getDefaultLruCache, { ICacheLike } from './utils/getDefaultLruCache';
1110

1211
export type RecordedCache = {
1312
timestamp: number;
@@ -21,7 +20,7 @@ export type Options = {
2120

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

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

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

src/utils/getDefaultLruCache.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AxiosPromise } from 'axios';
2+
import LRUCache from 'lru-cache';
3+
4+
export interface ICacheLike<T> {
5+
get(key: string): T | undefined;
6+
7+
set(key: string, value: T): void;
8+
9+
del(key: string): void;
10+
}
11+
12+
export default function getDefaultLruCache<T>(options: LRUCache.Options<any, any>): ICacheLike<T> {
13+
const defaultLruCache = new LRUCache<string, T>(options);
14+
return {
15+
get: defaultLruCache.get.bind(defaultLruCache),
16+
set: defaultLruCache.set.bind(defaultLruCache),
17+
del: defaultLruCache.delete.bind(defaultLruCache),
18+
};
19+
}

0 commit comments

Comments
 (0)