日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
面向?qū)ο缶幊?原生Javascript實現(xiàn)一個支持過期時間的DAO庫

本文主要解決原生localStorage無法設置過期時間的問題,并通過封裝,來實現(xiàn)一個操作便捷,功能強大的localStorage庫,關于庫封裝的一些基本思路和模式,我將采用之前寫的如何用不到200行代碼寫一款屬于自己的js類庫中類似的方法,感興趣的朋友可以學習,交流。

成都創(chuàng)新互聯(lián)主營芙蓉網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件開發(fā)公司,芙蓉h5微信平臺小程序開發(fā)搭建,芙蓉網(wǎng)站營銷推廣歡迎芙蓉等地區(qū)企業(yè)咨詢

設計思路

我們將基于localStorage原始api進行擴展,讓其支持失效時間,操作完成后的回調(diào)。在文章的最后,我將給出庫的完成代碼,接下來我們就一步步實現(xiàn)吧。

正文

首先,我們來設計庫的基本框架:

 
 
 
 
  1. const BaseStorage = function(preId, timeSign){ 
  2.   // 初始化一些操作 
  3.  
  4. BaseStorage.prototype = { 
  5.   storage: localStorage || window.localStorage, 
  6.   set: function(key, value, cb, time){ 
  7.      
  8.   }, 
  9.   get: function(key, cb){ 
  10.      
  11.   }, 
  12.   // 刪除storage,如果刪除成功,返回刪除的內(nèi)容 
  13.   remove: function(key, cb){ 
  14.     
  15.   } 

如上可以發(fā)現(xiàn),我們的storage會有三個核心api,分別為set,get,remove,我們使用localStorage作為基礎庫支持,當然你也可以將上面的庫換成sessionStorage或者其他。

有了基本骨架,我們就可以實現(xiàn)基本功能的封裝,這里我們先在原型中加一個屬性,來列出數(shù)據(jù)操作中的各個狀態(tài)。

 
 
 
 
  1. status: { 
  2.  SUCCESS: 0, // 成功 
  3.  FAILURE: 1, // 失敗 
  4.  OVERFLOW: 2, // 數(shù)據(jù)溢出 
  5.  TIMEOUT: 3  // 超時 
  6. }, 

為了實現(xiàn)過期時間,我們有兩種思路,第一種是先將一個過期時間存到storage中,每次操作都檢查一遍是否過期,但是這種方案意味著對不同的鍵就要設置不同的過期時間的storage與之對應,這樣會占用額外的庫內(nèi)存,維護起來也不方便。另一種方法就是將過期時間存放到鍵值中,將時間和值通過標識符分隔,每次取的時候從值中截取過期時間,再將真實的值取出來返回,這種方案不會添加額外的鍵值對存儲,維護起來也相對簡單,所以我們采用這種方案。 為了區(qū)分不同的庫對象,我們還可以添加鍵前綴,如下:

 
 
 
 
  1. const BaseLocalStorage = function(preId, timeSign){ 
  2.    this.preId = preId; // 鍵前綴 
  3.    this.timeSign = timeSign || '|-|';  // 過期時間和值的分隔符 
  4.  } 

基于這個思想,我們就可以接下來的實現(xiàn)了。

getKey——修飾key的方法,不影響用戶對真實key的影響

 
 
 
 
  1. getKey: function(key){ 
  2.      return this.preId + key 
  3.    }, 

set實現(xiàn)

 
 
 
 
  1. set: function(key, value, cb, time){ 
  2.      var status = this.status.SUCCESS, 
  3.      key = this.getKey(key); 
  4.      // 設置失效時間,未設置時間默認為一個月 
  5.      try{ 
  6.        time = new Date(time).getTime() || time.getTime(); 
  7.      }catch(e){ 
  8.        time = new Date().getTime() + 1000*60*60*24*31 
  9.      } 
  10.      try{ 
  11.        this.storage.setItem(key, time + this.timeSign + value); 
  12.      }catch(e){ 
  13.        status = this.status.OVERFLOW; 
  14.      } 
  15.      // 操作完成后的回調(diào) 
  16.      cb && cb.call(this, status, key, value) 
  17.    } 

get實現(xiàn)

 
 
 
 
  1. get: function(key, cb){ 
  2.      var status = this.status.SUCCESS, 
  3.      key = this.getKey(key), 
  4.      value = null, 
  5.      timeSignLen = this.timeSign.length, 
  6.      that = this, 
  7.      index, 
  8.      time, 
  9.      result; 
  10.      try{ 
  11.        value = that.storage.getItem(key); 
  12.      }catch(e){ 
  13.        result = { 
  14.          status: that.status.FAILURE, 
  15.          value: null 
  16.        } 
  17.        cb && cb.call(this, result.status, result.value); 
  18.        return result 
  19.      } 
  20.      if(value) { 
  21.        index = value.indexOf(that.timeSign); 
  22.        time = +value.slice(0, index); 
  23.        // 判斷是否過期,過期則清除 
  24.        if(time > new Date().getTime() || time == 0){ 
  25.          value = value.slice(index+timeSignLen); 
  26.        }else{ 
  27.          value = null, 
  28.          status = that.status.TIMEOUT; 
  29.          that.remove(key); 
  30.        } 
  31.      }else{ 
  32.        status = that.status.FAILURE; 
  33.      } 
  34.      result = { 
  35.        status: status, 
  36.        value: value 
  37.      }; 
  38.      cb && cb.call(this, result.status, result.value); 
  39.      return result 
  40.    } 

remove實現(xiàn)

 
 
 
 
  1. // 刪除storage,如果刪除成功,返回刪除的內(nèi)容 
  2.    remove: function(key, cb){ 
  3.      var status = this.status.FAILURE, 
  4.      key = this.getKey(key), 
  5.      value = null; 
  6.      try{ 
  7.        value = this.storage.getItem(key); 
  8.      }catch(e){ 
  9.        // dosomething 
  10.      } 
  11.      if(value){ 
  12.        try{ 
  13.          this.storage.removeItem(key); 
  14.          status = this.status.SUCCESS; 
  15.        }catch(e){ 
  16.          // dosomething 
  17.        } 
  18.      } 
  19.      cb && cb.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length)) 
  20.    } 

