diff --git a/dist/jquery-ajax-cache.js b/dist/jquery-ajax-cache.js index 4b8ad1b..dad0ee2 100644 --- a/dist/jquery-ajax-cache.js +++ b/dist/jquery-ajax-cache.js @@ -209,14 +209,14 @@ return /******/ (function(modules) { // webpackBootstrap _createClass(CacheProxy, [{ key: 'genCacheKey', - value: function genCacheKey(AjaxOptions, originalOptions) { - var dataString = AjaxOptions.data; - var key; + value: function genCacheKey(options, originalOptions) { + var dataOrigin = originalOptions.data || {}; + var key, dataString; try { if (typeof dataString !== 'string') { - dataString = JSON.stringify(AjaxOptions.data); + dataString = JSON.stringify(dataOrigin); } - key = AjaxOptions.ajaxCache.cacheKey || AjaxOptions.url.replace(/jQuery.*/, '') + AjaxOptions.type.toUpperCase() + (dataString || '') + (AjaxOptions.ajaxCache.version || _config.defaultDataVersion); + key = originalOptions.ajaxCache.cacheKey || originalOptions.url.replace(/jQuery.*/, '') + options.type.toUpperCase() + (dataString || '') + (originalOptions.ajaxCache.version || _config.defaultDataVersion); console.log(key); key = md5(key); } catch (e) { diff --git a/dist/jquery-ajax-cache.min.js b/dist/jquery-ajax-cache.min.js index 2ef9e01..f48e78f 100644 --- a/dist/jquery-ajax-cache.min.js +++ b/dist/jquery-ajax-cache.min.js @@ -5,4 +5,4 @@ * (c) 2013-2015 WQTeam, MIT license * */ -!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.$ajaxCache=void 0;var n,a=r(2),o=r(4),i=r(1);window[i.globalCachePluginName]?console.warn(i.globalCachePluginName+" has existed!"):(window.$||console.error("can not find jQuery in global!!"),t.$ajaxCache=n=new a.AjaxCache($),(0,o.addFilterToJquery)(n)),t.$ajaxCache=n},function(e,t){"use strict";function r(e){return console.warn("There is no cacheValidate function!!"),!1}Object.defineProperty(t,"__esModule",{value:!0}),t.defaultCacheValidate=r;t.defaultTimeout=3600,t.defaultStorageType="localStorage",t.defaultDataVersion="1.0.0",t.globalCachePluginName="$ajaxCache"},function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var a=function(){function e(e,t){for(var r=0;r>16)+(t>>16)+(r>>16);return n<<16|65535&r}function i(e,t){return e<>>32-t}function c(e,t,r,n,a,c){return o(i(o(o(t,e),o(n,c)),a),r)}function u(e,t,r,n,a,o,i){return c(t&r|~t&n,e,t,a,o,i)}function s(e,t,r,n,a,o,i){return c(t&n|r&~n,e,t,a,o,i)}function l(e,t,r,n,a,o,i){return c(t^r^n,e,t,a,o,i)}function f(e,t,r,n,a,o,i){return c(r^(t|~n),e,t,a,o,i)}function h(e,t){e[t>>5]|=128<>>9<<4)+14]=t;var r,n,a,i,c,h=1732584193,d=-271733879,g=-1732584194,p=271733878;for(r=0;r>5]>>>t%32&255);return r}function g(e){var t,r=[];for(r[(e.length>>2)-1]=void 0,t=0;t>5]|=(255&e.charCodeAt(t/8))<16&&(a=h(a,8*e.length)),r=0;16>r;r+=1)o[r]=909522486^a[r],i[r]=1549556828^a[r];return n=h(o.concat(g(t)),512+8*t.length),d(h(i.concat(n),640))}function y(e){var t,r,n="0123456789abcdef",a="";for(r=0;r>>4&15)+n.charAt(15&t);return a}function x(e){return unescape(encodeURIComponent(e))}function m(e){return p(x(e))}function C(e){return y(m(e))}function b(e,t){return v(x(e),x(t))}function w(e,t){return y(b(e,t))}function j(e,t,r){return t?r?b(t,e):w(t,e):r?m(e):C(e)}n=function(){return j}.call(t,r,t,e),!(void 0!==n&&(e.exports=n))}(this)},function(e,t,r){var n,a;!function(o,i){n=i,a="function"==typeof n?n.call(t,r,t,e):n,!(void 0!==a&&(e.exports=a))}(this,function(){"use strict";function e(e,t){for(var r in t)e[r]=t[r];return e}function t(e){var t=!1;if(e&&e.setItem){t=!0;var r="__"+Math.round(1e7*Math.random());try{e.setItem(r,r),e.removeItem(r)}catch(n){t=!1}}return t}function r(e){var t=typeof e;return"string"===t?window[e]:e}function n(e){return"[object Date]"===Object.prototype.toString.call(e)&&!isNaN(e.getTime())}function a(e,t){if(t=t||new Date,"number"==typeof e?e=e===1/0?f:new Date(t.getTime()+1e3*e):"string"==typeof e&&(e=new Date(e)),e&&!n(e))throw new Error("`expires` parameter cannot be converted to a valid Date instance");return e}function o(e){var t=!1;if(e)if(e.code)switch(e.code){case 22:t=!0;break;case 1014:"NS_ERROR_DOM_QUOTA_REACHED"===e.name&&(t=!0)}else-2147024882===e.number&&(t=!0);return t}function i(e,t){this.c=(new Date).getTime(),t=t||f;var r=a(t);this.e=r.getTime(),this.v=e}function c(e){return e&&"c"in e&&"e"in e&&"v"in e?!0:!1}function u(e){var t=(new Date).getTime();return tn;n++){var a=this.storage.key(n),o=null;try{o=h.deserialize(this.storage.getItem(a))}catch(i){}if(null!==o&&void 0!==o.e){var c=(new Date).getTime();c>=o.e&&t.push(a)}}return t.forEach(function(e){r["delete"](e)}),t},clear:function(){this.storage.clear()},add:function(t,r,n){t=s(t),n=e({force:!0},n);try{var a=h.deserialize(this.storage.getItem(t));if(!c(a)||!u(a))return this.set(t,r,n),!0}catch(o){return this.set(t,r,n),!0}return!1},replace:function(e,t,r){e=s(e);var n=null;try{n=h.deserialize(this.storage.getItem(e))}catch(a){return!1}if(c(n)){if(u(n))return this.set(e,t,r),!0;this["delete"](e)}return!1},touch:function(e,t){e=s(e);var r=(this.storage.getItem(e),null);try{r=h.deserialize(this.storage.getItem(e))}catch(n){return!1}if(c(r)){if(u(r))return this.set(e,this.get(e),{exp:t}),!0;this["delete"](e)}return!1}};return l.prototype=g,l})}])}); \ No newline at end of file +!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.$ajaxCache=void 0;var n,a=r(2),o=r(4),i=r(1);window[i.globalCachePluginName]?console.warn(i.globalCachePluginName+" has existed!"):(window.$||console.error("can not find jQuery in global!!"),t.$ajaxCache=n=new a.AjaxCache($),(0,o.addFilterToJquery)(n)),t.$ajaxCache=n},function(e,t){"use strict";function r(e){return console.warn("There is no cacheValidate function!!"),!1}Object.defineProperty(t,"__esModule",{value:!0}),t.defaultCacheValidate=r;t.defaultTimeout=3600,t.defaultStorageType="localStorage",t.defaultDataVersion="1.0.0",t.globalCachePluginName="$ajaxCache"},function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var a=function(){function e(e,t){for(var r=0;r>16)+(t>>16)+(r>>16);return n<<16|65535&r}function i(e,t){return e<>>32-t}function c(e,t,r,n,a,c){return o(i(o(o(t,e),o(n,c)),a),r)}function u(e,t,r,n,a,o,i){return c(t&r|~t&n,e,t,a,o,i)}function s(e,t,r,n,a,o,i){return c(t&n|r&~n,e,t,a,o,i)}function l(e,t,r,n,a,o,i){return c(t^r^n,e,t,a,o,i)}function f(e,t,r,n,a,o,i){return c(r^(t|~n),e,t,a,o,i)}function h(e,t){e[t>>5]|=128<>>9<<4)+14]=t;var r,n,a,i,c,h=1732584193,d=-271733879,g=-1732584194,p=271733878;for(r=0;r>5]>>>t%32&255);return r}function g(e){var t,r=[];for(r[(e.length>>2)-1]=void 0,t=0;t>5]|=(255&e.charCodeAt(t/8))<16&&(a=h(a,8*e.length)),r=0;16>r;r+=1)o[r]=909522486^a[r],i[r]=1549556828^a[r];return n=h(o.concat(g(t)),512+8*t.length),d(h(i.concat(n),640))}function y(e){var t,r,n="0123456789abcdef",a="";for(r=0;r>>4&15)+n.charAt(15&t);return a}function x(e){return unescape(encodeURIComponent(e))}function m(e){return p(x(e))}function C(e){return y(m(e))}function b(e,t){return v(x(e),x(t))}function w(e,t){return y(b(e,t))}function j(e,t,r){return t?r?b(t,e):w(t,e):r?m(e):C(e)}n=function(){return j}.call(t,r,t,e),!(void 0!==n&&(e.exports=n))}(this)},function(e,t,r){var n,a;!function(o,i){n=i,a="function"==typeof n?n.call(t,r,t,e):n,!(void 0!==a&&(e.exports=a))}(this,function(){"use strict";function e(e,t){for(var r in t)e[r]=t[r];return e}function t(e){var t=!1;if(e&&e.setItem){t=!0;var r="__"+Math.round(1e7*Math.random());try{e.setItem(r,r),e.removeItem(r)}catch(n){t=!1}}return t}function r(e){var t=typeof e;return"string"===t?window[e]:e}function n(e){return"[object Date]"===Object.prototype.toString.call(e)&&!isNaN(e.getTime())}function a(e,t){if(t=t||new Date,"number"==typeof e?e=e===1/0?f:new Date(t.getTime()+1e3*e):"string"==typeof e&&(e=new Date(e)),e&&!n(e))throw new Error("`expires` parameter cannot be converted to a valid Date instance");return e}function o(e){var t=!1;if(e)if(e.code)switch(e.code){case 22:t=!0;break;case 1014:"NS_ERROR_DOM_QUOTA_REACHED"===e.name&&(t=!0)}else-2147024882===e.number&&(t=!0);return t}function i(e,t){this.c=(new Date).getTime(),t=t||f;var r=a(t);this.e=r.getTime(),this.v=e}function c(e){return e&&"c"in e&&"e"in e&&"v"in e?!0:!1}function u(e){var t=(new Date).getTime();return tn;n++){var a=this.storage.key(n),o=null;try{o=h.deserialize(this.storage.getItem(a))}catch(i){}if(null!==o&&void 0!==o.e){var c=(new Date).getTime();c>=o.e&&t.push(a)}}return t.forEach(function(e){r["delete"](e)}),t},clear:function(){this.storage.clear()},add:function(t,r,n){t=s(t),n=e({force:!0},n);try{var a=h.deserialize(this.storage.getItem(t));if(!c(a)||!u(a))return this.set(t,r,n),!0}catch(o){return this.set(t,r,n),!0}return!1},replace:function(e,t,r){e=s(e);var n=null;try{n=h.deserialize(this.storage.getItem(e))}catch(a){return!1}if(c(n)){if(u(n))return this.set(e,t,r),!0;this["delete"](e)}return!1},touch:function(e,t){e=s(e);var r=(this.storage.getItem(e),null);try{r=h.deserialize(this.storage.getItem(e))}catch(n){return!1}if(c(r)){if(u(r))return this.set(e,this.get(e),{exp:t}),!0;this["delete"](e)}return!1}};return l.prototype=g,l})}])}); \ No newline at end of file diff --git a/package.json b/package.json index 5427640..2e18e03 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "license": "MIT", "author": "wuchangming", "description": "Ajax Cache plugin backed by localStorage or sessionStorage for jQuery", - "version": "1.0.1", + "version": "1.0.2", "repository": { "type": "git", "url": "https://github.com/WQTeam/jquery-ajax-cache" diff --git a/src/CacheProxy.js b/src/CacheProxy.js index 87d493c..28353b8 100644 --- a/src/CacheProxy.js +++ b/src/CacheProxy.js @@ -39,14 +39,14 @@ export class CacheProxy { // 清除已过期数据 this.deleteAllExpires(); } - genCacheKey (AjaxOptions, originalOptions) { - var dataString = AjaxOptions.data; - var key; + genCacheKey (options, originalOptions) { + var dataOrigin = originalOptions.data || {}; + var key,dataString; try { if (typeof dataString !== 'string') { - dataString = JSON.stringify(AjaxOptions.data); + dataString = JSON.stringify(dataOrigin); } - key = (AjaxOptions.ajaxCache.cacheKey || AjaxOptions.url.replace(/jQuery.*/,'') + AjaxOptions.type.toUpperCase() + (dataString || '') + (AjaxOptions.ajaxCache.version || defaultDataVersion)) + key = (originalOptions.ajaxCache.cacheKey || originalOptions.url.replace(/jQuery.*/,'') + options.type.toUpperCase() + (dataString || '') + (originalOptions.ajaxCache.version || defaultDataVersion)) console.log(key); key = md5(key); } catch (e) { diff --git a/test/unit/test.api.js b/test/unit/test.api.js index 001db27..2e0a5d6 100644 --- a/test/unit/test.api.js +++ b/test/unit/test.api.js @@ -283,7 +283,7 @@ describe('jquery-ajax-cache', function() { afterEach(function () { clock.restore(); }); - var customAjaxCache = function(sendData,callback) { + var customDefaultAjaxCache = function(sendData,callback) { $.ajax({ ajaxCache: { cacheValidate: function () { @@ -297,24 +297,92 @@ describe('jquery-ajax-cache', function() { } }); } - it('should request again when data is change', function () { - var callback = sinon.spy(); - customAjaxCache({a:1}, callback); - server.requests[0].respond( - 200, - { "Content-Type": "application/json" }, - JSON.stringify({name: 'request 1'}) - ); - expect(callback).have.been.calledWithExactly({name: 'request 1'}); - customAjaxCache({a:2}, callback); - server.requests[1].respond( - 200, - { "Content-Type": "application/json" }, - JSON.stringify({name: 'request 2'}) - ); - expect(callback).have.been.calledWithExactly({name: 'request 2'}); - customAjaxCache({a:1}, callback); - expect(callback).have.been.calledWithExactly({name: 'request 1'}); + var customOptionAjaxCache = function(sendData, callback, type) { + $.ajax({ + ajaxCache: { + cacheValidate: function () { + return true; + } + }, + type: type, + data: sendData, + url:'config/cacheValidate/' + type, + success: function (data) { + callback(data); + } + }); + } + describe('#default', function() { + it('should request again when data is change', function () { + var callback1 = sinon.spy(); + var callback2 = sinon.spy(); + var callback3 = sinon.spy(); + + customDefaultAjaxCache({a:1}, callback1); + server.requests[0].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 1'}) + ); + expect(callback1).have.been.calledWithExactly({name: 'request 1'}); + customDefaultAjaxCache({a:2}, callback2); + server.requests[1].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 2'}) + ); + expect(callback2).have.been.calledWithExactly({name: 'request 2'}); + customDefaultAjaxCache({a:1}, callback3); + expect(callback3).have.been.calledWithExactly({name: 'request 1'}); + }) + }) + describe('#post', function() { + it('should request again when data is change', function () { + var callback1 = sinon.spy(); + var callback2 = sinon.spy(); + var callback3 = sinon.spy(); + + customOptionAjaxCache({a:1}, callback1, 'POST'); + server.requests[0].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 1'}) + ); + expect(callback1).have.been.calledWithExactly({name: 'request 1'}); + customOptionAjaxCache({a:2}, callback2, 'POST'); + server.requests[1].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 2'}) + ); + expect(callback2).have.been.calledWithExactly({name: 'request 2'}); + customOptionAjaxCache({a:1}, callback3, 'POST'); + expect(callback3).have.been.calledWithExactly({name: 'request 1'}); + }) + }) + describe('#get', function() { + it('should request again when data is change', function () { + var callback1 = sinon.spy(); + var callback2 = sinon.spy(); + var callback3 = sinon.spy(); + + customOptionAjaxCache({a:1}, callback1, 'GET'); + server.requests[0].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 1'}) + ); + expect(callback1).have.been.calledWithExactly({name: 'request 1'}); + customOptionAjaxCache({a:2}, callback2, 'GET'); + server.requests[1].respond( + 200, + { "Content-Type": "application/json" }, + JSON.stringify({name: 'request 2'}) + ); + expect(callback2).have.been.calledWithExactly({name: 'request 2'}); + customOptionAjaxCache({a:1}, callback3, 'GET'); + expect(callback3).have.been.calledWithExactly({name: 'request 1'}); + }) }) })