# How to cache data in a plugin

**URL:** <https://www.ruby-forum.com/t/how-to-cache-data-in-a-plugin/130662>\
**Category:** Rails\
**Created:** [March 1, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-cache-data-in-a-plugin/130662 "2008-03-01T15:43:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 1, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-cache-data-in-a-plugin/130662/1 "2008-03-01T15:43:32Z")

</div>

Hi,

I’m using CountryCodes to get countries list for select box. It  
provides self.countries\_for\_select(\*args) method exactly for this  
purpose. I’m using it in only one place in my view. So I was thinking  
about caching this data somehow, so it won’t have to be recreated  
every time the page is loaded.

The problem is that the plugin initializes its main @countries  
variable in init.rb file by calling method  
CountryCodes.load\_countries\_from\_yaml.

Is there an easier way to cache this data than doing something like  
this:

lib/country\_codes.rb  
module CountryCodes  
…  
def self.cache\_countries\_for\_select  
@cached\_countries\_for\_select = countries\_for\_select(‘name’,  
‘a2’).sort.freeze  
end  
def self.cached\_countries\_for\_select  
@cached\_countries\_for\_select  
end  
…  
end

init.rb  
require ‘country\_codes’  
CountryCodes.load\_countries\_from\_yaml  
CountryCodes.cache\_countries\_for\_select

I don’t think that caching this data will make huge performance gain,  
I’m rather trying to do it to understand Ruby and Rails better.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 2, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/how-to-cache-data-in-a-plugin/130662/2 "2008-03-02T21:51:41Z")

</div>

On 2 Mar, 14:19, Okada [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and stores in the variable.
> 
> > I’m using CountryCodes to get countries list for select box. It  
> > this:  
> > end  
> > …  
> > end
> 
> > init.rb  
> > require ‘country\_codes’  
> > CountryCodes.load\_countries\_from\_yaml  
> > CountryCodes.cache\_countries\_for\_select
> 
> > I don’t think that caching this data will make huge performance gain,  
> > I’m rather trying to do it to understand Ruby and Rails better.

Thanks!

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 2, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/how-to-cache-data-in-a-plugin/130662/3 "2008-03-02T14:21:17Z")

</div>

The Ruby way of caching the result of a cost operation in a variable  
is:

def self.countries\_for\_select  
@cached\_countries\_for\_select ||= countries\_for\_select(‘name’,  
‘a2’).sort.freeze  
end

The “||=” returns the value in @cached\_countries\_for\_select “or”, if  
it is nil (will be first time), fetch the value  
and stores in the variable.

This is pure Ruby, nothing to do with Rails.

If you want to learn some cool Rails techniques, including the use of  
page/fragmant cache to better solve cases like this, I recommend

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
