Permalink
Browse files

this is my jam.

  • Loading branch information...
1 parent da7e39e commit 1dbb6098beccc50fa8d24850cc9a7c8ff4919e70 @paulirish paulirish committed Feb 1, 2012
Showing with 56 additions and 34 deletions.
  1. +15 −9 README.md
  2. +41 −24 jquery-ajax-jstorage-cache.js
  3. +0 −1 jquery-ajax-jstorage-cache.min.js
View
24 README.md
@@ -1,16 +1,17 @@
# About
-jquery-ajax-jstorage-cache is a plugin build for jQuery (>1.5.1) and [jStorage](http://www.jstorage.info) . It's a client-side cache plugin for AJAX response intended to save bandwith and time. All the responses are stored in the Local Storage powered by HTML5 (see [jStorage#Support](http://www.jstorage.info/#support)).
+jquery-ajax-localstorage-cache is a plugin built for jQuery (>1.5.1) and localStorage. It's a fork from the [jStorage-dependent original](https://github.com/nectify/jquery-ajax-jstorage-cache). It provides a client-side cache AJAX responses intended to save bandwith and time.
# How to use
## Parameters
$.ajax({
url: '/post',
+ localCache: true, // required to use
- cacheJStorage: true,
- cacheKey: 'post',
- isCacheValid: function(){
+ cacheTTL : 1, // in hours. Optional
+ cacheKey: 'post', // optional
+ isCacheValid: function(){ // optional
return true;
},
@@ -21,21 +22,26 @@ jquery-ajax-jstorage-cache is a plugin build for jQuery (>1.5.1) and [jStorage](
On your AJAX request you got 3 new parameters :
-* cacheJStorage
- * Turn cacheJStorage on/off
+* localCache
+ * Turn localCache on/off
* Default: false
+* cacheTTL
+ * time in hours the entry should be valid.
+ * only for this specific ajax request
+ * Default : 5 hours
* cacheKey
- * CacheKey is the key that will be used to store the response in jStorage. It allow you to delete your cache easily with the jStorage.removeKey() function.
+ * CacheKey is the key that will be used to store the response in localStorage. It allow you to delete your cache easily with the localStorage.remoteItem() function.
* Default: URL + TYPE(GET/POST) + DATA
* isCacheValid
* This function must return true or false. On false, the cached response is removed.
* Default: null
+
## Notes
-* You can delete the cache by using jStorage API (see [jStorage#Usage](http://www.jstorage.info/#usage)).
+* You can delete the cache by using `localStorage.clear()`.
* Note that you can pre-load content with this plugin. You just have do to the same AJAX request without a success callback and the same cacheKey.
# License
-This project is distributed under Apache 2 License. See LICENSE.txt for more information.
+This project is distributed under Apache 2 License. See LICENSE.txt for more information.
View
65 jquery-ajax-jstorage-cache.js
@@ -1,38 +1,55 @@
+
+// github.com/paulirish/jquery-ajax-localstorage-cache
+// dependent on Modernizr's localStorage test
+
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
+
+
// Cache it ?
- if( options.cacheJStorage == undefined || ! options.cacheJStorage )
- return;
-
- var cacheKey;
- // If cacheKey exist we take it, or default one will be used
- if ( options.cacheKey )
- cacheKey = options.cacheKey;
- else
- cacheKey = options.url + options.type + options.data;
+ if( !Modernizr.localstorage || !options.localCache ) return;
+
+ var hourstl = options.cacheTTL || 5;
+
+ var cacheKey = options.cacheKey ||
+ options.url.replace(/jQuery.*/,'') + options.type + options.data;
// isCacheValid is a function to validate cache
if( options.isCacheValid && ! options.isCacheValid() ){
- $.jStorage.deleteKey( cacheKey );
+ localStorage.removeItem( cacheKey );
+ }
+ // if there's a TTL that's expired, flush this item
+ var ttl = localStorage.getItem(cacheKey + 'cachettl');
+ if (ttl && ttl < +new Date()){
+ localStorage.removeItem( cacheKey );
+ localStorage.removeItem( cacheKey + 'cachettl');
+ ttl = 'expired';
}
- if( $.jStorage.get ( cacheKey ) ){
+ var value = localStorage.getItem( cacheKey );
+ if( value ){
//In the cache? So get it, apply success callback & abort the XHR request
- options.success( $.jStorage.get ( cacheKey ) );
+ // parse back to JSON if we can.
+ if (value.indexOf('{') === 0) value = JSON.parse(value);
+ options.success( value );
// Abort is broken on JQ 1.5 :(
jqXHR.abort();
}else{
- //If it not in the cache, we change the success callback, just put data on jStorage and after that apply the initial callback
- if( options.success ) {
- var successhandler = options.success;
-
- options.success = function( data ) {
- $.jStorage.set( cacheKey, data );
- successhandler( data );
- }
- }else{
- options.success = function( data ) {
- $.jStorage.set( cacheKey, data );
- }
+
+ //If it not in the cache, we change the success callback, just put data on localstorage and after that apply the initial callback
+ if ( options.success ) {
+ options.realsuccess = options.success;
+ }
+ options.success = function( data ) {
+ var strdata = data;
+ if (this.dataType.indexOf('json') === 0) strdata = JSON.stringify(data);
+ localStorage.setItem( cacheKey, strdata );
+ if (options.realsuccess) options.realsuccess( data );
+ };
+
+ // store timestamp
+ if (!ttl || ttl === 'expired'){
+ localStorage.setItem( cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl);
}
+
}
});
View
1 jquery-ajax-jstorage-cache.min.js
@@ -1 +0,0 @@
-$.ajaxPrefilter(function(options,originalOptions,jqXHR){if(options.cacheJStorage==undefined||!options.cacheJStorage)return;var cacheKey;if(options.cacheKey)cacheKey=options.cacheKey;else cacheKey=options.url+options.type+options.data;if(options.isCacheValid&&!options.isCacheValid()){$.jStorage.deleteKey(cacheKey)}if($.jStorage.get(cacheKey)){options.success($.jStorage.get(cacheKey));jqXHR.abort()}else{if(options.success){var successhandler=options.success;options.success=function(data){$.jStorage.set(cacheKey,data);successhandler(data)}}else{options.success=function(data){$.jStorage.set(cacheKey,data)}}}});

1 comment on commit 1dbb609

@pokle

Hi Paul,

Why do you remove the "jQuery" string from the url in the following code?

var cacheKey = options.cacheKey ||
options.url.replace(/jQuery.*/,'') + options.type + options.data;

Cheers!

Please sign in to comment.