LoginSignup
3
3

More than 5 years have passed since last update.

Rails 3系でメモリにフラグメントキャッシュする。

Posted at

Rails 4.0 では標準に採用されているようので、以下のgemのinstallは不要

gem 'cache_digests'

config/environments/production.rb
config.cache_store = :memory_store, { size: 64.megabytes, :expires_in => 3.minutes }
config.action_controller.perform_caching = true

上記の設定だと、メモリが64mbに達するか、3分以上経過した場合、キャッシュが無効となる。

開発環境でも動作確認するため、同様の設定をconfig/environments/development.rbにも行う。

<% cache ['company', @company.code] do %>
…
<% end %>

キャッシュしたい部分を上記のようにブロックで囲む。
cacheの後の配列部分がキーになるので、この配列をキャッシュ対象のものでユニークになるように設定する。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3