Skip to content


Subversion checkout URL

You can clone with
Download ZIP


set timestamp safely on response #11

wants to merge 2 commits into from

2 participants

@treasonx is running into a problem right now in chrome where when trying to set the TTL its throwing an exception due to localStorage being full. It looks like the timestamp should be set in the try catch when the response comes back.

@treasonx treasonx referenced this pull request in gruntjs/

Plugin list not loading on refresh in chrome. #15


Hmmm... i can understand try/catching it but dropping the conditional around that seems like an odd choice.


The conditional for setting the ttl seemed a bit odd to me given what the script is trying to accomplish.

1) Why do we set the ttl outside the success callback? Shouldn't the ttl be set when the data is stored on success? What if the request fails? Do we really want to set a ttl in a failure case? If we did there would be a ttl with no associated payload, which wastes space.

2) The only way we get into the block where the conditional was, is if we have no data and we need to retrieve it. The conditional was redundant. The only way we would get into the block where the conditional was is it we had no data. The only way we had no data was if the ttl was null or we had just expired it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 2, 2012
  1. @treasonx
Commits on Nov 3, 2012
  1. @treasonx
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 7 deletions.
  1. +2 −7 jquery-ajax-localstorage-cache.js
9 jquery-ajax-localstorage-cache.js
@@ -20,7 +20,6 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
if ( ttl && ttl < +new Date() ){
localStorage.removeItem( cacheKey );
localStorage.removeItem( cacheKey + 'cachettl' );
- ttl = 'expired';
var value = localStorage.getItem( cacheKey );
@@ -43,6 +42,7 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
// Save the data to localStorage catching exceptions (possibly QUOTA_EXCEEDED_ERR)
try {
+ localStorage.setItem( cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl );
localStorage.setItem( cacheKey, strdata );
} catch (e) {
// Remove any incomplete data that may have been saved before the exception was caught
@@ -54,10 +54,5 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
if ( options.realsuccess ) options.realsuccess( data );
- // store timestamp
- if ( ! ttl || ttl === 'expired' ) {
- localStorage.setItem( cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl );
- }
Something went wrong with that request. Please try again.