superwf / vuex-cache

cache vuex action when dispatch
MIT License
510 stars 32 forks source link

Cache expiry #4

Closed ghost closed 6 years ago

ghost commented 6 years ago

Just a suggestion,

Would be great if there was a way to set an expiry on the cache (ie 5 mins etc).

Something like store.cache.dispatch('LIST', 5 * 60, {..additional args}) etc

I can see where there might be issues keeping track of if additional arguments are expiries or normal data, but just off the top of my head:

 store.cache.dispatch = function () {
    const type = arguments[0]

    let current = store.cache.get(type)
    let now = new Date();    

    if (!store.cache.has(type) || current.expiry < now) {
      // Somehow add the expiry with expiry = now.setSeconds(now.getSeconds + arguments[1]) then splice it from the arguments?
      store.cache.set(type, store.dispatch.apply(store, arguments))
    }

    return store.cache.get(type)
  }
superwf commented 6 years ago

I think expiry should be controlled out of the cache circle. When cache expired, call store.cache.delete manually is a better choise. Thank you all the same.