在api的實現(xiàn)過程中,由于某種誤操作很可能導致storage報錯,所以建議最好用trycatch包裹,這樣可以避免影響后面的邏輯。

接下來我們可以這么使用:

 
 
 
 
  1. let a = new BaseStorage('_', '@'); 
  2. a.set('name', '123') 
  3. a.get('name') // {status: 0, value: "123"} 
  4. // 設置失效時間 
  5. a.set('name', '123', null, new Date().getTime() + 1000*60*60*24*31) 
  6. // 移除 
  7. a.remove('name') 

完整源碼

 
 
 
 
  1. /** 
  2.  * 數(shù)據(jù)管理器 
  3.  */ 
  4. (function(win){ 
  5.   const BaseStorage = function(preId, timeSign){ 
  6.     this.preId = preId; 
  7.     this.timeSign = timeSign || '|-|'; 
  8.   } 
  9.   
  10.   BaseStorage.prototype = { 
  11.     status: { 
  12.       SUCCESS: 0, 
  13.       FAILURE: 1, 
  14.       OVERFLOW: 2, 
  15.       TIMEOUT: 3 
  16.     }, 
  17.     storage: localStorage || window.localStorage, 
  18.     getKey: function(key){ 
  19.       return this.preId + key 
  20.     }, 
  21.     set: function(key, value, cb, time){ 
  22.       var status = this.status.SUCCESS, 
  23.       key = this.getKey(key); 
  24.       // 設置失效時間,未設置時間默認為一個月 
  25.       try{ 
  26.         time = new Date(time).getTime() || time.getTime(); 
  27.       }catch(e){ 
  28.         time = new Date().getTime() + 1000*60*60*24*31 
  29.       } 
  30.       try{ 
  31.         this.storage.setItem(key, time + this.timeSign + value); 
  32.       }catch(e){ 
  33.         status = this.status.OVERFLOW; 
  34.       } 
  35.       cb && cb.call(this, status, key, value) 
  36.     }, 
  37.     get: function(key, cb){ 
  38.       var status = this.status.SUCCESS, 
  39.       key = this.getKey(key), 
  40.       value = null, 
  41.       timeSignLen = this.timeSign.length, 
  42.       that = this, 
  43.       index, 
  44.       time, 
  45.       result; 
  46.       try{ 
  47.         value = that.storage.getItem(key); 
  48.       }catch(e){ 
  49.         result = { 
  50.           status: that.status.FAILURE, 
  51.           value: null 
  52.         } 
  53.         cb && cb.call(this, result.status, result.value); 
  54.         return result 
  55.       } 
  56.       if(value) { 
  57.         index = value.indexOf(that.timeSign); 
  58.         time = +value.slice(0, index); 
  59.         if(time > new Date().getTime() || time == 0){ 
  60.           value = value.slice(index+timeSignLen); 
  61.         }else{ 
  62.           value = null, 
  63.           status = that.status.TIMEOUT; 
  64.           that.remove(key); 
  65.         } 
  66.       }else{ 
  67.         status = that.status.FAILURE; 
  68.       } 
  69.       result = { 
  70.         status: status, 
  71.         value: value 
  72.       }; 
  73.       cb && cb.call(this, result.status, result.value); 
  74.       return result 
  75.     }, 
  76.     // 刪除storage,如果刪除成功,返回刪除的內(nèi)容 
  77.     remove: function(key, cb){ 
  78.       var status = this.status.FAILURE, 
  79.       key = this.getKey(key), 
  80.       value = null; 
  81.       try{ 
  82.         value = this.storage.getItem(key); 
  83.       }catch(e){ 
  84.         // dosomething 
  85.       } 
  86.       if(value){ 
  87.         try{ 
  88.           this.storage.removeItem(key); 
  89.           status = this.status.SUCCESS; 
  90.         }catch(e){ 
  91.           // dosomething 
  92.         } 
  93.       } 
  94.       cb && cb.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length)) 
  95.     } 
  96.   } 
  97.   
  98.   win.BS = BaseStorage; 
  99. })(window) 
  100.   

本文標題:面向?qū)ο缶幊?原生Javascript實現(xiàn)一個支持過期時間的DAO庫
鏈接地址:http://m.5511xx.com/article/cddegse.html