Skip to content

vencakrecl/vuex-simple-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vuex-simple-cache

NPM package version License Last test status

  • simple cache for vuex action

How to install

npm install @vencakrecl/vuex-simple-cache
yarn add @vencakrecl/vuex-simple-cache

How to use

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
    }
  }
})

API - VuexSimpleCache.cacheAction

  • 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)

About

Simple cache for vuex action

Resources

License

Stars

Watchers

Forks

Packages

No packages published