Skip to content

Commit

Permalink
limit client store item cache to 3 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 29, 2016
1 parent 0e2b0a1 commit ade06c6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/store/index.js
Expand Up @@ -36,6 +36,19 @@ const store = new Vuex.Store({
}, },


FETCH_ITEMS: ({ commit, state }, { ids }) => { FETCH_ITEMS: ({ commit, state }, { ids }) => {
// on the client, the store itself serves as a cache.
// only fetch items that we do not already have, or has expired (3 minutes)
const now = Date.now()
ids = ids.filter(id => {
const item = state.items[id]
if (!item) {
return true
}
if (now - item.__lastUpdated > 1000 * 60 * 3) {
return true
}
return false
})
if (ids.length) { if (ids.length) {
return fetchItems(ids).then(items => commit('SET_ITEMS', { items })) return fetchItems(ids).then(items => commit('SET_ITEMS', { items }))
} else { } else {
Expand Down

0 comments on commit ade06c6

Please sign in to comment.