views:

66

answers:

3

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:

  • I can not find a decent documentation for acts as cached (neither can I find it's repository)

  • I don't want to use memcached, but something simpler (static variable, or something like that).

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

A: 

acts_as_cached was superseded by cache_fu.

Bryan Ash
Is there something similar to cache_fu that would not use memcache but something simpler ?
jules
A: 

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
mark
As I said, I don't want to use memcached, it is too heavy to put in place. I want something simple.
jules
I guess SQL Caching is enhancing performances a lot in my case. I probably won't need something else, but it would be cool to have something like cache_flu that would work without memchached
jules
A: 

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
Bryan Ash