views:

58

answers:

4
+2  Q: 

Javascript cache

Hi all,

I need to get a large amount of data back from a server via ajax. It will take a bit of time to get to the browser. The second time the user goes back to same web page I don't want them to have to download the data via ajax again. Is there anyway Javascript can write the json data to the browsers cache or filesystem reliably?

Cheers

Rich

+1  A: 

Have you looked at the localStorage spec?

Tim
Today, thanks to your suggestion of localStorage I started using YUI storage-lite. Seems to work well with my modern browser. It's only 2.6k so I can get away with using it with JQuery without loading up my page size too much.
Rich
+1  A: 

As Tim says, localStorage is a good option, however not all browsers support it. As a fallback, you could store the JSON text in the session.

Ash White
Hi Ash, just looking at localStorage. It looks like exactly what I need but as you said, will only work with modern browsers. If I implemented your suggested fallback of storing in the users session, would that mean in a cookie? If so, wou;dn't that get passed backwards and forwards with each request to the server making the whole thing pointless? If you are not talking cookies, how do I store in a session?
Rich
Session data is stored on the server, not the client, which means that it is not passed around with each request. As for reading/writing the session, that depends on what language you're using.
Ash White
ok, get it, I was confused because my goal is to prevent multiple downloads of the data and sessions will not prevent that. I will have a memcached server in the architecture so I could store queryset or serialised version there rather than query the DB each time, I just need to reduce data download. It looks as though the answer might be to use localStorage where avail and tell the user that for best performance download the latest browser.
Rich
+1  A: 

I think if the response has the proper headers, the browser will cache it like any other page. I found this tutorial very informative.

http://www.mnot.net/cache_docs/

mqsoh
+3  A: 

That's when you can take advantage of having a RESTful service.

If your requests are made via GET and are idempotent (eg: same queried URL will always yield the same response), the browser will cache the response.

See: http://ajaxpatterns.org/RESTful_Service

NullUserException
I read the doc link you provided. Unfortunately the dataset grows over time so the query will return from a given GET will not exactly be idempotent. Unless there is a better pattern I intend to get the data, store the data and the last ID of the data and then specify the last ID in the next GET request so I only get data back from that point. If I have no localStorage, I will get a growing dataset each time. It will work but will be much slower.
Rich
@Rich You can have content that's updatable with a RESTful service, you just have to use your headers wisely.
NullUserException
But the content is being updated outside Ajax on the server so whast header should I be using to retrieve data that is changing independently of the client? It will work as far as I can tell so far, it just won't be idempotent. Do I understand it right?
Rich