zoukankan      html  css  js  c++  java
  • localstorge的缓存写法(超过一定时间自动清空)

    使用缓存: (设置缓存,尽量用大写,下划线的写法)

    const ls = {
        set: function (variable, value, ttl_ms) {
            var data = {value: value, expires_at:new Date(ttl_ms).getTime()};
            localStorage.setItem(variable.toString(), JSON.stringify(data));
        },
        get: function (variable) {
            var data = JSON.parse(localStorage.getItem(variable.toString()));
            if (data !== null) {
                debugger
                if (data.expires_at !== null && data.expires_at < new Date().getTime()) {
                    localStorage.removeItem(variable.toString());
                } else {
                    return data.value;
                }
            }
            return null;
        }
    };
    window.ls = ls;
    
    //使用方式
    let _CRM_COMMENT_GUIDE = ls.get("CRM_COMMENT_GUIDE");
    ls.set("CRM_COMMENT_GUIDE",true,deadLine);

      

  • 相关阅读:
    内置函数二
    通信的几个程序
    TCP协议和UDP协议
    异常处理
    logging模块
    网络编程一些概念
    hashlib
    序列化模块
    time,sys,os模块
    random模块
  • 原文地址:https://www.cnblogs.com/wanliyuan/p/7602394.html
Copyright © 2011-2022 走看看