Rails 2.1 brings some new caching features which makes it very easy to cache any values including models. See how in this episode.
# script/console
Rails.cache.write('date', Date.today)
Rails.cache.read('date')
Rails.cache.fetch('time') { Time.now }
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
cache.fetch('time') { Time.now }
c = Category.first
c.cache_key # => "categories/1-20080622195243"
# models/category.rb
def self.all_cached
Rails.cache.fetch('Category.all') { all }
end
# config/environments/production.rb
config.cache_store = :memory_store
config.cache_store = :file_store, '/path/to/cache'
config.cache_store = :mem_cache_store
config.cache_store = :mem_cache_store, { :namespace => 'storeapp' }
config.cache_store = :mem_cache_store, '123.456.78.9:1001', '123.456.78.9:1002'
本篇介绍Rails 2.1中新增的缓存特性,演示如何使用Rails.cache进行数据缓存,包括写入、读取及fetch方法的使用,并展示了如何为不同环境配置不同的缓存存储。
818

被折叠的 条评论
为什么被折叠?



