English Document
The jquery-ajax-cache plug-in extends jQuery's $.ajax
and provides a very convenient way to cache ajax requests into 'localStorage' or 'sessionStorage'. The only thing you have to do is implement the cacheValidate
method to verify whether the returned results need to be cached. The plug-in will clear expired data during page loading and data reading and writing processes to avoid the accumulation of expired data. At the same time, you can also call $ajaxCache.deleteAllExpires()
to manually clear the expired cache.
1. Easy to use!
2. Still easy to use! !
3. Important things 3 times, easy! ! !
4. Clear expired data as much as possible to avoid overflow
Download the latest jquery-ajax-cache
bower
bower install jquery-ajax-cache
npm
npm install jquery-ajax-cache --save-dev
<script src="../node_modules/jquery/dist/jquery.js"></script><script src='../dist/jquery-ajax-cache.min.js'></script>
Because in actual applications, the result returned by the background may be success information or failure information. So we only need to buffer requests that we think are successful in terms of business. The jquery-ajax-cache plug-in reserves a method cacheValidate
for users to determine whether the request is successful.
cacheValidate
(this method needs to be called once globally)$ajaxCache.config({//Business logic determines whether the request is cached, res is the ajax return result, options is the parameter of $.ajax cacheValidate: function (res, options) { //Optional, configure whether global verification needs to be cached Methods, "global configuration" and "custom", at least one implementation cacheValidate method return true; // All cases are cached // return res.state === 'ok'; // Cache only when certain conditions are met // return false; // Do not cache}, storageType: 'localStorage', //Optional, 'localStorage' or 'sessionStorage', default 'localStorage' timeout: 60 * 60, //Optional , unit is seconds. Default is 1 hour});$.ajax({//When using, just add a row of attributes ajaxCache: trueajaxCache: true // "Global configuration" and "custom", at least one implementation of the cacheValidate method/* others... */});
cacheValidate
for a single request$.ajax(//The parameters here will override the settings in 'Global Configuration' ajaxCache: {//Business logic determines whether the request is cached, res returns the result for ajax, options is the parameter of $.ajax cacheValidate: function (res, options) { //Optional, configure the global method to verify whether caching is required, "global configuration" and "custom", at least one implementation of the cacheValidate method return true; // Caching in all cases // return res.state === 'ok'; // Cache only if certain conditions are met // return false; // Do not cache}, storageType: 'localStorage', //Optional, 'localStorage' or 'sessionStorage', default' localStorage'timeout: 60 * 60, //Optional, unit seconds. Default is 1 hour, forceRefresh: false //Optional, default is false. Whether to force a refresh request. This request does not read the cache, and if the request is successful, the cache will be updated. Application scenarios such as: pull-down refresh}});
No need to actively call unless there are special circumstances, the plug-in will automatically clear expired data
$ajaxCache.deleteAllExpires();