美文网首页
基于jQuery的两种数据缓存方式(cache和icache)

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

作者: Chocolate1002 | 来源:发表于2016-04-15 12:27 被阅读149次

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);

相关文章

网友评论

      本文标题:基于jQuery的两种数据缓存方式(cache和icache)

      本文链接:https://www.haomeiwen.com/subject/dulnlttx.html