superwf / vuex-cache

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

Delete cache on call native dispatch #25

Closed henriq-88 closed 5 years ago

henriq-88 commented 5 years ago

Should a native action call (e.g. without .cache) update an already existing cache? Or is the sequence below intended?

let users = ['Sven', 'Kurt']
store.dispatch('setUsers', users)
store.cache.dispatch('getUsers') // ['Sven', 'Kurt'] (not cached)
users.push('Nisse')
store.dispatch('setUsers', users)
store.dispatch('getUsers') // ['Sven', 'Kurt', 'Nisse'] (native action, not cached)
store.cache.dispatch('getUsers') // ['Sven', 'Kurt'] (cached)
superwf commented 5 years ago

each time when call users.push('Nisse') delete the cache key 'getUsers' by using the 'delete' method store.cache.delete('getUsers') next time the cache.dispatch will make a real dispatch to fetch the latest data

henriq-88 commented 5 years ago

My hope was that it would automatically delete the cache when calling the native function.

Anyways thanks for the explanation.

VitorLuizC commented 5 years ago

It does not automatically delete the cache when you dispatch "native" actions.

@henriq-88, docs are confuse enough to make you think it'll work or its a enhancement suggestion?

VitorLuizC commented 5 years ago

If it's a suggestion I can reopen this issue and put suggestion label in it.

henriq-88 commented 5 years ago

Yes, it is a suggestion. That would be great, thanks!

VitorLuizC commented 5 years ago

I don't think this is a good idea because it will cause a breaking change, it's hard to explain and can cause undesirable behaviors.

What do you think about this behavior, @superwf?

@henriq-88, can you link other libraries with same behavior or just improve this suggestion with some cases and improvements that can do?

alijaya commented 3 years ago

hello, it would be great if we could add an option in cache.dispatch if we really want to replace the cache (for example, we already know that the response should be changed because we have mutated in certain way). For now, it's kinda pain to duplicate the delete after each change.