0x26res
0x26res

Reputation: 13902

Caching Models in rails

I have a rails application, with a model that is a kind of repository. The records stored in the DB for that model are (almost) never changed, but are read all the time. Also there is not a lot of them. I would like to store these records in cache, in a generic way. I would like to do something like acts_as_cached, but here are the issue I have:

Do you have any idea of what gems I could use to do that ?

Thanks

EDIT

I am still looking for something similar to cache_flu but without memcached

Upvotes: 1

Views: 1998

Answers (4)

arunagw
arunagw

Reputation: 1193

I have started a gem called cache_me which can work with any cache_store

it's in Alpha mode but you can give a try and then open some pull request / Issues.

https://github.com/arunagw/cache_me

I will let you know when it's ready to use full mode.

Upvotes: 1

Bryan Ash
Bryan Ash

Reputation: 4489

Could you store the data in a file and load it into a constant (as suggested on Ruby on Rails: Talk):

require "yaml"
class ApplicationController < ActionController::Base
  MY_CONFIG = YAML.load(File.read(File.join(RAILS_ROOT, "config", "my_config.yml")))
end

Upvotes: 1

mark
mark

Reputation: 10564

You can store data in rails default cache or, as seems to be the most popular choice, use mem_cache_store which uses memcached.

#production.rb

config.cache_store = :mem_cache_store, '127.0.0.1:11211', {:namespace => "production"}

#some_helper.rb

def get_some_data
  Rails.cache.fetch('some_reference'){Model.find_some_data}
end

See also: http://guides.rubyonrails.org/caching_with_rails.html

Also, if you're using passenger you'll need to do this:

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      Rails.cache.instance_variable_get(:@data).reset if Rails.cache.class == ActiveSupport::Cache::MemCacheStore
    else
      # No need to do anything.
    end
  end
end

Upvotes: 0

Bryan Ash
Bryan Ash

Reputation: 4489

acts_as_cached was superseded by cache_fu.

Upvotes: 0

Related Questions