今天小程序添加了一个轮播图 把信息存储在缓存中 并加上缓存时间 防止后台修改了轮播图信息前台没有改变,直接上代码
//获取轮播图信息
getLunbo:function(e){
var timestamp = parseInt(new Date().getTime()/1000); //当前时间戳
var lunbo = wx.getStorageSync('lunbotu')
if (lunbo.length == 0) { //轮播图信息缓存中不存在
loginModel.getLunBoTu()
.then(res=>{
//获取到信息 直接放入 data 并设置进入缓存 和两个小时的过期时间
this.setData({
lunboData:res.data.msg
})
wx.setStorageSync('lunbotu', res.data.msg)
wx.setStorageSync('lunboTime', parseInt(timestamp + 7200))
})
} else { //已存在 要判断过期时间
var lunboTime = wx.getStorageSync("lunboTime") //过期时间
if (timestamp > lunboTime) { //已经过期了 从新获取数据
loginModel.getLunBoTu()
.then(res=>{
this.setData({
lunboData:res.data.msg
})
wx.setStorageSync('lunboTime', parseInt(timestamp + 7200))
})
} else { // 没有过期 直接设置data
this.setData({
lunboData:lunbo
})
}
}
},