小程序开发----自定义有效期缓存

 在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
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风小薇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值