views:

672

answers:

2

Hi~ I had a problam, Ruby on Rails Cache in ApplicationController. The data can't get,but it's exist.

class ApplicationController < ActionController::Base
  before_filter :init

  def init
    @setting = Rails.cache.read("data/setting")
    if not @setting
      @setting = Setting.find_create
      Rails.cache.write("data/setting",@setting)
    end
  end
end

Development.log show this:

Cache read: data/setting

Setting Load (0.5ms) SELECT * FROM > "settings" LIMIT 1
Cache write: data/setting

Why?

A: 

If you are running your app in development mode, it's likely that caching is turned off. Check your config/development.rb file for the following:

config.action_controller.perform_caching             = false

To test your caching you can either change this value to true, or run in a different environment where caching is turned on. I often create an environment development_as_production, pointing at the development db, but with my production.rb settings.

tomafro
A: 

I am Json, I was changed the name.

But the development environment setting current is:

config.action_controller.perform_caching             = true

The perform_caching was trun on! The others caches can working. only this cache didn't working (It's in the ApplicationController).

@setting = Rails.cache.read("data/setting")
if not @setting
  @setting = Setting.find_create
  Rails.cache.write("data/setting",@setting)
end

Why? Is Rails.cache.read can't working in the ApplicationController?

huacnlee