#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 -38 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. Pedro C May 28, 2008 at 13:47

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


24. Shawn Jun 18, 2008 at 22:57

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


25. 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


26. 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?


27. Gary Sep 09, 2008 at 21:09

Thanks Ryan.
It is a great screencast!


28. DD Oct 02, 2008 at 23:55

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


29. DD Oct 03, 2008 at 00:01

Thanks.
i will build a likely website


30. nunommc Apr 22, 2009 at 07:10

I dont know why, but I'm not getting the result I was expecting.

I'm doing a project, when I know that the Id for one object is different in each environment. So the only thing I need is to keep the result of a query in a variable, and use it foreve on.

As a simple example, I tried to do

<script src="http://gist.github.com/99813.js"></script>

and displaying that on the view, and it has everytime different value.

What is my mistake? should I use memcached instead?

Thanks


31. nunommc Apr 22, 2009 at 07:12

Ooops! I tried to paste the following on Gist:

@now ||= Time.now # => or also @@now ||= Time.now


32. nunommc Apr 22, 2009 at 08:27

I already know the solution :)

I forgot to change the development config file (config/environments/development.rb). Here is only needed to change "config.cache_classes = false" to "true".


33. stOPHER May 22, 2009 at 15:13

What if another thread updates your object in the database, while you are working with an old instance of the user? I think it is much better to cache these objects on the database server, or through a data access object. It really depends what your doing with the user, but it can get kind of messy caching stuff in this way.


34. veeru Aug 29, 2009 at 09:31

Ryan...awesome.....Thank you....this will help many like me .....


35. euroluxury Dec 08, 2009 at 23:30

good tip


36. discount Apr 02, 2010 at 23:32

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


37. Leandro May 28, 2010 at 09:25

Good tip.


38. Sam Smith May 29, 2010 at 17:04

This is a great video, thank you very much.


39. haynes repair May 31, 2010 at 21:00

incredible ... This is very helpful at all, and you tell the detail. pleased to be able to visit here


40. bestbuy Jul 15, 2010 at 09:27

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


41. Jerseys Jul 18, 2010 at 22:37

Nice thank you again


42. animal shaped rubber bands Jul 26, 2010 at 21:02

<A href="http://www.toptoys2trade.com/power-balance-wholesale-2-c-40/ "> power balance</A>


43. asics shose Jul 28, 2010 at 00:08

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.


44. timberlandbootsuk Aug 02, 2010 at 02:08

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


45. fade rolex watches Aug 04, 2010 at 17:55

I am a fanatic watch collection, especially the well-known watches, you also can do, click on my name!


47. erisa bonds Aug 09, 2010 at 09:52

I generally do not post in Blogs but your weblog forced me to, amazing work.. beautiful.


48. free directory list Aug 11, 2010 at 22:01

you for sharing.Nice post


49. free directory list Aug 11, 2010 at 22:02

you for sharing.Nice post


50. free directory list Aug 11, 2010 at 22:02

you for sharing.Nice post


51. uggs online Aug 13, 2010 at 22:06

Article is very nicely written.


52. milgaussreplica Aug 16, 2010 at 01:31

comes housed in a black plastic case with stainless steel lugs at each side. The black
wristband is nicely crafted from leather, and in case you plan on banging your new watch
around a bit, the display is protected by a durable, scratch-resistant mineral crystal
face.


53. boss Aug 18, 2010 at 01:07

<a href="http://www.powerbalancebracelet.org"><strong>Power Balance bracelet</strong></a> is a new technology that aids performance by using holograms embedded with frequencies that react positively with your body's energy to improve balance and strength.


54. boss Aug 18, 2010 at 01:09

<a href="http://www.powerbalancebracelet.org"><strong>Power Balance bracelet</strong></a> is a new technology that aids performance by using holograms embedded with frequencies that react positively with your body's energy to improve balance and strength.


55. Air Jordan AJF 3 Aug 19, 2010 at 00:20

This was a really useful episode, ROR really is awesome in that it lets you write code like that! Thanks for very good information. Excellent article that will provide the incentive and basis for my works.I wonder if I can mention the article as a bibliographic reference in my work. Thanks!


56. louis vuitton shoes Aug 26, 2010 at 21:03

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


57. snow boots Aug 31, 2010 at 02:17

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.


58. louis vuitton sunglasses Sep 01, 2010 at 22:32

Nice post. My friend John told me about this blog some weeks ago but this is the first time I’m visting. I’ll undoubtedly be back.

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