Hi all,
I’m trying to generate a bunch of cached pages with a publish function.
The idea is that a publish action gets called, there is a sweeper that
is activated and called a number of times (once for each page to be
re-cached). So far I can happily get the pages expired, but I then want
to immediately generate a new page so that the first user to request the
new version of the page doesn’t have to wait for it to generate. (I’m
working with some fairly big xml files that may take up to a minute or
more to generate).
My idea was to use cache_page() but I can’t find a way to get rails to
“pretend” to visit the url to generate the page. for example:
class PublishSweeper < ActionController::Caching::Sweeper
observe Page
def after_save(record)
case record
when Page
expire_page :controller => “xml”, :action => “page”, :id =>
record.id
cache_page :controller => “xml”, :action => “page”, :id =>
record.id
…
================================================================
What I’m aiming for is that the cache_page call “visits” the url and
saves the content to the cache file. Instead the response to the current
request is saved to file (which is exactly what the api says it should
do…). Alternatively if it’s possible to call a controller method and
save the returned value to a variable I could try this:
content = PageController.page(1)
cache_page content, :controller => “xml”, :action => “page”, :id =>
record.id
After all that rambling… I have 2 questions:
1 - is there a ‘proper’ way to regenerate cached pages without waiting
for the next user to visit that url so that cached pages are always
ready.
2 - can I call a controller method (outside the normal request/response
- in this case from a sweeper) and save the rendered output to a
variable/file or whatever.
thanks,
Dan