This repository has been archived by the owner. It is now read-only.
Caching jQuery AJAX requests using localStorage
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md
jalc-ms.js
jalc-ms.min.js
package.json

README.md

JALC (milliseconds fork)

JALC is a plugin built for jQuery (> 1.5.1) and any object implementing the storage interface, such as localStorage. It allows you cache your AJAX requests using localStorage API.

It's built on a fork from the original Christopher Keefer's Jalc. My simple fork allows you to use milliseconds instead of hours in cacheTTL parameter.

Table of contents

Parameters

$.ajax({
	localCache: true,
	cacheTTL: 1000 * 60 * 60 * 2,
	cacheKey: 'list_of_phones',
	isCacheValid: function() { return true; },

	url: '/phones',

	success: function(data, textStatus, jqXHR) {
		/* cached data will be available in first (data) variable */
	},
	error: function(jqXHR, textStatus, errorThrown) {/* ... */},
});

On your AJAX request you get 4 new parameters:

localCache

Default: false. Turn localCache on/off, or specify an object implementing the Storage interface to use. To enable this plugin this parameter should be true.

cacheTTL

Default: 18000000 (5 hours). How long cache will be valid in milliseconds.

cacheKey

Default is concatenated string: escaped url parameter value + type parameter value + data parameter value. 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.removeItem() function.

isCacheValid

Default: null. This function must return true or false. On false, the cached response is removed.

Notes

  • You can delete the cache by using localStorage.clear(), or by using localStorage.removeItem(cacheKey) if you specified a cacheKey (e.g. localStorage.removeItem('list_of_phones')). Note the above assumes you're using localStorage — replace as appropriate with your storage interface implementing object.
  • You can pre-load content with this plugin. You just have do to an initial AJAX request with the same cacheKey.
  • In most cases, you can rely on the 'intelligent guess' for the dataType parameter and leave it off, and the plugin will store the content type returned from the server alongside the data. However, you will get more consistent results if you explicitly specify the dataType in the AJAX parameters.

License

This project is distributed under Apache 2 License. See LICENSE for more information.