Skip to content

Commit

Permalink
[feat] add clear cache feature. bump to 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jin5354 committed Oct 9, 2017
1 parent 3a5e624 commit 87e8715
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ http({

### instance.__removeFilter(reg)

Remove filter.

### instance.__clearCache()

Clear cache.

## wrapper options

Options are optional.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 7 additions & 0 deletions src/cacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ export class Cacher {
return this.cacheMap.get(JSON.stringify(key))
}

/**
* [clear 清空缓存]
*/
clear() {
this.cacheMap.clear()
}

}
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*/
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 87e8715

Please sign in to comment.