#1
Mar 04, 2007

Caching with Instance Variables

Learn a quick way to improve performance. Just store the end result of an expensive command in an instance variable!
Download (5.4 MB, 2:06)
alternative download for iPod & Apple TV (3.5 MB, 2:06)
# application.rb
def current_user
  @current_user ||= User.find(session[:user_id])
end

RSS Feed for Episode Comments 30 comments

1. Ryan Bates Apr 23, 2007 at 16:34

I should have made this more clear in the episode:

An instance variable only sticks around for a single request, so using the technique described will only benefit you if you need to call a method multiple times per request.

If you need to store a model or other data between requests, look into memcached.


2. chineseGuy May 01, 2007 at 02:35

I agree with Ryan Bates,and it is still useful for me.good tip!

我同意Ryan Bates,同时它依然对我有用.不错的技巧!


3. rorguy May 12, 2007 at 12:28

good tip


4. niko Jun 03, 2007 at 08:39

This is kindof automatically included in edge rails auto query caching.


5. Ryan Bates Jun 03, 2007 at 08:52

@niko, right. This isn't as necessary in edge rails since the query is cached. However it will return a new instance of User every time so I still recommend caching it in an instance variable.


6. Luke Freeman Jul 04, 2007 at 22:07

Hi,
Is it possible to subscribe to the old posts over itunes or only ones after episode 24?

Cheers,
Luke


7. Ryan Bates Jul 04, 2007 at 22:44

@Luke, I went ahead and changed the feed so it will display all episodes. You should be able to download all episodes through iTunes in a day or so.


8. Brian Jul 09, 2007 at 19:24

It appears the feed is missing episodes 20-23. If you can, can you add those to the iTunes feed?


9. Ryan Bates Jul 09, 2007 at 21:26

@Brian, hmm, those episodes are showing up for me in iTunes Music Store. Is that where you're looking?


10. Brian Jul 10, 2007 at 04:15

Ryan, yes, I am in the iTunes Music Store. In my iTunes I see all 1-55 listed except for 20-23. Given you're seeing them all I'm guessing now that I may have inadvertently deleted those from my iTunes (I did already view them once).

So an iTunes question for you or anyone -- is there a good way to restore a deleted iTunes entry?


11. brooke r Jul 25, 2007 at 12:44

since people are talking about it here...

I'd love all the back issues in iTunes as well. Right now, i'm only seeing episode 33 on. Did the feed get reset to only allow the last 30 episodes?

Thanks for such a great screencast!


12. Ryan Bates Jul 25, 2007 at 15:49

@brooke, the full quality feed should show all episodes on iTunes. The iPod version doesn't - I'll try to fix that.


13. saket Jul 27, 2007 at 13:51

its good


14. saket Jul 27, 2007 at 16:14

Can anyone explain this in more attractive way, so that I can easily uderstand the actual concept?


15. mike Aug 11, 2007 at 05:49

agree with saket...

would be great if there is a common scenario that demonstrates the benefit of using this technique...


16. Tom Styles Oct 09, 2007 at 03:42

I've just discovered Railscasts and thought I'd start at the first episode. Low and behold, absolute genius, I cut one of my request times in half by caching the return from my ldap lookup. Thanks, hoping to find many more gems as I work my way through the episodes.


17. Ste Oct 19, 2007 at 03:44

This might sound like stupid question. I recently started learning php with sql. This looks like php coding but am not sure. It maybe little advance for me or maybe am just been stupid and there is previous tutorials.... I looked at #1 because I thouth it would be the easiest to get my head round. I kinda understand what is going on in this tutorial... but I am not sure where I would apply such a method? Thanks Ste


18. James Mitchell Nov 28, 2007 at 20:23

@Ste You might want to start with the Agile book titled "Agile Web Development with Rails". It makes a very nice beginner course for people who have done web development with other languages and frameworks.

Have a look.


19. mike b Feb 27, 2008 at 12:10

Ryan, spectacular job on the Webcasts. Helps demystify the rails magic. This is a great tip but I am not clear on how it works? I thought instance variables only live as long as the routine is called. So every time you call current_user, isn't @current_user destroyed? Why isn't it? Is that rails or ruby at work? I assume rails.

thanks again for your dedication to the community


20. Ryan Bates Feb 28, 2008 at 09:06

@mike b, in Ruby, instance variables stick around as long as the object exists. In Rails this controller object sticks around for the entire web request. So if you call current_user multiple times during that one request the User.find call will only happen once.


21. Allen Feng Apr 17, 2008 at 00:44

Where can I read the development.log in real time like it shows in the railcast? What I did was I open the development.log as text file each time I insert a new comment to read the latest log. I tried something with tail -f but that didn't work. I'm a newbie btw.


22. Philip Bermingham May 02, 2008 at 04:16

excellent this is want I needed to do to get images to display that ere uploaded using file_column


23. kino May 04, 2008 at 05:14

Thanks for your screencasts!


24. Pedro C May 28, 2008 at 13:47

good tutorial, i had exactly that problem, with that instance variables it can improve the performance


25. Shawn Jun 18, 2008 at 22:57

The movie is damaged, could you upload again? Thanks!


26. Drekorian Jul 26, 2008 at 08:34

Shawn, the movie is not damaged, but the url is wrong.

You can get the movie at:

http://media.railscasts.com/videos/001_caching_with_instance_variable.mov


27. Simi Sep 08, 2008 at 09:04

Hi,

I have a doubt regarding caching in instance variables, forgive me if it is stupid.

What if I would need to catch more than 500 records..For example if I need to retreive all the employees of an organization..Would it be fine or is there any other suitable way to maximise performance?


28. Gary Sep 09, 2008 at 21:09

Thanks Ryan.
It is a great screencast!


29. DD Oct 02, 2008 at 23:55

I just want to say,it's a good start.


30. DD Oct 03, 2008 at 00:01

Thanks.
i will build a likely website

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