Skip to content

Commit

Permalink
Update cacl expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
vencakrecl committed Jan 29, 2021
1 parent 273ad76 commit 6473882
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,43 @@ store.registerModule('test', {
items: [{ name: 'cachedData' }],
},
actions: {
testAction: cache.cacheAction('items', ({ commit }) => { // cache data for 30 seconds
// call API
const data = [{ name: 'newData' }];
testAction: cache.cacheAction(
'items',
({ commit }) => {
// call API
const data = [{ name: 'newData' }];

commit('testMutation', data);
return data;
},
),
testAction2: cache.cacheAction(
'items',
({ commit }) => {
// call API
const data = [{ name: 'newData' }];

return data;
}, 30),
commit('testMutation', data);

return data;
},
120, // cache data for 120 seconds
({ commit }, data) => { // onCache callback
commit('testMutation', data);
}
),
},
mutation: {
testMutation: (state, payload) => {
state.items = payload
}
}
}
})
```

### API - VuexSimpleCache.cacheAction
* key - name of key from vuex state
* action - standard vuex action
* expiration - time in seconds
* expiration - time in seconds (default 30s)
* onCache - callback for cached data (default return cached data)

[npm]: https://img.shields.io/npm/v/@vencakrecl/vuex-simple-cache.svg?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@vencakrecl/vuex-simple-cache",
"author": "Venca Krecl <[email protected]>",
"description": "Simple cache for vuex action",
"version": "1.0.0-beta.0",
"version": "1.0.0-beta.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class VuexSimpleCache {

if (expiration > 0) {
if (this.items.has(key)) {
if (Date.now() - expiration > (this.items.get(key) || 0)) {
if (Date.now() - expiration * 1000 > (this.items.get(key) || 0)) {
this.items.delete(key);

return action(context);
Expand Down

0 comments on commit 6473882

Please sign in to comment.