/
jquery.cacheAjax.js
104 lines (93 loc) · 4.19 KB
/
jquery.cacheAjax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Copyright (c) 2009 Zhang Jinzhu ( wosmvp@gmail.com )
* Dual licensed under the MIT and GPL
*
* cacheAjax -> // can use all $.ajax's setting options.
* force new ajax request
* $.cacheAjax({ force : true }) // force a new ajax request
* cache key
* $.cacheAjax({ key : customized_key }) // customize
* url + data // default key
* setTimeout
* $.cacheAjax({ timeout : 2000 }) // expire this ajax's response after 2 seconds
* $.setCacheTimeout(2000) // set global timeout, all cached result will be expire after 2 seconds.
* $.setCacheTimeout(false) // set global timeout as false, this means never expire.
*
* expireCache ->
* $.expireCache(/regexp/)
* $.expireCache('string-key')
* $.expireCache() // expire defaultKey,defaultKey is document.location.hash
* // Set Default Key
* $.setCacheDefaultKey('string')
* $.setCacheDefaultKey(function() {})
*
* addCache -> $.addCache(key,value,timeout);
*
* getCache -> $.getCache(key)
*/
(function($) {
var cacheAjax = (function() {
var defaultKey = document.location.hash, defaultTimeout, data = {};
function get(/*String*/ key) {
var value = data[ key];
return (!value || (value[1] && (new Date()).getTime() > value[1])) ? false : value[0];
}
function add(/*String*/ key,/*String*/ value,/*Number*/ timeout) {
timeout = timeout || defaultTimeout;
timeout = timeout && ((new Date()).getTime() + timeout);
data[key] = [value, timeout];
}
function remove(/*String*/ key) {
if (key) {
if(key instanceof RegExp) {
for (var i in data) { if (key.test(i)) { data[i] = false;} }
} else {
data[key] = false;
}
} else {
// default key
if (defaultKey instanceof Function) {
key = defaultKey.call(this).toString();
} else {
key = defaultKey.toString();
}
data[key] = false;
}
}
function ajax(/*Object*/ oldAjaxSetting) {
var newAjaxSetting = $.extend( {type: 'GET',dataType : 'script'} , oldAjaxSetting);
if (newAjaxSetting.type == 'GET') {
// customize/default cache key
var cache_key = newAjaxSetting.key || (newAjaxSetting.url + ((typeof(newAjaxSetting.data) == 'object') ? $.param(newAjaxSetting.data) : newAjaxSetting.data));
newAjaxSetting.success = function(e) {
add(/*String*/ cache_key,/*Response*/ e,/*Integer*/ newAjaxSetting.timeout);
if (newAjaxSetting.dataType == 'script') { eval(e); }
if (oldAjaxSetting.success) { oldAjaxSetting.success.call(this,e); }
};
// force a new ajax request or get cached response.
var result = newAjaxSetting.force ? false : get(cache_key);
if (result) {
if (newAjaxSetting.dataType == 'script') { eval(result); }
if (oldAjaxSetting.success) { oldAjaxSetting.success.call(this,result); }
var complete = oldAjaxSetting.complete || jQuery.ajaxSettings.complete; // FIXME Other jQuery Global Event
if(complete){ complete.call(this,result); }
}else{
// if dataType is 'script',reset it to 'text' to cache the response.
$.ajax($.extend({}, newAjaxSetting, (newAjaxSetting.dataType == 'script') ? {dataType: 'text'} : {}));
}
} else {
$.ajax(oldAjaxSetting);
}
}
return {
cacheAjax : ajax,
setCacheTimeout : function(value) { defaultTimeout = value; },
setCacheDefaultKey : function(value) { defaultKey = value; },
expireCache : function(key) { return remove(key); },
getCache : function(key) { return get(key); },
addCache : function(key,value,timeout) { return add(key,value,timeout); }
}
})();
$.extend(cacheAjax);
$.fn.extend(cacheAjax);
})(jQuery);