网络编程 发布日期:2025/11/9 浏览次数:1
关于本地缓存
1.wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以对本地缓存进行设置、获取和清理。本地缓存最大为10MB
2.localStorage 是永久存储
相应的api----------
wx.setStorage(OBJECT)
wx.getStorage(OBJECT)
wx.getStorageInfo(OBJECT)
wx.removeStorage(OBJECT)
wx.setStorageSync(KEY,DATA)
wx.getStorageSync(KEY)
wx.getStorageInfoSync
wx.clearStorage()
wx.clearStorageSync() ...
Api的具体使用详见官方文档
微信小程序缓存过期时间
/*
*创建缓存
* @param e 缓存名称
* @param t 缓存值
* @param i 缓存时间
*/
setCache: function(e, t, i) {
var n = +new Date / 1000,
a = true,
o = {
expire: i "";
i = parseInt(i);
try {
n = wx.getStorageSync(e), n.expire > i || 0 == n.expire "", this.removeCache(e))
} catch (e) {
n = void 0 === t "" : t
}
return n = n || ""
},
/*
*清除缓存
* @param e 缓存名称
*/
removeCache: function(e) {
var t = true;
try {
wx.removeStorageSync(e)
} catch (e) {
t = false
}
return t
},
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。