# Page caching with custom route

**URL:** <https://www.ruby-forum.com/t/page-caching-with-custom-route/120840>\
**Category:** Rails\
**Created:** [November 21, 2007, 1:11am UTC](https://www.ruby-forum.com/t/page-caching-with-custom-route/120840 "2007-11-21T01:11:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![konrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@konrad](https://www.ruby-forum.com/u/konrad)\
**Post date:** [November 21, 2007, 1:11am UTC](https://www.ruby-forum.com/t/page-caching-with-custom-route/120840/1 "2007-11-21T01:11:44Z")

</div>

I know the subject has been discussed a lot already, and there’s  
plenty of articles about it on the web. But I still haven’t figured  
out a solution to my problem, so here goes a cry for help.

I have the following custom route in my application:  
map.connect ‘/:username’, :controller =\> ‘public’, :action =\>  
‘show\_profile’

Basically, I want my users to be able to access their personal page  
through the “shortcut” [mywebsite.com is available for purchase - Sedo.com](http://www.mywebsite.com/username), so once that URL is  
entered or clicked, I redirect them to the appropriate action, which  
is show\_profile. There I have a simple code, kinda like this

@user = User.find\_by\_username(params[:username])

to retrieve their data from the DB.

This solution works great, but since their personal pages will be  
rarely updated, I want to use page caching to store it in a HTML file  
and avoid all Rails processing/DB queries, expiring the cache just  
when I actually need to.

And now enters my problem. Rails is saving the cached pages as  
show\_profile.html. That clearly doesn’t work, since each user must  
have a different cached HTML with their specific data. I want the  
cached HTML files to be named after the username (username.html) not  
after the action (show\_profile.html).

I’ve managed to make this work by adding the controller and action to  
the route, like this

map.connect ‘/public/show\_profile/:username’, :controller =\>  
‘public’, :action =\> ‘show\_profile’

and then my cached files would be stored in a ‘public/show\_profile/  
username.html’ path.  
But I really need the URLs to be simple as [mywebsite.com is available for purchase - Sedo.com](http://www.mywebsite.com/username),  
so that doesn’t work. And as only fragment caching accepts a  
‘action\_suffix’ parameter, I really can’t figure how to work this out  
with page caching.

Any ideas?

Thanks,

Konrad

---

<div class="post-metadata">

**Author:** ![konrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@konrad](https://www.ruby-forum.com/u/konrad)\
**Post date:** [November 21, 2007, 5:30am UTC](https://www.ruby-forum.com/t/page-caching-with-custom-route/120840/2 "2007-11-21T05:30:05Z")

</div>

> I know the subject has been discussed a lot already, and there’s  
> plenty of articles about it on the web. But I still haven’t figured  
> out a solution to my problem, so here goes a cry for help.
> 
> I have the following custom route in my application:  
> map.connect ‘/:username’, :controller =\> ‘public’, :action =\>  
> ‘show\_profile’

I don’t know, but I suspect Rails wants some sort of “anchor” before it  
will cache. Maybe not.

But for example, what happens if I create a user one your site named  
“images” or “javascripts” ? That’s going to conflict with existing  
directories in RAILS\_ROOT/public

Just something to think about. You may _want_ to move it “down a  
level”.

-philip

---

<div class="post-metadata">

**Author:** ![konrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@konrad](https://www.ruby-forum.com/u/konrad)\
**Post date:** [November 21, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/page-caching-with-custom-route/120840/3 "2007-11-21T13:17:23Z")

</div>

That wouldn’t really be a problem, since I’ve configured my page  
caching path to be under RAILS\_ROOT/public/cache and I also get to  
choose their usernames.

Right now the cached page, show\_profile.html, is been kept in the  
public/cache/public path (this last public directory is due to my  
controller, named ‘public’). The path is fine, I just need to be able  
to name the cached files after my usernames, not my action.
