当前位置:网站首页>小程序开发----自定义有效期缓存
小程序开发----自定义有效期缓存
2022-06-22 14:32:00 【影夜随风】
在uni-app中使用:
/**
* 缓存数据优化
* import cache from '@/common/js/cache'
* 使用方法 【
* 一、设置缓存
* string cache.put('k', 'string你好啊');
* json cache.put('k', { "b": "3" }, 2);
* array cache.put('k', [1, 2, 3]);
* boolean cache.put('k', true);
* 二、读取缓存
* 默认值 cache.get('k')
* string cache.get('k', '你好')
* json cache.get('k', { "a": "1" })
* 三、移除/清理
* 移除: cache.remove('k');
* 清理:cache.clear();
* 】
* @type {String}
*/
const postfix = '_msfkd'; // 缓存前缀
const expires = 1000*60*30;//设置缓存时间 ms
/**
* 设置缓存
* @param {[type]} k [键名]
* @param {[type]} v [键值]
* @param {[type]} t [时间、单位秒]
*/
function put(k, v, t) {
uni.setStorageSync(k, v)
let seconds =t;
if (seconds > 0) {
let timestamp = new Date().getTime();
if(k=="userInfo"){
uni.setStorageSync(k + postfix, seconds*expires + "")
}else{
let newTime = seconds*18000000+timestamp;//缓存时间
uni.setStorageSync(k + postfix, newTime + "")
}
} else {
uni.removeStorageSync(k + postfix)
}
}
/**
* 获取缓存
* @param {[type]} k [键名]
* @param {[type]} def [获取为空时默认]
*/
function get(k, def) {
let deadtime = parseInt(uni.getStorageSync(k + postfix))
if (deadtime) {
let date = new Date().getTime();
if ( date - deadtime >expires ) {//清空缓存
remove(k)
remove(k+postfix)
}else{
return uni.getStorageSync(k)
}
}else{
let res = uni.getStorageSync(k);
if (res) {
return res;
} else {
if (def == undefined || def == "") {
def = false;
}
return def;
}
}
}
function remove(k) {
uni.removeStorageSync(k);
uni.removeStorageSync(k + postfix);
}
export default {
put,
get,
remove,
}
支付宝原生小程序内:
// 优化缓存
// * 使用方法 【
// * 一、设置缓存
// * string cache.put('k', 'string你好啊');
// * json cache.put('k', { "b": "3" }, 2);
// * array cache.put('k', [1, 2, 3]);
// * boolean cache.put('k', true);
// * 二、读取缓存(可设置默认值),没有读取到缓存值,使用设置的默认值
// * 默认值 cache.get('k')
// * string cache.get('k', '你好')
// * json cache.get('k', { "a": "1" })
// * 三、移除/清理
// * 移除: cache.remove('k');
// * 清理:cache.clear();
// * 】
const postfix = "_msfkd";//缓存前缀
const expiress = 1000* 60*60;//设置缓存时间ms 默认一个小时
/**
*设置缓存
@param {[type]} k [键名]
@param {[type]} v [键值]
@param {[type]} t [时间、单位ms]
*/
function put(k,v,t){
my.setStorageSync({
key: k,
data: v
});
let seconds = t;
if(seconds>0){//如果存在t 设置缓存时间
my.setStorageSync({
key: k+postfix,
data: seconds*expiress
});
}else{//移除时间缓存
my.removeStorageSync({
key: k+postfix
});
}
}
/**
* 获取缓存 (可添加默认值)
* @param {[type]} k [键名]
* * @param {[type]} def [获取为空时默认]
*/
function get(k,def){
let deadtime = my.getStorageSync({
key: k+postfix
}).data;
deadtime = parseInt(deadtime);
if(deadtime){
let date = new Date().getTime();
if(date-deadtime>expiress){
remove(k)
}else{
return my.getStorageSync({
key:k
}).data;
}
}else{
let res = my.getStorageSync({
key:k
}).data;
if(res) return res;
if (def == undefined || def == "") {
def = false;
}
return def;
}
}
/**
* 移除缓存
* @param {*} k
*/
function remove(k) {
my.removeStorageSync(k);
my.removeStorageSync(k + postfix);
}
/**
* 删除全部缓存
*/
function clear(){
my.clearStorageSync();
}
export default {
put,
get,
remove,
clear
}边栏推荐
- 还可以这样搞nlp(分类)
- 【newman】postman生成漂亮的测试报告
- 向量6(继承)
- Exploration and practice of dewu app data simulation platform
- Database connection pool: Code Directory
- Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
- “软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
- Ml notes matrix fundamental, gradient descent
- FreeRTOS task priority and interrupt priority
- 米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
猜你喜欢

Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down

加密市场进入寒冬,是“天灾”还是“人祸”?

Once, I had 5 part-time jobs just to buy a new earring for my girlfriend

New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer

On the routing tree of gin

推进兼容适配,使能协同发展 GBase 5月适配速递

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

Verilog使用inout信号的方法

Tree structured binary tree

C# 实现插入排序
随机推荐
求求了,别被洗脑了,这才是90%中国人的生存实况
Scala语言学习-04-函数作为参数传入函数-函数作为返回值
专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
Is it difficult for flush to open an account? Is it safe to open an account online?
又可以这样搞nlp(分类)
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
基础版现在SQL分析查询不能用了吗?
大会倒计时 | 亚马逊云科技创新大会邀您一起构建AI新引擎 !
OOP 多重收纳(类模板)
After 17 years, Liu Yifei became popular again: ordinary people don't want to be eliminated, but they also need to understand this
高精度计算
js中const定义变量及for-of和for-in
Devsecops: best practices for ci/cd pipeline security
FreeRTOS task priority and interrupt priority
英国考虑基于国家安全因素让Arm在伦敦上市
乱解码nlp
小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效
Those confusing user state & kernel state
百行代码实现基于Redis的可靠延迟队列
Verilog使用inout信号的方法