找回密码
 立即注册
搜索

技术分享 storage 使用缓存策略

0
回复
2324
查看
[复制链接]

2

主题

3

帖子

25

积分

 楼主| 2019-9-23 10:00:13 显示全部楼层 |阅读模式
引用 import storage from '@system.storage' 事例代码:ctime缓存过期时间(分,天),DatList用户缓存数据 //返回格式化日期 yyyy/MM/dd HH:mm:ss utcToDate(date) { date = typeof (date) == "undefined" ? new Date() : date; var ndt; ndt = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); return ndt; } //格式化计算比较时间差 返回相差的秒数 Days(day1, day2, isday) { day2 = typeof (day2) == "undefined" ? this.utcToDate() : day2; day1 = day1.replace(" ", " "); day2 = day2.replace(" ", " "); var reg1 = /年|||\/|:| /; var reg2 = /年|||\/|:| /; if (day1.indexOf('-') != -1) reg1 = /-|-| |\/|:| /; else if (day1.indexOf('/') != -1) reg1 = /\/|\/| |\/|:| /; if (day2.indexOf('-') != -1) reg2 = /-|-| |\/|:| /; else if (day2.indexOf('/') != -1) reg2 = /\/|\/| |\/|:| /; //用正则分割 var DI1 = day1.split(reg1); var DI2 = day2.split(reg2); var date1 = new Date(DI1[0], DI1[1] * 1 - 1, DI1[2], this.sethour(DI1[3]), DI1[4], DI1[5]); var date2 = new Date(DI2[0], DI2[1] * 1 - 1, DI2[2], this.sethour(DI2[3]), DI2[4], DI2[5]); //用距标准时间差来获取相距时间 var minsec = Date.parse(date1) - Date.parse(date2); var days = minsec / 1000; //相差的秒数 return days; } //增加当前时间XX分钟 addTime(minute) { var date = new Date(new Date().getTime() + 60000 * minute); return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); } //增加当前时间XX天 addTimes(day) { var date = new Date(new Date().getTime() + 86400000 * day); return date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " 00:00:00"; } var DatList_ = { "ctime": addTimes(30), "List": DatList }; 缓存存储: storage.set({ key: "缓存KEY", value: DatList_ }) 缓存读取: LoadData fetch 读取数据 storage.get({ key: "缓存KEY", success: function (data) { if (data != '' && typeof data == "string") { var DatList = JSON.parse(data); if (typeof (DatList.ctime) != "undefined") { if (Days(DatList.ctime, utcToDate(), true) < 0) { mthis.LoadData(key); } else { mthis.InData = DatList.List; } } else mthis.LoadData(key); } else { mthis.LoadData(key); } }, fail: function (data, code) {} })
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册