localStorage缓存的工具类

define(function(require) {
require(“./context_holder”);
var util = require(“ UI/system/lib/base/util);varjustep=require( U I / s y s t e m / l i b / b a s e / u t i l ” ) ; v a r j u s t e p = r e q u i r e ( “ UI/system/lib/justep”);

var STCTools = {};

/**
 * 添加到缓存
 * 
 */
STCTools.addToCache = function(prefix,cacheName,value){

    util.setGlobalValue(prefix+"_"+cacheName,value);
};

/**
 * 从缓存的中读取
 */
STCTools.getValueFromCache = function(prefix,cacheName){
    return util.getGlobalValue(prefix+"_"+cacheName);
};

/**
 * 保存到数据库(localStorage实现)
 */
STCTools.save2DB = function(key,value){

    var storage = window.localStorage;
    if(storage && value){
        try{
            storage.setItem(key, JSON.stringify(value));
        }catch(e){
            console.log(e);
        }
    }
};

/**
 * 从数据中删除(localStorage实现)
 */
STCTools.deleteFromDB = function(key){
    var storage=window.localStorage;
    if(storage){
        storage.removeItem(key);
    }
};

/**
 * 从数据中读取(localStorage实现)
 */
STCTools.getFromDB = function(key){

    var storage=window.localStorage;
    var result = storage.getItem(key);

    try{
        return result?$.parseJSON(result):null;
    }catch(e){
        console.log(e);
        return null;
    }
};

window.STCTools = STCTools;
return STCTools;

});

你可能感兴趣的:(前端)