PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    封装localStorage与plus.storage 2.0版本

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:在使用plus.storage频繁地存取数据时,可以感觉到明显的卡顿,而且很耗内存,在切换到localstorage时虽然效率很高,页面渲染速度明显变快了...
    在使用plus.storage频繁地存取数据时,可以感觉到明显的卡顿,而且很耗内存,
    在切换到localstorage时虽然效率很高,页面渲染速度明显变快了,且手机发热不明显,不过又遇到了存储瓶颈(一般<=5M),
    因此折中采取了plus.storage与localStorage混合的方案:
    当localStorage达到存储瓶颈时切换到plus.storage

    1. (function(com, mui) { 
    2.                 var myStorage = {}; 
    3.                 var first=null
    4.                 function getItem(key) { 
    5.                     var jsonStr = window.localStorage.getItem(key.toString()); 
    6.                     return jsonStr ? JSON.parse(jsonStr).data : null
    7.                 }; 
    8.  
    9.                 function getItemPlus(key) { 
    10.                     var jsonStr = plus.storage.getItem(key.toString()); 
    11.                     console.log(new Date().getTime()-first); 
    12.                     return jsonStr ? JSON.parse(jsonStr).data : null
    13.                 }; 
    14.                 myStorage.getItem = function(key) { 
    15.                     first=new Date().getTime(); 
    16.                     return getItem(key) || getItemPlus(key); 
    17.                 }; 
    18.                 myStorage.setItem = function(key, value) { 
    19.                     first=new Date().getTime(); 
    20.                     value = JSON.stringify({ 
    21.                         data: value 
    22.                     }); 
    23.                     key=key.toString(); 
    24.                     try { 
    25.                          window.localStorage.setItem(key, value); 
    26.                     } catch (e) { 
    27.                         console.log(e); 
    28.                         //TODO 超出localstorage容量限制则存到plus.storage中 
    29.                         //且删除localStorage重复的数据www.bcty365.com
    30.                         removeItem(key); 
    31.                         plus.storage.setItem(key, value); 
    32.                     } 
    33.                     console.log(new Date().getTime()-first); 
    34.                 }; 
    35.  
    36.                 function getLength() { 
    37.                     return window.localStorage.length; 
    38.                 }; 
    39.                 myStorage.getLength = getLength; 
    40.  
    41.                 function getLengthPlus() { 
    42.                     return plus.storage.getLength(); 
    43.                 }; 
    44.                 myStorage.getLengthPlus = getLengthPlus; 
    45.  
    46.                 function removeItem(key) { 
    47.                     return window.localStorage.removeItem(key); 
    48.                 }; 
    49.  
    50.                 function removeItemPlus(key) { 
    51.                     return plus.storage.removeItem(key); 
    52.                 }; 
    53.                 myStorage.removeItem = function(key) { 
    54.                     window.localStorage.removeItem(key); 
    55.                     return plus.storage.removeItem(key); 
    56.                 } 
    57.                 myStorage.clear = function() { 
    58.                     window.localStorage.clear(); 
    59.                     return plus.storage.clear(); 
    60.                 }; 
    61.  
    62.                 function key(index) { 
    63.                     return window.localStorage.key(index); 
    64.                 }; 
    65.                 myStorage.key = key; 
    66.  
    67.                 function keyPlus(index) { 
    68.                     return plus.storage.key(index); 
    69.                 }; 
    70.                 myStorage.keyPlus = keyPlus; 
    71.  
    72.                 function getItemByIndex(index) { 
    73.                     var item = { 
    74.                         keyname: ''
    75.                         keyvalue: '' 
    76.                     }; 
    77.                     item.keyname = key(index); 
    78.                     item.keyvalue = getItem(item.keyname); 
    79.                     return item; 
    80.                 }; 
    81.                 myStorage.getItemByIndex = getItemByIndex; 
    82.  
    83.                 function getItemByIndexPlus(index) { 
    84.                     var item = { 
    85.                         keyname: ''
    86.                         keyvalue: '' 
    87.                     }; 
    88.                     item.keyname = keyPlus(index); 
    89.                     item.keyvalue = getItemPlus(item.keyname); 
    90.                     return item; 
    91.                 }; 
    92.                 myStorage.getItemByIndexPlus = getItemByIndexPlus; 
    93.                 /** 
    94.                  * @author liuyf 2015-05-04 
    95.                  * @description 获取所有存储对象 
    96.                  * @param {Object} key 可选,不传参则返回所有对象,否则返回含有该key的对象 
    97.                  */ 
    98.                 myStorage.getItems = function(key) { 
    99.                     var items = []; 
    100.                     var numKeys = getLength(); 
    101.                     var numKeysPlus = getLengthPlus(); 
    102.                     var i = 0; 
    103.                     if (key) { 
    104.                         for (; i < numKeys; i++) { 
    105.                             if (key(i).toString().indexOf(key) != -1) { 
    106.                                 items.push(getItemByIndex(i)); 
    107.                             } 
    108.                         } 
    109.                         for (i = 0; i < numKeysPlus; i++) { 
    110.                             if (keyPlus(i).toString().indexOf(key) != -1) { 
    111.                                 items.push(getItemByIndexPlus(i)); 
    112.                             } 
    113.                         } 
    114.                     } else { 
    115.                         for (i = 0; i < numKeys; i++) { 
    116.                             items.push(getItemByIndex(i)); 
    117.                         } 
    118.                         for (i = 0; i < numKeysPlus; i++) { 
    119.                             items.push(getItemByIndexPlus(i)); 
    120.                         } 
    121.                     } 
    122.                     return items; 
    123.                 }; 
    124.                 /** 
    125.                  * @description 清除指定前缀的存储对象 
    126.                  * @param {Object} keys 
    127.                  * @default ["filePathCache_","ajax_cache_"] 
    128.                  * @author liuyf 2015-07-21 
    129.                  */ 
    130.                 myStorage.removeItemByKeys = function(keys, cb) { 
    131.                     if (typeof(keys) === "string") { 
    132.                         keys = [keys]; 
    133.                     } 
    134.                     keys = keys || ["filePathCache_""ajax_cache_""Wedding""wedding"]; 
    135.                     var numKeys = getLength(); 
    136.                     var numKeysPlus = getLengthPlus(); 
    137.                     //TODO plus.storage是线性存储的,从后向前删除是可以的  
    138.                     //稳妥的方案是将查询到的items,存到临时数组中,再删除   
    139.                     var tmpks = []; 
    140.                     var tk, 
    141.                         i = numKeys - 1; 
    142.                     for (; i >= 0; i--) { 
    143.                         tk = key(i); 
    144.                         Array.prototype.forEach.call(keys, function(k, index, arr) { 
    145.                             if (tk.toString().indexOf(k) != -1) { 
    146.                                 tmpks.push(tk); 
    147.                             } 
    148.                         }); 
    149.                     } 
    150.                     tmpks.forEach(function(k) { 
    151.                         removeItem(k); 
    152.                     }); 
    153.                     for (i = numKeysPlus - 1; i >= 0; i--) { 
    154.                         tk = keyPlus(i); 
    155.                         Array.prototype.forEach.call(keys, function(k, index, arr) { 
    156.                             if (tk.toString().indexOf(k) != -1) { 
    157.                                 tmpks.push(tk); 
    158.                             } 
    159.                         }); 
    160.                     } 
    161.                     tmpks.forEach(function(k) { 
    162.                         removeItemPlus(k); 
    163.                     }) 
    164.                     cb && cb(); 
    165.                 }; 
    166.                 com.myStorage = myStorage; 
    167.                 window.myStorage = myStorage; 
    168.             }(common = {}, mui)); 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-2793-1.html
    相关热词搜索: