MUI - 封裝localStorage與plus.storage


MUI - 封裝localStorage與plus.storage 2.0版本

在使用plus.storage頻繁地存取數據時,可以感覺到明顯的卡頓,而且很耗內存,
在切換到localstorage時雖然效率很高,頁面渲染速度明顯變快了,且手機發熱不明顯,不過又遇到了存儲瓶頸(一般<=5M),
因此折中采取了plus.storage與localStorage混合的方案:
	當localStorage達到存儲瓶頸時切換到plus.storage

封裝的方法基本上和plus.storage沒區別。關於plus.storage參考http://www.html5plus.org/doc/zh_cn/storage.html

接口

  1. getItem
    通過鍵key檢索獲取應用存儲的值

        var item=myStorage.getItem(key);
    
    • 參數key: DOMString必選
      存儲的鍵值
    • 返回值DOMString : 鍵key對應應用存儲的值,如果沒有則返回null
    • 說明:方法內部默認先從localStorage取值,沒有再從plus.Storage取值
  2. setItem
    修改或添加鍵值(key-value)對數據到應用數據存儲中

        void myStorage.setItem(key, value);
    
    • 說明:方法默認將數據存儲在localStorage中,超出localStorage容量限制則存到plus.storage
  3. getLength
    獲取localStorage中保存的鍵值對的個數

        var len=myStorage.getLength();
    
  4. getLengthPlus
    獲取plus.storage中保存的鍵值對的個數

  5. removeItem
    通過key值刪除鍵值對存儲的數據

        void myStorage.removeItem();
    
  6. clear
    清除應用所有的鍵值對存儲數據

        void myStorage.clear();
    
  7. key
    獲取localStorage鍵值對中指定索引值的key值

        var foo = myStorage.key(index);
    
  8. keyPlus
    獲取plus.storage鍵值對中指定索引值的key值

        var foo = myStorage.keyPlus(index);
    
  9. getItemByIndex
    通過鍵key檢索獲取應用存儲localStorage的值

        var item=myStorage.getItemByIndex(index);
    
    • 參數index: Number必選 存儲鍵值的索引
    • 返回值DOMString : 鍵key對應應用存儲的值,如果沒有則返回null
  10. getItemByIndexPlus
    通過鍵key檢索獲取應用存儲的值

        var item=myStorage.getItemByIndexPlus(index);
    
    • 參數index: Number必選 存儲鍵值的索引
    • 返回值DOMString : 鍵key對應應用存儲的值,如果沒有則返回null
  11. getItems
    通過鍵key檢索獲取應用存儲的值

        var items=myStorage.getItems(key)
    
    • 參數 key: Number可選 存儲鍵值的索引
    • 返回值Array:不傳key參則返回所有對象,否則返回含有該key的對象
  12. removeItemByKeys
    清除指定前綴的存儲對象

        void myStorage.removeItemBykeys(keys,cb)
    
    • 參數keysDOMStringArray, 必選 keysString,方法內部自動轉換為Array
    • 參數cbFunction 可選 回調函數

說明

以上方法經常用到的還是getItem setItem
getItems在測試或控制台查看時倒是偶爾用得到
removeItemBykeys是結合本地文件common.cache.clear緩存清除時一齊使用的


代碼已分享到github
地址在https://github.com/phillyx/MUIDemos/tree/master/js/myStorage.js
也可直接使用壓縮后的代碼https://github.com/phillyx/MUIDemos/tree/master/dist/common.js


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM