# Regenerating cached pages automatically

**URL:** <https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756>\
**Category:** Rails\
**Created:** [February 16, 2007, 2:34am UTC](https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756 "2007-02-16T02:34:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 16, 2007, 2:34am UTC](https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756/1 "2007-02-16T02:34:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 17, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756/2 "2007-02-17T16:40:41Z")

</div>

To answer your questions…

1. The “proper” way of using the cache is _not_ to generate a new cached  
page. If you edit the page _again_ before any user visited it, then  
you’d  
have to needlessly empty the cached version again and repropagate it.
2. I dunno. Have you tried? 🙂

RSL

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 18, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756/3 "2007-02-18T23:19:42Z")

</div>

Russell N. wrote:

> To answer your questions…
> 
> 1. The “proper” way of using the cache is _not_ to generate a new cached  
> page. If you edit the page _again_ before any user visited it, then  
> you’d  
> have to needlessly empty the cached version again and repropagate it.
> 2. I dunno. Have you tried? 🙂
> 
> RSL

Thanks Russell,

1. makes sense in a weblog kind of way, but I’m happy to (and in fact  
need to) force republishing on any changes (basically _all_ changes must  
be approved by admin before they can be released), and;

2. render\_to\_string is my new best friend 🙂 (however there are  
limitations, such as, I can only get it working after converting all of  
my templates to partials as I can’t seem to get variables into the  
templates)

---

<div class="post-metadata">

**Author:** ![dang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dang](https://www.ruby-forum.com/u/dang)\
**Post date:** [February 26, 2007, 10:13am UTC](https://www.ruby-forum.com/t/regenerating-cached-pages-automatically/85756/4 "2007-02-26T10:13:14Z")

</div>

> 1. render\_to\_string is my new best friend 🙂 (however there are  
> limitations, such as, I can only get it working after converting all of my templates to partials as I can’t seem to get variables into the  
> templates)

I’m gonna introduce you a new best friend then 😉

render\_component\_as\_string (beats me why the difference in the naming  
using “as” and not “to” breaking the POLS, but that’s not the point  
here) will not just render the view but it will call the real action  
and then render the result as a string. This way you don’t have to worry  
about partials or about coding your actions in a different way. Just  
code them as usual, and use them as components.

Using components is not recommended sometimes because it will introduce  
some overhead in your system. Calling a component from another action  
means Rails has to instantiate a bunch of new objects, but for  
administrative/batch tasks like cleaning/reloading cache it seems a good  
option to me.

Regards,

javier ramirez

* * *

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!
