From 6473882080bf5b7e58b3b0933e25c3ad98a48c2b Mon Sep 17 00:00:00 2001 From: Venca Krecl Date: Fri, 29 Jan 2021 16:09:44 +0100 Subject: [PATCH] Update cacl expiration --- README.md | 32 ++++++++++++++++++++++++-------- package.json | 2 +- src/index.ts | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bc72ac3..5409127 100644 --- a/README.md +++ b/README.md @@ -32,19 +32,35 @@ 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 - } + } } }) ``` @@ -52,7 +68,7 @@ store.registerModule('test', { ### 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 diff --git a/package.json b/package.json index 29ffb1e..8c1e6f3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@vencakrecl/vuex-simple-cache", "author": "Venca Krecl ", "description": "Simple cache for vuex action", - "version": "1.0.0-beta.0", + "version": "1.0.0-beta.1", "license": "MIT", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 422d230..b25df3c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);