jQuery插件 - localstorage 缓存 AJAX请求

jopen 8年前

 jQuery插件——利用‘localStorage’ 和 ‘sessionStorage’ 对 jQuery AJAX 请求进行缓存。

下载

下载 最新 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.js'></script>

使用

全局配置

$ajaxCache.config({      // 业务逻辑判断请求是否缓存, res为ajax返回结果      cacheValidate: function (res) {    //选填,配置全局的验证是否需要进行缓存的方法,“全局配置” 和 ”自定义“,至少有一处实现cacheValidate方法          return res.state === 'ok';      },      storageType: 'localStorage', //选填,‘localStorage’ or 'sessionStorage', 默认‘localStorage’      timeout: 60 * 60, //选填, 单位秒。默认1小时  });

简单使用

$.ajax({      // 使用时 只要增加给ajax请求增加一行属性   ajaxCache: true      ajaxCache: true     // “全局配置” 和 ”自定义“,至少有一处实现cacheValidate方法      /*       others...      */  });

自定义

$.ajax(      // 此处的参数会覆盖‘全局配置’中的设置      ajaxCache: {          // 业务逻辑判断请求是否缓存, res为ajax返回结果          cacheValidate: function (res) { //选填,配置全局的验证是否需要进行缓存的方法, “全局配置” 和 ”自定义“,至少有一处实现cacheValidate方法              return res.state === 'ok' && res.code ==='200';          },          storageType: 'localStorage', //选填,‘localStorage’ or 'sessionStorage', 默认‘localStorage’          timeout: 60 * 60, //选填, 单位秒。默认1小时      }  });

项目主页:http://www.open-open.com/lib/view/home/1450362758688