[tip] making cached pages look dynamic


#1

Hey

I just launched a new release of www.strongside.dk based on rails 1.1.

The site is in danish so it wont make any sense to most of you.

However, here at technique I want to share with you.

Some of the pages on that site requires a lot of calculations, but the
data is actually static. So these pages are cached.

However, when a logged on user is viewing a cached page, we still want
to show him links based on his profile.

Enter, a litte rjs…

  1. in my layout i have:
Login |
<a href="/account/signup">Opret Profil</a>
 |

but when a user is logged in I want to replace these with a logout and a
view profile link.

  1. create a action and add a whatever.js route for it. I called my route
    “profile.js”.
    Just below my navigation is call this action with
  1. implement the action

def profile
render :update do |page|
if session[‘user’]
page.replace_html ‘login_links’,:inline=>‘Log af | Min
Profil
|’
end
end
end

I know, I should use the url helpers for this…

Anyways…

When a page is shown on the site, it only contains the login and create
profile links.
It calls the rjs action through the profile.js import
The rjs action checks if its an logged in user, if so, replace the
login/
create profile links with logout/view profile.

This technique is in part inspired by one shown previously at http://
caboo.se, but this one, using rjs is much simpler…

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)


#2

Mikkel B. wrote:

However, when a logged on user is viewing a cached page, we still want
to show him links based on his profile.

Enter, a litte rjs…

When a page is shown on the site, it only contains the login and create
profile links.
It calls the rjs action through the profile.js import
The rjs action checks if its an logged in user, if so, replace the login/
create profile links with logout/view profile.

Interesting.

I had been thinking about this problem myself. I have a couple of pretty
expensive pages that I was thinking about removing the customizations
from so that they would be served from the cache.

The solution that I was contemplating was to move the customizations
into partials. Then, I was thinking, that the main page would be cached,
but the partials would be dynamically generated.

Does anyone know 1) if my mental model of caching is even correct and 2)
how the two approaches would compare?

Thanks,

Ray


#3

page caching writes the view to an actual .html file in public, so its
completely static and partials wouldnt help you.
You might have more luck with fragment cache, but is you have a lot of
data in the fragment, that might cause a problem???

On Tuesday, March 28, 2006, at 11:09 AM, Ray B. wrote:

It calls the rjs action through the profile.js import
The solution that I was contemplating was to move the customizations


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)


#4

If I were you I would take a look at partial page caching. It looks
something like:
<% cache(“file/path/here”) do %>
<%= render :whatever %>
<% end %>

This will cache whatever is in the block to the file given. If the
file isn’t there, then it will create it and the output as needed. I
use this method quite a bit since I have a header which changes when a
user is logged in or not. It seems to work pretty well.

-Nick