As object of cache search by identifier

select * from Foo where id = 200

How to cache object search by id, as we observe that the selection call is executed all the time on a specific identifier. How to include this in rails in an unobtrusive way so that we can switch between any cache storage in the future (for example, memcached or redis).

EDIT:

Much more like a Hibernate-based caching strategy org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITEthat applies to all Entity classes in Java EE.

+5
source share
4 answers

You can use built-in caching with rails

Rails.cache.fetch :cache_id_here, :expires_in => 1.days do
   Model.find(id)
end

:cache_id_here , , . memcached , . .

UPDATE

, , Rail.find .save . - , , : https://github.com/johnkoht/cache-money-millionaire .

, Post.find(3), Post 3, . , -, .

+2

Foo , , -

class Foo < ActiveRecord::Base

  def self.find_some_foo(foo_id)
    Rails.cache.fetch("foo_#{foo_id}", expires_in: 7.days) do
      begin
        self.where(:id => foo_id)
      rescue Exception => e
        logger.info "Exception fetching Foo ID: #{e.to_s}"
        nil
      end
    end
  end

end

, config.cache_store= config/application.rb, , config.cache_store = :memory_store , 2.1.

- , , cache_store. . , , , config/application.rb. , , memcache -.

, . , rake tmp:clear. memcache, . .

+3

:

record-cache

Rails 3. .

cache_money

Created by Twitter, now supported by the community. I do not know if Rails 3 is supported.

+1
source

Identity Map can help: http://api.rubyonrails.org/classes/ActiveRecord/IdentityMap.html

Its still in beta, so use with caution. He will also not track associations.

0
source

All Articles