Forum: Ruby on Rails Can we cache user home pages?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-10 06:15
(Received via mailing list)
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-10 06:34
(Received via mailing list)
On 1/9/06, Vivek Krishna <krishna.vivek@gmail.com> 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-cach...
http://blog.caboo.se/articles/2005/12/27/when-page...
http://blog.caboo.se/articles/2006/01/07/reference...

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
19c9af375aca3001e3978cb16684803c?d=identicon&s=25 Parker Thompson (Guest)
on 2006-01-10 07:02
(Received via mailing list)
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 Krishna <krishna.vivek@gmail.com> wrote:
> looks like
>
>
>


--
Parker Thompson
http://www.parkert.com/
510.541.0125
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-10 07:53
(Received via mailing list)
Thanks for the suggestions.

On 1/10/06, Parker Thompson <parkert@gmail.com> 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-10 07:59
(Received via mailing list)
> 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
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2006-01-10 15:27
(Received via mailing list)
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-10 15:48
(Received via mailing list)
On 1/10/06, Vivek Krishna <krishna.vivek@gmail.com> 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
This topic is locked and can not be replied to.