欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」js代码如何设置缓存数据cache.js

作者 : 自学哈 本文共2459个字,预计阅读时间需要7分钟 2023-04-26 共115人阅读
也想出现在这里? 联系我们

js代码如何设置缓存数据cache.js

设置缓存数据js的源代码cache.js,可以在浏览器中使用js缓存数据,需要一点小伙伴可以看一下。

cache.js源代码如下:

$.extend($, {
    Cache : {
        userData: false,
        supportLocalStorage: typeof localStorage == 'object' ? true : false,
        name: location.hostname,
 
        init: function () {
            if ( $.Cache.supportLocalStorage )
                return false;
            if ( !$.Cache.userData ) {
                try {
                    $.Cache.userData = document.createElement('INPUT');
                    $.Cache.userData.type = "hidden";
                    $.Cache.userData.style.display = "none";
                    $.Cache.userData.addBehavior("#default#userData");
                    document.body.appendChild($.Cache.userData);
                    var expires = new Date();
                    expires.setDate(expires.getDate() + 365);
                    $.Cache.userData.expires = expires.toUTCString();
                } catch (e) {
                    return false;
                }
            }
            return true;
        },
 
        set: function (key, value, expire) {
            if ( typeof value == 'object' ) {
                value = JSON.stringify(value);
            }
            if ( expire == undefined )
                expire = 0;
 
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                $.Cache.userData.setAttribute(key, value);
                if ( expire > 0 ) {
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp + expire;
                    $.Cache.userData.setAttribute(key + "_EXPIRE", expiration);
                }
                $.Cache.userData.save($.Cache.name);
            } else {
                localStorage.setItem(key, value);
                if ( expire > 0 ) {
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp + expire;
                    localStorage.setItem(key + "_EXPIRE", expiration);
                }
            }
        },
 
        get: function (key) {
            var val;
            var timestamp = Date.parse(new Date());
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                val = $.Cache.userData.getAttribute(key);
                var expiration = $.Cache.userData.getAttribute(key + "_EXPIRE");
                if ( expiration != null && expiration != undefined && expiration > 0  ) {
                    if ( expiration < timestamp) {
                        $.Cache.userData.removeAttribute(key);
                        $.Cache.userData.removeAttribute(key + "_EXPIRE");
                        return undefined;
                    }
                }
            } else {
                val = localStorage.getItem(key);
                var expiration = localStorage.getItem(key + "_EXPIRE");
                if ( expiration != null && expiration != undefined && expiration > 0 ) {
                    if ( expiration < timestamp) {
                        localStorage.removeItem(key);
                        localStorage.removeItem(key + "_EXPIRE");
                        return undefined;
                    }
                }
            }
            if ( val == null || val == undefined || val == "" )
                return undefined;
            if ( val.indexOf("{") == 0 || val.indexOf("[") == 0 ) {
                return JSON.parse(val);
            }
            return val;
        },
        del : function(key) {
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                $.Cache.userData.removeAttribute(key);
                $.Cache.userData.removeAttribute(key + "_EXPIRE");
            } else {
                localStorage.removeItem(key);
                localStorage.removeItem(key + "_EXPIRE");
            }
        },
    }
});
 
使用方法演示:
 
$(function(){
var cacheCity = $.Cache.get('city');
        var cacheCity_id = $.Cache.get('city_id');
 
        if ( cacheCity ) {
            $("input[name=city]").val(cacheCity);
        }
 
        if(cacheCity_id){
            $("input[name=city_id]").val(cacheCity_id);
        }
}
 
缓存数据可直接赋值给input,使用起来还是非常方便的。

 

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」js代码如何设置缓存数据cache.js
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号