views:

49

answers:

2

I have a web app that's currently ~700 LOC in JS (more is serverside). I'm using jQuery. As I find myself trying to minimize AJAX requests to the server, I've got an awkward mix of caching and spaghetti code developing. It feels like I need a more centralized solution.

My idea: Create a map with the AJAX call to make (URL and args), and a dirty bit. If part of the app decides that the data in the map is outdated, it sets the dirty bit to true. If a part of the app requests the data, and the data is dirty or doesn't exist, the map will request it from the server before returning it. Otherwise, the map will return it directly.

Does that sound like a good design? Or is there a jQuery plugin or some other code that already does this?

+1  A: 

Sounds good to me. You're basically creating a custom caching system. You have a way to invalidate and refresh your cache on demand. That's all any caching system has at the core.

As far as available libraries for jQuery, I couldn't say.

Jason McCreary
A: 

jCache plugin is a jQuery plugin that allows you to cache by key (URL and args in your case). When you retrieve the value you could make an object to contain your real data and the dirty flag (or a Date object as an expiration control).