- simple cache for vuex action
npm install @vencakrecl/vuex-simple-cache
yarn add @vencakrecl/vuex-simple-cache
import Vue from 'vue';
import Vuex, { Store } from 'vuex';
import VuexSimpleCache from '@vencakrecl/vuex-simple-cache';
Vue.use(Vuex);
const store = new Store({});
// create for every module
const cache = new VuexSimpleCache()
store.registerModule('test', {
state: {
items: [{ name: 'cachedData' }],
},
actions: {
testAction: cache.cacheAction(
'items',
({ commit }) => {
// call API
const data = [{ name: 'newData' }];
return data;
},
),
testAction2: cache.cacheAction(
'items',
({ commit }) => {
// call API
const data = [{ name: 'newData' }];
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
}
}
})
- key - name of key from vuex state
- action - standard vuex action
- expiration - time in seconds (default 30s)
- onCache - callback for cached data (default return cached data)