网站首页lua/js

封装个localstorage的方法接口,可设置localstorage过期时间

发布时间:2020-10-29 12:27:54编辑:slayer.hover阅读(247)

    让localstorage可以直接存取json对象,并可设置key过期时间(单位秒,默认时间3600秒)


    const storage = {
        set(key, data, time) {
            let obj = {
                data,
                createTime: Date.now(),
                lifeTime: time*1000 || 60 * 60 * 1000 //默认设置过期时间一个小时
            };
            window.localStorage.setItem(key, JSON.stringify(obj));
        },
        get(key) {
            let obj = JSON.parse(window.localStorage.getItem(key));
            let {data,createTime,lifeTime}=obj
            let getTime = Date.now();
            if (getTime - createTime >= lifeTime) {
                window.localStorage.removeItem(key);
                return null;
            } else {
                return data;
            }
        },
        ttl(key){
            if(this.exists(key)){
                let obj = JSON.parse(window.localStorage.getItem(key));
                let {data,createTime,lifeTime}=obj
                let getTime = Date.now();
                return Math.round((lifeTime + createTime - getTime)/1000);
            }else{
                return -1;
            }
        },
        keys(key){
            if(key){
                if(this.exists(key)) {
                    return Object.keys(this.get(key));
                }else{
                    return null;
                }
            }else {
                return Object.keys(localStorage);
            }
        },
        exists(key){
            return window.localStorage.hasOwnProperty(key);
        },
        del(key){
            window.localStorage.removeItem(key);
        },
        add(key, obj){
            if(this.exists(key)){
                let data = this.get(key);
                if(data) {
                    let newObj = Object.assign(data, obj);
                    this.set(key, newObj, this.ttl(key));
                }
            }
        },
        edit(key, innerKey, innerVal){
            if(this.exists(key)){
                let data = this.get(key);
                if(data.hasOwnProperty(innerKey)) {
                    data[innerKey] = innerVal;
                    this.set(key, data, this.ttl(key));
                }
            }
        },
        remove(key, innerKey){
            if(this.exists(key)){
                let data = this.get(key);
                if(data.hasOwnProperty(innerKey)) {
                    delete data[innerKey];
                    this.set(key, data, this.ttl(key));
                }
            }
        },
        clear(){
            window.localStorage.clear();
        }
    };


    ----------------------------------------------------------用例----------------------------------------------------------


    存:    

        storage.set('item',  {id:106,  name:'books',  num: 500},  300);

    取:    

        storage.get('item').name;

    获取所有key:

        storage.keys();

    获取key的剩余时间:

        storage.ttl('item');

    判断key是否存在:

        storage.exists('item');

    删除key:

        storage.del('item');

    为key添加数据:

        storage.add('item', {color:'red'});

    修改key的一个对象

        storage.edit('item', {color:'green'});

    删除key的一个对象:

        storage.remove('item',  'color');

    清空storage:

        storage.clear();

评论