English | 中文 |
:tophat:The best jQuery-ajax-cache plugin
AJAX-Cache is a jQuery plug-in. It implements asynchronous request caching based on localStorage/sessionStorage, and provides two cache modes: snapshot and timer.
npm i ajax-cache --save
https://github.com/tower1229/AJAX-Cache
You only need to add a localCache
configuration for jQuery.ajax ()
$.ajax({
url: "http://rapapi.org/mockjsdata/9195/common/getRandom",
dataType:'json',
localCache: 'snapshot',
success: function(res) {
if (res.snapshot) {
console.log('[snapshot] ' + res.data);
} else if (res.snapshootEqual) {
console.log('remote data is equal snapshot');
} else {
console.log('[remote data] ' + res.data);
}
}
});
$.ajax({
url: "http://rapapi.org/mockjsdata/9195/common/getRandom",
dataType:'json',
localCache: 5000,
success: function(res) {
console.log('\n[Caching for 5 seconds] ' + res.data);
}
});
$.ajax({
url: "http://rapapi.org/mockjsdata/9195/common/getRandom",
dataType:'json',
localCache: false,
success: function(res) {
console.log('Cache has been cleared');
console.log(res.data);
}
});
$.ajaxCache.clear();
$.ajaxCache.set({
storage: 'localStorage', //Storage mode, default "localStorage", optional "sessionStorage"
cacheNamePrefix: '_ajaxcache' //Storage prefix, usually without modification
});
http://refined-x.com/AJAX-Cache/test/
Copyright (c) 2017-present, refined-x.com