zoukankan      html  css  js  c++  java
  • MUI

    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

  • 相关阅读:
    iOS UI基础05
    iOS UI基础04
    document.referrer
    节点
    特殊符号编码
    margin和padding的百分比
    XSS要点总结
    页面加载的过程
    函数声明和函数表达式
    jpg和png
  • 原文地址:https://www.cnblogs.com/phillyx/p/4761364.html
Copyright © 2011-2022 走看看