博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单实用的ajax缓存类
阅读量:3557 次
发布时间:2019-05-20

本文共 1267 字,大约阅读时间需要 4 分钟。

工作中经常使用的,其实也可以把get和save改成其他方式如cookie、sessionStorage等

//缓存类function cacheHandler() {	this.data = {};	this.lifeTime = 3600*24; //默认缓存一天	this.setLifeTime = function(lifeTime) {		this.lifeTime = lifeTime;	};	this.save = function(name, value, lifeTime) {		this.data[name] = {			expire: new Date().getSeconds() + (parseInt(lifeTime) || this.lifeTime),			value: value		};		return this.data[name]['value'];	};	this.get = function(name) {		if (this.data[name] == undefined || this.data[name]['expire'] < new Date().getSeconds()) return undefined;		return this.data[name]['value'];	};	this.getOrSave = function(name, value, lifeTime) {		return this.get(name) == undefined && this.save(name, value, lifeTime);	};	this.clear = function(name) {		name ? delete this.data[name] : this.data = {};	};}//测试var cache=new cacheHandler();//实例化个全局缓存对象$(document).ready(function(){	$("#data").click(function(){		var cache_data=cache.get("cache_data");		if(cache_data){ //缓存中有直接取 否则发ajax请求			//处理结果					}else{			$.ajax({				url: "list.php",				type: 'get',				dataType: "json",				success: function (data) {					//console.log(JSON.stringify(data));					cache.save('cache_data', data);										//处理结果				},				error: function(data, textStatus, errorThrown){					//请求失败要操作的				}			});		}	})})

转载地址:http://bodrj.baihongyu.com/

你可能感兴趣的文章
C#常用的设计模式
查看>>
C#-快速排序算法
查看>>
docker 部署SpringBoot项目
查看>>
mybatis基础知识(四)&输入映射与输出映射
查看>>
gitflow工作流
查看>>
【MongoDB】update修改器($set、$unset、$inc、$push、$pull、$pop)
查看>>
JAVA 继承
查看>>
电脑键盘突然不能打字,很多键变成快捷键了
查看>>
Hbase表映射Hive表三种方法
查看>>
Java中获取List长度
查看>>
this关键字有什么用处?怎么用? 1.访问成员变量,区分成员变量和局部变量。 2.访问成员方法。 3.访问构造方法。 4.返回对当前对象的引用 5.将对当前对象的引用作为参数传递给其他方法。
查看>>
自学sql
查看>>
基于Springboot的社区开发项目
查看>>
nowcoder 左神算法1
查看>>
code刷题
查看>>
左神进阶2窗口
查看>>
dubbo入门
查看>>
http 错误类型
查看>>
一篇文章解决HTTP 请求头!
查看>>
学习日记02
查看>>