How to create custom cache mechanism for ajax calls using localStorage in JQuery?

Maybe i'm wrong, but if I hit the cache I don't even start an ajax call. This is how I usually use cache, I think you can adapt it to use local storage instead of a cache object.

Maybe i'm wrong, but if I hit the cache I don't even start an ajax call. This is how I usually use cache, I think you can adapt it to use local storage instead of a cache object var cache = {}; $('input'). Change(function(){ var val = $(this).val(); if (cacheval === undefined){ $.

GetJson(url, function(data){ cacheval = data; use_data(data); } }else{ use_data(cacheval); } }).

I've to make a large number of calls from multiple places, which all use getJSON. It would be tiresome to add the caching to each of these. That's why I was looking to intercept the ajax call – Capt.

Nemo Aug 1 at 14:48 @Capt. Nemo, in that case i'd use a proxy, so that each of your calls, calls a proxy function that handles a centralized cache. I think it's a pattern of the gang of four.

– Nicola Peluchetti Aug 1 at 14:53.

Another option is to override the $. Ajax method. You can try out my fiddle.

Internally the $. Ajax method is used for load, get, and post. (function(){ // Store a reference to the original ajax method.

Var originalAjaxMethod = jQuery. Ajax; // Define overriding method. JQuery.

Ajax = function(options){ var key = ''; if(options. Url) key += options. Url; if(options.

Data) key += '? ' + options. Data; // has made this request if(('localStorage' in window) && (key in localStorage)){ // todo: determine which callbacks to invoke var cb = options.

Complete || options. Success; cb. Call(this, localStoragekey); } else{ // has not made this request // todo: determine which callbacks to intercept var cb = options.

Success; options. Success = function(responseText){ localStoragekey = responseText; cb. Apply(this, arguments); }; originalAjaxMethod.

Call( this, options ); } }; })().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions