Reputation: 983
I have a huge page that I'm caching with rails caches_page command. Before the cache has been generated the first http request takes about 30 seconds to generate the cache. I would like to avoid this so the first user hitting the page can load it much faster.
I'm trying to generate the cache programmatically with http but this doesn't seem to work:
uri = URI.parse("http://mydomain.com/huge_page")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
What I got as response is a timeout error:
Timeout::Error (execution expired)
Do you have any suggestion how I could do this?
Upvotes: 1
Views: 361
Reputation: 2173
If you are trying to generate the cache by way of an HTTP request, you have to make sure you aren't making that request in the context of your original request. Meaning the page would be requesting itself.
If you're doing that and you have only a single thread or server running your app, it will never be able to complete the request and will timeout. This is because the action won't complete until it finishes the response = http.request(request)
part. However your server won't be able to respond because it's in the middle of an action.
However, all this is to say that you should really never under any circumstances have a request that takes that long to load. Anything that takes more than one second (or, in production, like 200ms) should be moved into a Delayed::Job worker or a similar background queue.
Without knowing the details of your app, I would highly recommend coming up with a way to generate the page much, much faster rather than relying on caching here.
Upvotes: 1