uniapp的缓存问题
2020/07/20
uniapp的本地缓存,分为同步缓存和异步缓存。个人比较推荐同步缓存,异步缓存会有稍许延迟。
同步缓存
存
try {
uni.setStorageSync('storage_key', 'hello');
} catch (e) {
// error
};
取
try {
const value = uni.getStorageSync('storage_key');
if (value) {
console.log(value);
}
} catch (e) {
// error
};
获取当前 storage 的相关信息
try {
const res = uni.getStorageInfoSync();
console.log(res.keys);
console.log(res.currentSize);
console.log(res.limitSize);
} catch (e) {
// error
};
移除指定key
try {
uni.removeStorageSync('storage_key');
} catch (e) {
// error
};
清除本地数据缓存
try {
uni.clearStorageSync();
} catch (e) {
// error
};
异步缓存
存
uni.setStorage({
key: 'storage_key',
data: 'hello',
success: function () { //非必填
console.log('success');
}
});
取
uni.getStorage({
key: 'storage_key',
success: function (res) { //非必填
console.log(res.data);
}
});
获取当前 storage 的相关信息
uni.getStorageInfo({
success: function (res) {
console.log(res.keys);
console.log(res.currentSize);
console.log(res.limitSize);
}
});
移除指定key
uni.removeStorage({
key: 'storage_key',
success: function (res) {
console.log('success');
}
});
清除本地数据缓存
uni.clearStorage();
此是个人总结,如果需要具体了解请到uniapp官方网站仔细查询。
https://uniapp.dcloud.io/api/storage/storage?id=setstorage