Skip to content

Commit 5c36673

Browse files
authored
🐛 compatible lru-cache v7 del method and fix its overwrote issue (#96)
1 parent 77a3b0e commit 5c36673

File tree

5 files changed

+15
-40
lines changed

5 files changed

+15
-40
lines changed

src/cacheAdapterEnhancer.ts

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

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

1212
declare module 'axios' {
1313
interface AxiosRequestConfig {
@@ -30,7 +30,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
3030
const {
3131
enabledByDefault = true,
3232
cacheFlag = 'cache',
33-
defaultCache = getDefaultLruCache<AxiosPromise>({ ttl: FIVE_MINUTES, max: CAPACITY }),
33+
defaultCache = new LRUCache({ ttl: FIVE_MINUTES, max: CAPACITY }),
3434
} = options;
3535

3636
return config => {
@@ -57,7 +57,7 @@ export default function cacheAdapterEnhancer(adapter: AxiosAdapter, options: Opt
5757
try {
5858
return await adapter(config);
5959
} catch (reason) {
60-
cache.del(index);
60+
'delete' in cache ? cache.delete(index) : cache.del(index);
6161
throw reason;
6262
}
6363

src/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@
44
* @since 2017-09-28
55
*/
66

7-
import LRUCache from 'lru-cache';
7+
import Cache from 'lru-cache';
88
import cacheAdapterEnhancer from './cacheAdapterEnhancer';
99
import retryAdapterEnhancer from './retryAdapterEnhancer';
1010
import throttleAdapterEnhancer from './throttleAdapterEnhancer';
11-
import { ICacheLike } from './utils/getDefaultLruCache';
12-
13-
class Cache<K, V> extends LRUCache<K, V> {
14-
constructor(options: LRUCache.Options<any, any>) {
15-
super(options);
16-
}
17-
18-
del(key: K): boolean {
19-
return super.delete(key);
20-
}
21-
}
11+
import { ICacheLike } from './utils/isCacheLike';
2212

2313
export {
2414
Cache,

src/throttleAdapterEnhancer.ts

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

77
import { AxiosAdapter, AxiosPromise, AxiosRequestConfig } from 'axios';
8+
import LRUCache from 'lru-cache';
89
import buildSortedURL from './utils/buildSortedURL';
9-
import getDefaultLruCache, { ICacheLike } from './utils/getDefaultLruCache';
10+
import { ICacheLike } from './utils/isCacheLike';
1011

1112
export type RecordedCache = {
1213
timestamp: number;
@@ -20,7 +21,7 @@ export type Options = {
2021

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

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

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

@@ -37,7 +38,7 @@ export default function throttleAdapterEnhancer(adapter: AxiosAdapter, options:
3738

3839
return response;
3940
} catch (reason) {
40-
cache.del(index);
41+
'delete' in cache ? cache.delete(index) : cache.del(index);
4142
throw reason;
4243
}
4344

src/utils/getDefaultLruCache.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/utils/isCacheLike.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* @homepage https://github.com/kuitos/
44
* @since 2018-03-19
55
*/
6-
import { ICacheLike } from './getDefaultLruCache';
6+
export type ICacheLike<T> = {
7+
get(key: string): T | undefined;
8+
set(key: string, value: T): void;
9+
} & ({ del(key: string): void } | { delete(key: string): void });
710

811
export default function isCacheLike(cache: any): cache is ICacheLike<any> {
9-
return typeof cache.get === 'function' && typeof cache.set === 'function' && typeof cache.del === 'function';
12+
return typeof cache.get === 'function' && typeof cache.set === 'function' && (typeof cache.delete === 'function' || typeof cache.del === 'function');
1013
}

0 commit comments

Comments
 (0)