js 简易缓存类
var tool = function(){
//设置缓存
function setCache(name, value) {
var storage = 'localStorage';
if(!window.localStorage){
storage = 'cookie';
}
if (settings === null) {
'localStorage' == storage ? window.localStorage.removeItem(name): delCookie(name);
} else {
'localStorage' == storage ? window.localStorage.setItem(name,settings): addCookie(name,settings);
}
}
//清空缓存
function clearCache(name) {
var storage = 'localStorage';
if(!window.localStorage){
storage = 'cookie';
}
if (name === null) {
'localStorage' == storage ? window.localStorage.clear(): clearAllCookie();
} else {
'localStorage' == storage ? window.localStorage.removeItem(name): delCookie(name);
}
return true;
}
//删除所有cookie
function clearAllCookie() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if(keys) {
for(var i = keys.length; i--;)
document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
}
return true;
}
//提取缓存
function getCache(name) {
var data = storage == 'localStorage'? window.localStorage.getItem(name): $.cookie.get(name);
if (typeof data == 'string') {
try {
var obj=JSON.parse(data);
if(typeof data == 'object' && data ){
data = obj;
}
} catch(e) {
///
}
}
return data;
}
/**
* 添加cookie
* @param objName
* @param objValue
* @param objHours
*/
function addCookie(objName, objValue, objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
return true;
}
/**
* 获取cookie
* @param objName
* @returns {string}
*/
function getCookie(objName){//获取指定名称的cookie的值
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0] == objName)
return unescape(temp[1]);
}
}
/**
* 移除cookie
* @param name
*/
function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}
//cache 方法 包含设置 删除 获取
function cache(name, settings,) {
name = name || 'cache';
var storage = 'localStorage';
if(!window.localStorage){
storage = 'cookie';
}
//先判断是不是删除
if(settings === null){
//删除缓存
storage == 'localStorage'? window.localStorage.removeItem(name): delCookie(name);
}
//判断是不是获取
if(!settings){
//获取缓存
var data = storage == 'localStorage'? window.localStorage.getItem(name): getCookie(name);
if (typeof data == 'string') {
try {
var obj=JSON.parse(data);
if(typeof data == 'object' && data ){
data = obj;
}
} catch(e) {
///
}
}
return data;
}
settings = typeof settings ==="object" ?JSON.stringify(settings):settings;
//开始写缓存
storage == 'localStorage'? window.localStorage.setItem(name,settings): addCookie(name,settings);
return true;
}
return {
// 页面加载动画
setCache : function (name, value) {
setCache(name, value)
},
getCache :function (name) {
getCache(name)
},
cache: function (name, settings){
return cache(name, settings);
}
};
}();
版权声明:
作者:超级管理员
链接:
https://apecloud.ltd/article/detail.html?id=96
来源:猿码云个人技术站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
打赏
![/static/admin/img/weixin.jpg](https://www.apecloud.ltd/static/admin/img/weixin.jpg)
![/static/admin/img/zfb.jpg](https://www.apecloud.ltd/static/admin/img/zfb.jpg)
共有0条评论