#172
Jul 27, 2009

Touch and Cache

Rails 2.3.3 brings us a new feature called "touch". See how to use this to auto-expire associated caches in this episode.
Download (12.2 MB, 6:38)
alternative download for iPod & Apple TV (8.5 MB, 6:38)

Note: Sorry about the audio problems in the first half of this episode. Not sure what happened there.

Resources

# config/environment.rb
RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION

# config/environments/development.rb
config.action_controller.perform_caching = true

# comment.rb
belongs_to :article, :touch => true
<!-- articles/show.html.erb -->
<% cache @article do %>
  ...
<% end %>

RSS Feed for Episode Comments 29 comments

1. xinuc Jul 27, 2009 at 01:31

wohooo...

first to comment :P

thanks as always ryan...

great job!


2. Igor Jul 27, 2009 at 01:33

Great one, thank you!

Monday started as always with Ryan
s screencast - it mean it will be great week as always! ;)

Cheers


3. EppO Jul 27, 2009 at 03:33

In this use case, you can probably just use a counter cache on belongs_to association, as it will store comments_count information in Article table, so it will be updated each time a comment is inserted.
But anyway, good screencast showing this cool new feature although I don't see any other use case that caching for that one.
(Audio problems was not a big deal for me, it's understandable)

Cheers


4. Chris Gunther Jul 27, 2009 at 08:00

Nice screencast but is there any built in way to recover space from previously expired caches or would this have to be set up as some sort of a cron job to go through and delete the old ones?


5. Vlad Zloteanu Jul 27, 2009 at 09:04

:) Cool screencast, Ryan. Keep up the good work.


6. Joe Jul 28, 2009 at 11:52

@EppO you wouldn't want to do that in case you wanted to add the ability to update a comment, the count wouldn't change.

I suppose in the limited functionality the blog Ryan uses that would work fine, but in the spirit of scaling using the cache_key will always work!


7. Ryan Bates Jul 29, 2009 at 09:36

@Chris, it depends what cache store you are using. It defaults to file based store which you will need to set up some kind of cron task to remove old caches.

If you set it up to use a memcache store it will roll off the old caches automatically when it reaches the set memory limit.


8. Arun Jul 31, 2009 at 14:56

@Ryan: Nice video.
@all: I am rails evangelist and currently using it to make my personal web blog. I want to print friends blogging activity on user home page. Is there any recommended tutorial or pointers to accomplish that. I thought of using cache but thinking there could be a better way.


9. Thomas Aug 08, 2009 at 09:46

While this seems neat, is there really a big benefit to it? You will be hitting the article table everytime to get the cache key, right?

So you're still hitting the database a lot; You do avoid the comment queries, but it seems silly to be fetching the entire article model just to get the caching key. Seems a rather naive way of trying to scale up, particularly considering article is the one thing that is likely NOT to change, making it the perfect thing to cache in the first place.

Maybe you could provide some insights as to why you would use this, as it seems to be a pretty short-sighted way of making the page scale; why not just add the caching key expiration in the first place, it's not as though it's hard or messy?


10. Ryan Bates Aug 08, 2009 at 09:55

@Thomas, as mentioned near the beginning of this episode, there are other caching solutions which may work better in this scenario. This is more about showing off another technique for your caching repertoire.

The most optimized approach here is page caching. However, that requires you set up a sweeper on both article and comment and is a bit more complex.

The solution shown here works best when you have a heavy page where you are fetching a lot of related data that relies all on a single model. It might be a user profile page for example.


11. itkin Aug 14, 2009 at 16:21

Does it trigger callbacks) on the parent instance ?


12. Emery Finkelstein Aug 26, 2009 at 10:23

About a year ago I wrote a plugin to provide touch functionality to models. I've been using it to fuel another plugin of mine I call conflict warnings(http://github.com/EmFi/conflict_warnings). To deal with the the race condition that arises when two users modify the same resource asynchronously.

It relies heavily on touch because the pages I have a user acting on, are usually a collection of child records. Caching is a big help in this respect.


13. kent Sep 03, 2009 at 07:03

Can touch be used in a polymorphic relationship?


14. Kazuyoshi Tlacaelel Sep 28, 2009 at 01:23

I Really like your stuff, your a source of inspiration when it comes to discipline!

Thanks for all your hours of effort and work you've put out there for us!


15. Elka Oct 23, 2009 at 07:11

эмс...мощно)


16. ルイヴィトン 通販 Dec 07, 2009 at 08:11

cool....
nothing can be better than it!


17. Search files Dec 29, 2009 at 03:37

Seriously, if you guys add caching, you would dominate the world. Thanks for the nice blog!


18. wholesale nike shoes Jan 13, 2010 at 23:10

A very good article, I will always come in.


19. wholesale nike shoes Jan 13, 2010 at 23:10

A very good article, I will always come in.


20. cheap adidas shoes Jan 31, 2010 at 19:05

Adidas Shoes Online Shop-Hot Selling Adidas Shoes & Cheap Adidas Shoes.
my


21. wholesale Feb 10, 2010 at 05:57

I Really like your stuff, your a source of inspiration when it comes to discipline!


22. Kesha Feb 19, 2010 at 01:18

Cool screencast, Ryan. Keep up the good work.


23. Vitasha Feb 21, 2010 at 05:40

article and comment and is a bit more complex.


24. Marunya Feb 22, 2010 at 06:05

I have a user acting on, are usually a collection of child records.


25. Albina Feb 23, 2010 at 06:16

Super !!!


26. Kotasha Feb 26, 2010 at 22:26

Un articolo molto buono, io sarò sempre entrare


27. Cheap Abercrombie Mar 02, 2010 at 23:25

Thanks for all your hours of effort and work you've put out there for us!


28. alextk77 Mar 04, 2010 at 05:03

A very good article, I will always come in.


29. Louis Vuitton handbags Mar 09, 2010 at 19:20

railscasts.com/episodes/172-touch-and-cache

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source