缓存设置(失效时间)

/*
* @desc 缓存
* @params
* preId {String, Number} 缓存前缀
* time {Number} 缓存时长
* @author niuyueyang
* @date 2020/08/05
* */
const BaseStorage = function (preId) {
    this.preId = preId || 'WEIBO-';
    this.time = -1;
    this.expression = '|-|'
}
BaseStorage.prototype = {
    status: {
        SUCCESS: 0,
        FAILURE: 1,
        OVERFLOW: 2,
        TIMEOUT: 3
    },
    storage: localStorage || window.localStorage,

    /*
    * @desc 删除cookie
    * @params
    * key {String} 缓存键值
    * value {String, Number, Array, Object} 缓存的值
    * t {Number} 缓存时长(单位:天),-1代表永久缓存
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    setCookie: function(key, value, t){
        var oDate = new Date();
        oDate.setDate(oDate.getDate()+t);
        if(t != -1){
            document.cookie=key+"="+encodeURIComponent(value)+";expires="+oDate.toUTCString();
        }
        else{
            document.cookie=key+"="+encodeURIComponent(value);
        }
        if(this.getCookie(key)){
            return true;
        }else{
            return false;
        }
    },

    /*
    * @desc 获取cookie
    * @params
    * key {String} 缓存键值
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    getCookie: function (key){
        var str = document.cookie.replace(/;\s*/,';');
        var cookieArr = str.split(';');
        var cookieObj = {};
        var len = cookieArr.length;
        for(var i = 0; i < len; i++){
            var item = cookieArr[i];
            var k = item.split('=')[0];
            var v = item.split('=')[1];
            cookieObj[k] = v;
        }
        if(cookieObj[key]){
            return decodeURIComponent(cookieObj[key]);
        }else{
            return false;
        }
    },

    /*
    * @desc 删除cookie
    * @params
    * key {String} 缓存键值
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    removeCookie: function (key){
        document.cookie = key+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        if(!this.getCookie(key)){
            return true;
        }else{
            return false;
        }
    },
    getKey: function(key){
        return this.preId + key;
    },

    /*
    * @desc 设置缓存
    * @params
    * type {String} 缓存类型 local session cookie
    * key {String} 缓存键值
    * val {Array, Object, String, Number} 要缓存的值
    * time {Number} 缓存时长(单位:天),-1代表永久缓存
    * cb {Function} 回调
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    set: function (type, key, val, time, cb) {
        var status = this.status.SUCCESS;
        key = this.getKey(key);
        if(type == 'local'){
            this.storage = localStorage || window.localStorage
        }
        else if(type == 'session'){
            this.storage = sessionStorage || window.sessionStorage
        }
        else{
            this.storage = document.cookie;
        }
        // 未设置失效时间,默认是一个月
        try{
            if(time != -1){
                this.time = new Date().getTime() + time * 24 * 60 * 1000 ;
            }
            else{
                this.time = -1
            }
        }
        catch(e){
            this.time = new Date().getTime() + 1000 * 24 * 60 * 60 * 31;
        }

        try {
            if(window.localStorage || window.sessionStorage){
                if(type == 'local' || type == 'session'){
                    if(typeof val != Object){
                        val = this.time != -1 ? val + '|-|'+this.time : val;
                        this.storage.setItem(key, val)
                    }
                    else{
                        val = this.time != -1 ? JSON.stringify(val) + '|-|'+this.time : val;
                        this.storage.setItem(key, val)
                    }
                }
                else{
                    this.setCookie(key, val, this.time)
                }
            }
            else{
                this.setCookie(key, val, this.time)
            }
        }
        catch(e){
            status = this.status.OVERFLOW;
        }
        cb && cb.call(this, status, key, val);
    },

    /*
    * @desc 获取缓存
    * @params
    * type {String} 缓存类型 local session cookie
    * key {String} 缓存键值
    * cb {Function} 回调
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    get: function (type, key, cb) {
        var status = this.status.SUCCESS;
        key = this.getKey(key);
        var value = '';
        var timeExpire = 0;

        if(type == 'local'){
            this.storage = localStorage || window.localStorage
        }
        else if(type == 'session'){
            this.storage = sessionStorage || window.sessionStorage
        }
        else{
            this.storage = document.cookie;
        }

        try{
            if(window.localStorage || window.sessionStorage){
                var index = this.storage.getItem(key).indexOf(this.expression);
                timeExpire = this.time != -1 ? Number(this.storage.getItem(key).slice(index + this.expression.length)) : -1
                if(type == 'local' || type == 'session'){
                    value = this.storage.getItem(key);
                }
                else{
                    value = this.getCookie(key)
                }
            }
            else{
                value = this.getCookie(key)
            }
        }
        catch(e){
            value = null;
            cb && cb.call(this, status, key, value);
            return value;
        }
        if(timeExpire == -1 || new Date().getTime() <= timeExpire){
            cb && cb.call(this, status, value);
        }
        else{
            value = null;
            status = this.status.TIMEOUT;
            this.remove(type, key)
        }
    },

    /*
    * @desc 删除缓存
    * @params
    * type {String} 缓存类型 local session cookie
    * key {String} 缓存键值
    * cb {Function} 回调
    * @author niuyueyang
    * @date 2020/08/05
    * @public
    * */
    remove: function (type, key, cb) {
        var status = this.status.FAILURE;
        key = this.getKey(key);
        if(type == 'local'){
            this.storage = localStorage || window.localStorage
        }
        else if(type == 'session'){
            this.storage = sessionStorage || window.sessionStorage
        }
        else{
            this.storage = document.cookie;
        }
        try{
            if(window.localStorage || window.sessionStorage){
                if(type == 'local' || type == 'session'){
                    this.storage.removeItem(key);
                }
                else{
                    this.removeCookie(key);
                }
            }
            else{
                this.removeCookie(key);
            }
            cb && cb.call(this, status, null);
        }catch(e){
            cb && cb.call(this, this.status.FAILURE);
        }
    }
}

你可能感兴趣的:(原生js)