Caching AJAX calls with local storage
fre.sc
fre.sc
The only problem is that HTTP-based caching requires sane server URLs, and the Rails code here indicates a lack of understanding of how to build a cache-friendly URL structure.
I'm also a little fazed by the fact that the 'isCacheValid' key not only checks the validity of the cache, but also sets a cache key at the same time.
About 'isCacheValid' setting the cache key, you're right, it should have been done in the success callback. The gist is going to be updated soon with your feedback.
I recently ran into something like this where I wanted to cache a JSON (maybe it was JSONP), but couldn't find a solution to store the cached JSON somewhere that works for IE.
This works great insofar as you aren't bound by PCI requirements; per PCI, you cannot transmit potentially sensitive data in a GET request, and so basically every request must be a POST. Furthermore, implementing CSRF via GET is a non-starter -- that's not even an option.
I think there is a good use case for something like this in certain situations, but using the browser cache for Ajax requests is generally a better idea.
I wrote locache as a more general client-side cache mechanism for all sorts of objects rather than just caching requests. I was caching lots of backbone models and user actions - it generally worked quite well. I also actually used it quite a bit for caching third party API's that I had no control over (more specifically I was working on a mashup using three different API's and often fetching the same content). When being lazy I tried once to cache my own API and regretted it after as its basically a poor mans browser cache.
Your going to run into a bunch of issues with localStorage if you have not already - specifically to do with memory limits, the blocking API and some strange support on mobile browsers. You may find some useful things in my code, or you may want to drop me a line to discuss it further.
Like I said before, we wanted to have a programmatic access to our cache. It let us read what is cached, update it or destroy it when some events occurs.