Rails.cache.fetch异常:TypeError(<ModelName>无法引用)

Mat*_*ark 9 caching ruby-on-rails

我正在使用漂亮的实现一些缓存Rails.cache.fetch.但是,在某个特定实例中,有时我会遇到异常:

TypeError in EmloController#index

Emlo can't be referred to

app/controllers/emlo_controller.rb:320:in `get_employees'
app/controllers/emlo_controller.rb:356:in `prepare_json_response'
app/controllers/emlo_controller.rb:23:in `block (2 levels) in index'
app/controllers/emlo_controller.rb:15:in `index'
Run Code Online (Sandbox Code Playgroud)

看起来fetch会在第一次尝试时总是爆炸(使用上面的内容),然后只要获取在到期时就可以正常工作.我知道我错过了什么,所以一双新鲜的眼睛会很好看.

这是调用缓存提取的方法:

def get_employees

  # This is for a AJAX refresh loop, so a 5-second cache actually helps quite a bit
  Rails.cache.fetch('emlo_all', :expires_in => 5.seconds, :race_condition_ttl => 1) do

    conditions = (params[:id]) ? {:user_id => params[:id]} : nil

    selections = [
      'employee_locations.id AS emlo_id',
      'employee_locations.status_id',
      'employee_locations.notes',
      'employee_locations.until',
      'employee_locations.updated_at',
      'employee_locations.user_id',
      'location_states.id AS state_id',
      'location_states.title AS status_string',
      'location_states.font_color',
      'location_states.bg_color',
      'users.displayname',
      'users.email',
      'users.mobile',
      'users.department',
      'users.extension',
      'users.guid',
      'users.dn'
    ].join(', ')

    Emlo.all(
        :select => selections,
        :joins => 'LEFT JOIN users ON employee_locations.user_id=users.id LEFT JOIN location_states ON employee_locations.status_id=location_states.id',
        :conditions => conditions,
        :order => 'users.displayname ASC'
    )
  end
end
Run Code Online (Sandbox Code Playgroud)

Mat*_*ark 15

config.action_controller.perform_caching = trueAND 开发模式中出现此问题config.cache_classes = false- 似乎ActiveRecord对象无法存储Rails.cache.

但是,如果您需要config.action_controller.perform_caching在开发模式下启用测试缓存,那么您还必须启用config.cache_classes.但这是暂时的,因为在更改资产管道中的类或文件后,您必须重新启动开发服务器.

禁用缓存后,我会Rails.cache.write(some_name, some_value) if Rails.env.production?用来防止缓存在开发中爆炸. Rails.cache.read()似乎没有受到影响.

  • LOL 10 年后,这仍然没有修复! (3认同)