I’ve got a product page. The show action has the following code:
def show
...
LiveView.add_live_view(@product, request)
...
end
In this same controller, if I have:
caches_action :show, :cache_path => (proc do
product_path(params[:id], :user_id => user_signed_in? ? current_user.id : nil)
end)
I’m trying to display real-time views, by grabbing the IP address from request. So, I need to keep track of every view that hits the page. However, if the page is already cached, it will never trigger the LiveView.add_live_view(@product, request) method.
The question is. Should I be caching this action? If so, how should it be done? Or should I forget about caching for this method?
Should I think of a different caching approach? Any suggestions?
I am not sure if caching at the application level is the right solution. I suggest looking at mod_cache which caches the response at the apache layer. (If you are running apache with passenger).
Finally, don’t forget about this rule. Never optimize unless you have to. What I mean is, test your application first. Doesn’t suffer performance loss if you don’t cache. Do you still need to cache everything? Do some code analysis and maybe you can use a better algorithm.
Edit, also look at HTTP status code 304. If the user refreshes the page, and you can convey that nothing has changed then just return 304.