#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 18 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. Kesha Feb 19, 2010 at 01:18

Cool screencast, Ryan. Keep up the good work.


19. Vitasha Feb 21, 2010 at 05:40

article and comment and is a bit more complex.


20. Marunya Feb 22, 2010 at 06:05

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


21. Albina Feb 23, 2010 at 06:16

Super !!!


22. Kotasha Feb 26, 2010 at 22:26

Un articolo molto buono, io sarò sempre entrare


23. alextk77 Mar 04, 2010 at 05:03

A very good article, I will always come in.


24. Polyana Apr 19, 2010 at 23:22

а спаммеров-то развелось - жуть!.... =(


25. louis vuitton wallets Jul 14, 2010 at 05:08

i love it~~~


26. rapidshare Jul 25, 2010 at 16:42

nice post. i will also try this.


27. fivefingers kso Jul 26, 2010 at 23:36

fivefingers kso


28. bamboo-kitchen Jul 27, 2010 at 22:30

I really appreciate what you post.


29. hotfile download Jul 27, 2010 at 23:58

its a nice post. i really appprociate about this post.


30. buy jordans Aug 17, 2010 at 19:48

Thanks for the nice blog! i really appprociate about this post. Keep up the good work.


31. wholesale new era caps Aug 20, 2010 at 20:28

The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.


32. tausiah Aug 21, 2010 at 16:08

thanks.............wwwtausiahustadzcom.blogspot.com


33. Wholesale Electronics Aug 25, 2010 at 01:47

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


34. louis vuitton shoes Aug 26, 2010 at 20:55

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


35. Laptop Accessories Aug 30, 2010 at 20:24

thanks for your sharing, it is very good, i like it so much.


36. snow boots Aug 30, 2010 at 20:53

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?


37. louis vuitton sunglasses Sep 01, 2010 at 21:39

I think this is a great post. One thing that I find the most helpful is number five. Sometimes when I write, I just let the flow of the words and information come out so much that I loose the purpose. It’s only after editing when I realize what I’ve done. There’s defiantly a lot of great tips here I’m going to try to be more aware of.

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