基于jQuery的两种数据缓存方式(cache和icache)

cache

/**
* cache.
* page data cache in cache.
*/
(function($) {
$.cache = {};
$.extend($.cache, {
    map : {},
    push : function(key, value) {
    $.cache.map[key] = value;
},
remove : function(key) {
    delete($.cache.map[key]);
},
clear : function() {
    $.cache.map = {};
},
get : function(key) {
    return $.cache.map[key];
}
});
})(jQuery);

icache

/**
 * icache.
* page data cache in dom. 
*/
(function($) {
    $.icache = {};
    $.extend($.icache, {
     validStr : function(str) {
     return typeof(str) == 'string' ? true : false;
 },
 data : {
     containerId :'icacheContainer'
 },
 enable : function() {
     if ($('#' + $.icache.data.containerId).length != 0) return;
     var container = $('<div>').attr('id', $.icache.data.containerId).hide();
     $('body').append(container);
 },
 getContainer : function() {
     $.icache.enable();
     return $('#' + $.icache.data.containerId);
 },
 push : function(key, value) {
     if (!$.icache.validStr(key) || !$.icache.validStr(value)) return;
     var container = $.icache.getContainer();
     var e = container.find('#' + key);
     if (e.length == 0) e = $('<div>').attr('id', key).appendTo(container);
     e.html(value);
 },
 get : function(key) {
     return $.icache.getContainer().find('#' + key).html();
 },
 remove : function(key) {
     $.icache.getContainer().find('#' + key).remove();
 },
 clear : function() {
     $.icache.getContainer().empty();
 }
});
})(jQuery);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容