时间:2021-05-25
需求:
cache使用map来实现
ES6 模块与 CommonJS 模块的差异
因为esm输出的是值的引用,直接就是单例模式了
详细
export let cache = new Cache()版本1
思路:
注意: 在插件里面获取的mutations-type是包含命名空间的,而在actions里面则是没有命名空间,需要补全。
/mutation-types.js
/** * 需要缓存的数据会在mutations-type后面添加-CACHED */export const SET_HOME_INDEX = 'SET_HOME_INDEX-CACHED'/modules/home/index.jsconst actions = { /** * @description 如果有缓存,就返回把缓存的数据,传入mutations, * 没有缓存就从接口拿数据,存入缓存,把数据传入mutations */ async fetchAction ({commit}, {mutationType, fetchData, oPayload}) { // vuex开启了命名空间,所这里从cachekey要把命名空间前缀 + type + 把payload格式化成JSON const cacheKey = NAMESPACE + mutationType + JSON.stringify(oPayload) const cacheResponse = cache.get(cacheKey || '') if (!cacheResponse) { const [err, response] = await fetchData() if (err) { console.error(err, 'error in fetchAction') return false } commit(mutationType, {response: response, oPayload}) } else { console.log('已经进入缓存取数据!!!') commit(mutationType, {response: cacheResponse, oPayload}) } }, loadHomeData ({ dispatch, commit }) { dispatch( 'fetchAction', { mutationType: SET_HOME_INDEX, fetchData: api.index, } ) }}const mutations = { [SET_HOME_INDEX] (state, {response, oPayload}) {},}const state = { indexData: {}}export default { namespaced: NAMESPACED, actions, state, getters, mutations}编写插件,在这里拦截mutations,判断是否要缓存
/plugin/cache.js
store/index.js
import Vue from 'vue'import Vuex from 'vuex'import home from './modules/home'import cachePlugin from './plugins/cache'Vue.use(Vuex)const store = new Vuex.Store({ modules: { home, editActivity, editGuide } plugins: [cachePlugin]})export default store版本2
思路:直接包装fetch函数,在里面里面判断是否需要缓存,缓存是否超时。
优化点:
/actions.js
const actions = { async loadHomeData ({ dispatch, commit }, oPayload) { commit(SET_HOME_LOADSTATUS) const [err, response] = await fetchOrCache({ api: api.index, queryArr: oPayload.queryArr, mutationType: SET_HOME_INDEX }) if (err) { console.log(err, 'loadHomeData error') return [err, response] } commit(SET_HOME_INDEX, { response }) return [err, response] }}在fetchOrCache判断是需要缓存,还是请求接口
/** * 用这个函数就说明是需要进入缓存 * @param {*} api 请求的接口 * @param {*} queryArr 请求的参数 * @param {*} mutationType 传入mutationType作为cache的key值 */export async function fetchOrCache ({api, queryArr, mutationType, diff}) { // 这里是请求接口 const fetch = httpGet(api, queryArr) const cachekey = `${mutationType}:${JSON.stringify(queryArr)}` if (cache.has(cachekey)) { const obj = cache.get(cachekey) if (cacheFresh(obj.cacheTimestemp, diff)) { return cloneDeep(obj) } else { // 超时就删除 cache.delete(cachekey) } } // 不取缓存的处理 let response = await fetch() // 时间戳绑定在数组的属性上 response.cacheTimestemp = Date.now() cache.set(cachekey, response) // 返回cloneDeep的对象 return cloneDeep(response)}/** * 判断缓存是否失效 * @param {*} diff 失效时间差,默认15分钟=900s */const cacheFresh = (cacheTimestemp, diff = 900) => { if (cacheTimestemp) { return ((Date.now() - cacheTimestemp) / 1000) <= diff } else { return true }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
vuex-persistedstate核心原理:在本地存储中存入所有的vuex数据,页面刷新时到缓存中取数据,放到vuex中下载:$npminstallvuex
vuex数据改变,组件中页面不渲染相信许多vuex新手都会遇到这样的问题:vuex数据更新后,插件中使用数据的地方没有更新这样的代码data(){return{
解决vuex页面刷新导致数据丢失问题vuex数据是存在内存当中,当页面刷新之后vuex数据自然会丢失。我们有两种方法解决该问题:1.使用vuex-along2.
vuex与super-vuexsuper-vuex是一套用于简化Vuex的数据架构。适用场景在繁重的中后台项目中,引入vuex的作用如下:全局数据共享,组件数据
一、使用Vuex的目的实现多组件状态管理。多个组件之间需要数据共享时,Vuex是个很好的帮手哦二、Vuex的五大核心其中state和mutation是必须的,其