開發(fa)客戶端時常會用到(dao)一(yi)些非永久(jiu)緩(huan)(huan)(huan)(huan)存(cun)(cun)的需(xu)求,而(er)微(wei)信(xin)緩(huan)(huan)(huan)(huan)存(cun)(cun)只(zhi)能(neng)按key永久(jiu)緩(huan)(huan)(huan)(huan)存(cun)(cun);其(qi)次,當讀取(qu)緩(huan)(huan)(huan)(huan)存(cun)(cun)失敗(bai)時,有時需(xu)要給個默認(ren)值,所以(yi)得二(er)次判斷下,今天想了一(yi)下何不封(feng)裝一(yi)個緩(huan)(huan)(huan)(huan)存(cun)(cun)框架。 ...
開發(fa)客戶(hu)端(duan)時(shi)常會用到一些非永久(jiu)緩存(cun)的需(xu)求(qiu),而微信緩存(cun)只能按key永久(jiu)緩存(cun);其(qi)次,當讀(du)取緩存(cun)失敗時(shi),有(you)時(shi)需(xu)要給(gei)個默認值,所以(yi)得二次判(pan)斷下(xia),今天想了(le)一下(xia)何不封裝(zhuang)一個緩存(cun)框架。
put(k, v, t)
k為key,v為具體內容(支持字(zi)符(fu)串、json、數組、boolean等等),t為可(ke)選參數表示有(you)效時(shi)間(單(dan)位:秒)
如存(cun)儲k為123過期時(shi)間1秒(miao),則(ze)調(diao)用put('k', '123', 1)方法;若永久存(cun)儲調(diao)用put('k', '123')
永(yong)久保存json:put('k', {"a":"1"}),數組、boolean等同理。
get(k, def)
k為(wei)key,def為(wei)可(ke)選參數,表示(shi)無緩存數據時返回值(支(zhi)持(chi)字符串、json、數組、boolean等等)
如(ru)讀取k緩存,則調用get('k');若想要無緩存時,返(fan)回默認值則get('k','默認值'),支(zhi)持各個(ge)數(shu)據(ju)類型(xing)。
remove(k)
移除某個key
clear()
清空所有key
其他方法
使用wx原生的(de)即可。

put(k, v, t)function put(k, v, t) {// console.log(k);wx.setStorageSync(k, v)var seconds = parseInt(t);if (seconds > 0) {var timestamp = Date.parse(new Date());timestamp = timestamp / 1000 + seconds;// console.log(timestamp);wx.setStorageSync(k + postfix, timestamp + "")} else {wx.removeStorageSync(k + postfix)}}先存儲(chu)key的數據(字(zi)符(fu)串、數組(zu)、json),再判斷過期(qi)時(shi)(shi)間是否大于0,當大于0時(shi)(shi),存儲(chu)key+一個(ge)后綴,內容為當前時(shi)(shi)間戳(chuo)(單(dan)位秒)+有(you)效時(shi)(shi)間t。
get(k, def)
function get(k, def) {var deadtime = parseInt(wx.getStorageSync(k + postfix))if (deadtime) {if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {if (def) { return def; } else { return; }}}var res = wx.getStorageSync(k);if (res) {return res;} else {return def;}}get方法,先通過(guo)key+一個后綴得到(dao)時(shi)間(jian)戳,如果時(shi)間(jian)戳存(cun)(cun)在(zai),切小于當前時(shi)間(jian),說(shuo)明(ming)過(guo)期;【那么,當有默(mo)認值(zhi)(zhi)時(shi)返(fan)回(hui)默(mo)認值(zhi)(zhi)(包(bao)含字符串(chuan)、數組、json),否(fou)則返(fan)回(hui)空。】當沒(mei)過(guo)期時(shi),正常(chang)讀取key的(de)內(nei)容(rong),key存(cun)(cun)在(zai)時(shi)正常(chang)返(fan)回(hui);否(fou)則返(fan)回(hui)默(mo)認值(zhi)(zhi),當不存(cun)(cun)在(zai)默(mo)認值(zhi)(zhi)時(shi)返(fan)回(hui)空。
其他方法
function remove(k) {wx.removeStorageSync(k);wx.removeStorageSync(k + postfix);}function clear() {wx.clearStorageSync();}remove(k)需要移除2個key,因為存儲的時候(hou),可能存了時間戳,當(dang)然即使(shi)不(bu)存在key,移除也(ye)是不(bu)會報錯的。
下(xia)載src文(wen)件(jian)夾內wCache.js文(wen)件(jian) 需要使用的(de)js文(wen)件(jian)頭加入var wc = require('../../src/wcache.js')。 var s=wc.get('k', '你好(hao)')、wc.put('k', 'string你好(hao)啊')等; 使用例子
get(e) {this.setData({text: null});switch (e.currentTarget.dataset.type) {case "def":this.setData({text: wc.get('k')});break;case "string":this.setData({text: wc.get('k', '你(ni)好')});break;case "json":this.setData({text: wc.get('k', { "a": "1" })});break;}}put(e) {console.log(e);switch (e.currentTarget.dataset.type) {case "string":wc.put('k', 'string你(ni)好啊');break;case "json":wc.put('k', { "b": "3" }, 2);break;case "list":wc.put('k', [1, 2, 3]);break;case "boolean":wc.put('k', true);break;}wx.showToast({title: '存儲成功',duration: 500,})}