# Can we cache user home pages?

**URL:** <https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024>\
**Category:** Rails\
**Created:** [January 10, 2006, 6:15am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024 "2006-01-10T06:15:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 6:15am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/1 "2006-01-10T06:15:54Z")

</div>

Hi Railers,  
I am trying to create a simple application like a guest book which has  
users  
and uses sessions to keep track of  
which users are logged on. The user’s page is constructed after doing  
some  
SQL queries and the user clicks on another link and hits the back button  
I  
dont want to regenerate the whole page (which is what happens now) .So I  
want to use some kind of caching mechanism.  
The page caching doc clearly says that this works for only stateless  
pages.But again the example mentions a page which can be cached  
'weblog/show/5 ’ .The user’s home page url in my guestbook is a page  
which  
looks like  
/users/index/5 (5 is the unique user id).  
So does this mean I can use page caching?  
If not is there any other approach ?  
Vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 6:34am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/2 "2006-01-10T06:34:05Z")

</div>

On 1/9/06, Vivek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> looks like  
> /users/index/5 (5 is the unique user id).  
> So does this mean I can use page caching?  
> If not is there any other approach ?  
> Vivek

Page caching means that for weblog/show/5, it writes this file in your  
public directory:

public/weblog/show/5.html

Assuming you have your webserver set up correctly, it will then serve  
this file instead of hitting rails. I do a lot with page caching  
(probably more than is healthy), and have been writing a little series  
on the subject with Courtenay on the caboose blog:

[http://blog.caboo.se/articles/2005/12/16/page-caching-your-whole-app](http://blog.caboo.se/articles/2005/12/16/page-caching-your-whole-app)  
[http://blog.caboo.se/articles/2005/12/27/when-page-caching-goes-bad-horribly-bad](http://blog.caboo.se/articles/2005/12/27/when-page-caching-goes-bad-horribly-bad)  
[http://blog.caboo.se/articles/2006/01/07/referenced-page-caching](http://blog.caboo.se/articles/2006/01/07/referenced-page-caching)

As always, consult the docs for more info. You can also look into  
action and fragment caching, which work well too.

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 7:53am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/3 "2006-01-10T07:53:22Z")

</div>

Thanks for the suggestions.

On 1/10/06, Parker T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% end %\>  
> Parker,  
> Where exactly should this be in the view ? I presume in my case the  
> unique\_suffix will be the user id!

and Rick  
What does  
“First, remove all user-referencing erb code from the views.” mean?  
I use a @user instance variable to show many things like name and other  
details.If I do remove then it becomes a stateless page anyway!

Thanks for the link to the blog btw…It has a lot of other useful tips.  
Vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 7:02am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/4 "2006-01-10T07:02:00Z")

</div>

Vivek,

I do this by using fragment caching, e.g.:

\<% cache( :action =\> ‘fragment\_identifier’,  
:controller =\> ‘controller’,  
:action\_suffix =\> ‘unique\_suffix’  
%\>  
@content\_for\_cache  
\<% end %\>

In my case I cache pages based on where a user is logged in from (it’s  
a location-based service), and cache different pages for users who are  
admins vs. those who are not, so my action suffix looks like:

get\_current\_place.id.to\_s + “-” + get\_current\_user.admin?.to\_s

I can then expire both admin/non cached pages using the regexp version  
of expire\_fragment:

expire\_fragment(/controller/method.action\_suffix=#{get\_current\_place.id.to\_s\*/)

There may be better ways to do this, I’m relatively new to the Rails  
caching framework, but this should at least work for you.

In your case, I"m not sure how much use you’ll get out of caching  
unless your app causes user to refresh regularly. I know that  
LiveJournal gets some use out of what could be considered per-user  
page caching, but that’s because their users are OCD and refresh their  
browsers habitually, which is not typical of most sites. Best of luck  
to you either way.

pt.

On 1/9/06, Vivek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> looks like

–  
Parker T.

> **[ParkErt.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=parkert.com)**
>
> Join thousands of people who own a premium domain. Affordable financing available.

510.541.0125

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/5 "2006-01-10T15:27:23Z")

</div>

I tried some very simple page caching to get used to the idea. But it  
didnt  
generate any pages in the public/ directory. All I did was  
caches\_page :index

In  
def login

# do authorization and set a sessiion

redirect\_to :action=\>‘index’  
end  
def index

# get the session and set a variable like @username

render :action =\>‘index’  
end

I know the cache would probably get overwritten because its not unique  
for  
each user but I dont see any generated html .Am i missing something ?  
I read that caching is disabled in the development mode (i use webrick)  
how do I enable it?  
Vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/6 "2006-01-10T15:48:45Z")

</div>

On 1/10/06, Vivek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried some very simple page caching to get used to the idea. But it didnt  
> generate any pages in the public/ directory. All I did was  
> caches\_page :index

Caching only occurs in production mode. You can configure  
config/environment/development.rb to cache in dev mode.

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [January 10, 2006, 7:59am UTC](https://www.ruby-forum.com/t/can-we-cache-user-home-pages/52024/7 "2006-01-10T07:59:24Z")

</div>

> and Rick  
> What does  
> “First, remove all user-referencing erb code from the views.” mean?  
> I use a @user instance variable to show many things like name and other  
> details.If I do remove then it becomes a stateless page anyway!
> 
> Thanks for the link to the blog btw…It has a lot of other useful tips.  
> Vivek

Exactly what you said. You’d have to do some javascript trickery  
then. Or use some combination of fragment/action caching.

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
