/*
* @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)) : -1if(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);}}
}

缓存设置(失效时间)相关推荐

  1. java中怎么给redis缓存设置失效时间

    //参数分别是key,value,时间,时间单位,这里表示缓存的这个键值对3600s后失效 redisTemplate.opsForValue().set("key", value ...

  2. redis缓存失效时间设为多少_java操作Redis缓存设置过期时间的方法

    关于Redis的概念和应用本文就不再详解了,说一下怎么在java应用中设置过期时间. 在应用中我们会需要使用redis设置过期时间,比如单点登录中我们需要随机生成一个token作为key,将用户的信息 ...

  3. Redis(设置失效时间,RedisDesktopManger远程管理工具)

    Redis 设置失效时间 RedisDesktopManager工具 设置失效时间 有时候我们并不希望 redis 的 key 一直存在.例如缓存,验证码等数据,我们希 望它们能在一定时间内自动的被销 ...

  4. JavaScript中Cookie的使用——设置失效时间

    1.什么是Cookie? 1.1简介 主要用于存储访问过的网站数据,存储浏览器的信息到本地计算机中,用于客户端和服务器端的通讯 Cookie 是为了解决"如何记住用户信息"而发明的 ...

  5. Nginx缓存以及反向代理缓存设置

    expires指令 (1).expires [time] server {listen 90;server_name localhost;location /czj {root /home;expir ...

  6. Nginx缓存设置教程

    这篇文章主要介绍了Nginx缓存设置案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下 在开发调试web的时候,经常会碰到因浏览器缓存(cache)而 ...

  7. Linux系统下的Nginx的缓存设置、压缩配置和自动列目录配置

    (本文内所有的centos系统命令均使用斜体加粗表示,以便各位阅读) 注意!本人使用的是Lnmp脚本模式安装的Nginx 1.8.0稳定版,如果是从官网下载的版本,nginx.conf的配置文件有些不 ...

  8. nginx 缓存设置

    浏览器缓存原理 浏览器缓存 HTTP协议定义的缓存机制(如:Expires:Cache-control等) 2.浏览器无缓存 3.客户端有缓存 校验过期机制 校验是否过期                ...

  9. php 静态扩展,thinkphp5行为扩展实现html静态缓存设置

    thinkphp5行为扩展实现html静态缓存设置 2018-06-19 11:53:10ThinkPHP thnkphp5行为扩展html静态缓存 利用钩子thinkphp钩子进行行为扩展.先上本地 ...

  10. nginx 压缩和缓存设置

    nginx js和jpg图片缓存设置 server { listen       80; server_name  localhost; index index.htm index.html; roo ...

最新文章

  1. python程序员工资低吗-程序员嫌工资低拒绝offer,HR:估计你一辈子就是个程序员...
  2. windows server 2012多用户用一个账号同时登陆
  3. 第十届蓝桥杯java B组—试题C 数列求值
  4. ITK:写一个TIFF图像
  5. bootstrap-导航
  6. 《Java编码指南:编写安全可靠程序的75条建议》—— 指南16:避免授予过多特权...
  7. 第4章 Selenium2-java WebDriver API (三)
  8. 使用阿富汗和巴基斯坦地区的SRTM数据生成山体阴影和彩色地形图
  9. mootools LightBox
  10. 《数字图像处理与机器视觉——Visual C++与Matlab实现》——0.2 数字图像处理与识别...
  11. jsjavaScriptDate的时间格式转换,直接粘贴就可以使用
  12. VB 显示当前时间 24小时制
  13. oracle crystall ball,Oracle Crystal Ball下载
  14. docx4j学习笔记
  15. SPSS篇—卡方检验
  16. Alsa 调试下篇:应用篇
  17. 年薪90万的阿里p7和副处级干部选哪个?
  18. 期货市场亏了怎么自救?
  19. [EXCEL] 宏的录制、调用和删除
  20. RTMP、RTSP、m3u8、flv 区别及含义

热门文章

  1. centos7安装并使用supervisor管理服务队列
  2. 内网ip和外网ip区别
  3. 开始学习机器学习之前你必须要了解的知识有哪些?机器学习系列入门篇
  4. 台式计算机有无线网卡吗,台式机无线网卡怎么用?图解在这自己收藏
  5. 量子计算机和量子纠缠的关系,科普:什么是量子纠缠和量子计算?
  6. monkeyrunner之环境搭建及实例(三)
  7. 青铜时代 —— 相机算法
  8. 打坐是开发潜能的快速方法
  9. [Java聊天室服务器]实战之六 去除死链接
  10. 什么叫反向链接?什么是死链接?什么是错误链接?