Caching comments: timestamps and subdomains

Hi,

I'm working on a website that has a very slow loading frontpage. I
wanted to start by caching certain elements that have high load. For
example, it loads the last 50 comments, along with the corresponding
usernames, avatars and more. So I cached that fragment and created a
sweeper that observes Comment and expires after every create or
destroy action.

However, I have two problems:

- Every comment has a timestamp that gets displayed like "1 minute
ago". When the fragment displaying the last 50 comments is cached,
this timestamp gets cached as well. That means that even after 15
minutes, it still says "1 minute ago" until another comment is posted.
Is there any way to take that bit out of the cached fragment so that
it gets updated? Caching these comments saves us about 150
milliseconds, so we really do want to cache it!

- When expiring a cached fragment, it sends along the subdomain. This
means that when I create a comment at www.domain.com/comment/create,
it will expire the cache for www.domain.com, but not for domain.com. I
don't want it to store two different cached fragments for www and non-
www, as they're exactly the same. I also want both the www and non-www
page to expire. Should I do something like cache(:action =>
'...', :subdomain => false, :action_suffix => '...') and do the same
for the expire_fragment part? Or is there a better solution?

Thank you!

- When expiring a cached fragment, it sends along the subdomain. This
means that when I create a comment atwww.domain.com/comment/create,
it will expire the cache forwww.domain.com, but not for domain.com. I
don't want it to store two different cached fragments for www and non-
www, as they're exactly the same. I also want both the www and non-www
page to expire. Should I do something like cache(:action =>
'...', :subdomain => false, :action_suffix => '...') and do the same
for the expire_fragment part? Or is there a better solution?

Sorry. The above doesn't work! Even with :subdomain => false present,
it will look for the www-version. Is there another way? Or should I
start redirecting all www-requests to domain.com?

With the first problem, you can do something like this:

In the coment render the datetime when they create, like this:

user1 make a coment 2009-12-16 20:44

and with jquery (if you use jquery) you can transform the datetime to “time ago” format with this library:

http://timeago.yarp.com/

Like this the fragment is cached and with javascript make the change.

See the library timeago for the datetime format.

Yes, I've thought about that. Thanks for pointing me to that library.
There's no Rails way around it though?

Thanks again :slight_smile:

- When expiring a cached fragment, it sends along the subdomain. This
means that when I create a comment atwww.domain.com/comment/create,
it will expire the cache forwww.domain.com, but not for domain.com. I
don't want it to store two different cached fragments for www and non-
www, as they're exactly the same. I also want both the www and non-www
page to expire. Should I do something like cache(:action =>
'...', :subdomain => false, :action_suffix => '...') and do the same
for the expire_fragment part? Or is there a better solution?

I have another remark about this: I can't fully disregard subdomains,
as we actually use them for our users. If you go to
username.domain.com/ you'll call another controller than with www.domain.com.
If I disregard the subdomain however, it will store both the fragments
in /views/domain.com/?action_suffix=something. That won't work.

Is there a way to explicitly say where to cache a fragment in the
memory (e.g. cache('/home/index/feed') do .. end ) and expire it the
same way (e.g. expire_fragment('/home/index/feed') ) ?