Administrator Kiranatama Staff
Knowledge


How do we cache an ajax response in Rails, so that we dont need to do an ajax the next time ? It's the question I'm facing on building an application that is AJAX-heavy, and it's becoming important for the application to be high-performance. After some googling, I came across several stuff. One is this interesting article on setting 'Expires' header in the HTTP response. But well, I dont really know how to implement that in Rails.
Anyway his book, High Performance Websites seems like a very good book to buy!
So to cache an ajax response, I use the solution on this site. Here is a simpler version of that:
<script> var cache = new Array; function myscript(somevalue) { var url = "ajax/function?someparam=somevalue"; if (cache[url]) { eval(cache[url]); } else { new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete:function(request) { if(request.status == 200) { cache[url] = request.responseText; } }}); } </script>