diff --git a/README.md b/README.md index 1f07417..d2b77a5 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,12 @@ http({ ### instance.__removeFilter(reg) +Remove filter. + +### instance.__clearCache() + +Clear cache. + ## wrapper options Options are optional. diff --git a/package.json b/package.json index a3af83b..e662351 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "axios-cache-plugin", - "version": "0.0.7", + "version": "0.1.0", "description": "Help you cache GET request when using axios.", "main": "dist/index.js", "repository": { diff --git a/src/cacher.js b/src/cacher.js index 5931c70..003601a 100644 --- a/src/cacher.js +++ b/src/cacher.js @@ -85,4 +85,11 @@ export class Cacher { return this.cacheMap.get(JSON.stringify(key)) } + /** + * [clear 清空缓存] + */ + clear() { + this.cacheMap.clear() + } + } diff --git a/src/index.js b/src/index.js index d45edd7..cedff8c 100644 --- a/src/index.js +++ b/src/index.js @@ -104,6 +104,13 @@ export default function wrapper(instance, option) { */ axiosWithCache.__cacher = cacher + /** + * [__clearCache cacher instance clear function proxy] + */ + axiosWithCache.__clearCache = function(){ + cacher.clear() + } + /** * [proxy axios instance functions which are no need to be cached] */ diff --git a/test/test.js b/test/test.js index 24b1f60..43d38e3 100644 --- a/test/test.js +++ b/test/test.js @@ -285,3 +285,31 @@ test('with out options ignore header', async t => { }) t.is(http.__cacher.cacheMap.size, 2) }) + +test('clear cache', async t => { + + let http = wrapper(axios) + let reg = /users/ + http.__addFilter(reg) + + t.is(http.__cacher.cacheMap.size, 0) + http.__clearCache() + t.is(http.__cacher.cacheMap.size, 0) + + await http({ + url: 'http://www.404forest.com:3000/users/jin5354', + method: 'get' + }) + + t.is(http.__cacher.cacheMap.size, 1) + + await http({ + url: 'http://www.404forest.com:3000/users/yyhappynice', + method: 'get' + }) + + t.is(http.__cacher.cacheMap.size, 2) + + http.__clearCache() + t.is(http.__cacher.cacheMap.size, 0) +})