对查询到的列表做缓存

问题场景:

vue开发环境下,查询用户列表,带有分页功能,每次条页都要请求接口,产生没必要的请求,比如,查询完第一页然后查询第二页,再查询第一页,仍然要重新请求接口,造成资源浪费和客户端等待时间浪费,所以决定优化,增加对象对查询结果进行缓存。

由于开发用了vuex对状态进行管理,所以在store/index的state里定义了一个listCache对象用来缓存查询结果

    listCache:{
        userList:[]
    }

在列表查询页页码切换事件里添加代码

handleCurrentChange:function(val){// 控制当前页显示
                this.loading = true;
                if(this.listCache.userList[val]){
                    if(this.listCache.userList[val].length != 0){
                        this.currentPage = val
                        this.userList = []
                        this.userList = this.listCache.userList[val]
                        this.loading = false
                    }
                }else{
                    this.initUserList(val)
                }
            }

在每次改变form筛选条件重新查询列表时,会清空 this.listCache.userList

原文地址:https://www.cnblogs.com/cnundefined/p/cache.html