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

时间:2022-07-21 20:40:20

I'm implementing some caching by using the nifty Rails.cache.fetch. However, in one particular instance, sometimes I encounter an exception:

我正在使用漂亮的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'

It seems the fetch will always explode (with the above) on the first try, and then work fine as long as the fetch is within the expiration. I know I'm missing something, so a fresh pair of eyes would be nice.

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

Here's the method which invokes the cache 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

2 个解决方案

#1


14  

This problem arises in development mode when config.action_controller.perform_caching = true AND config.cache_classes = false -- it seems ActiveRecord objects cannot be stored with Rails.cache.

当config.action_controller.perform_caching = true和config.cache_classes = false时,在开发模式中出现此问题 - 似乎ActiveRecord对象无法与Rails.cache一起存储。

But if you need to enable config.action_controller.perform_caching in development mode for testing caching, then you must also enable config.cache_classes. This would be temporary, though, because then you'd have to restart the development server after changing classes or files in the asset pipeline.

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

With caching disabled, I would use Rails.cache.write(some_name, some_value) if Rails.env.production? to prevent caching from blowing up in development. Rails.cache.read() doesn't seem to be affected.

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

#2


-1  

Depending on the structure of your application, you might get an error in development like this: TypeError (User can't be referred) This error is caused by some caching-reloading madness: The middleware implanted by some gem is cached. But in development, your classes usually aren't. Thus some classes may not be available under certain circumstances, e.g. if you are using before filters for user authentication provided by some engine. You should be able to get rid of the error above by turning on class caching. Try it (and restart the server afterwards):

根据应用程序的结构,您可能会在开发中遇到如下错误:TypeError(无法引用用户)此错误是由某些缓存重新加载的疯狂引起的:某些gem植入的中间件被缓存。但在开发过程中,你的课程通常不是。因此,某些类别在某些情况下可能无法使用,例如如果您在过滤器之前使用某些引擎提供的用户身份验证。您应该能够通过启用类缓存来摆脱上述错误。试一试(然后重启服务器):

development.rb

config.cache_classes = true

config.cache_classes = true

If the error is gone, you're lucky. But since it is not feasible to cache classes in development, turn off class caching again and explicitly require the class that couldn't be referred. I.E.:

如果错误消失了,你很幸运。但是由于在开发中缓存类是不可行的,所以再次关闭类缓存并明确要求无法引用的类。即:

top of development.rb

require 'app/models/user'

#1


14  

This problem arises in development mode when config.action_controller.perform_caching = true AND config.cache_classes = false -- it seems ActiveRecord objects cannot be stored with Rails.cache.

当config.action_controller.perform_caching = true和config.cache_classes = false时,在开发模式中出现此问题 - 似乎ActiveRecord对象无法与Rails.cache一起存储。

But if you need to enable config.action_controller.perform_caching in development mode for testing caching, then you must also enable config.cache_classes. This would be temporary, though, because then you'd have to restart the development server after changing classes or files in the asset pipeline.

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

With caching disabled, I would use Rails.cache.write(some_name, some_value) if Rails.env.production? to prevent caching from blowing up in development. Rails.cache.read() doesn't seem to be affected.

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

#2


-1  

Depending on the structure of your application, you might get an error in development like this: TypeError (User can't be referred) This error is caused by some caching-reloading madness: The middleware implanted by some gem is cached. But in development, your classes usually aren't. Thus some classes may not be available under certain circumstances, e.g. if you are using before filters for user authentication provided by some engine. You should be able to get rid of the error above by turning on class caching. Try it (and restart the server afterwards):

根据应用程序的结构,您可能会在开发中遇到如下错误:TypeError(无法引用用户)此错误是由某些缓存重新加载的疯狂引起的:某些gem植入的中间件被缓存。但在开发过程中,你的课程通常不是。因此,某些类别在某些情况下可能无法使用,例如如果您在过滤器之前使用某些引擎提供的用户身份验证。您应该能够通过启用类缓存来摆脱上述错误。试一试(然后重启服务器):

development.rb

config.cache_classes = true

config.cache_classes = true

If the error is gone, you're lucky. But since it is not feasible to cache classes in development, turn off class caching again and explicitly require the class that couldn't be referred. I.E.:

如果错误消失了,你很幸运。但是由于在开发中缓存类是不可行的,所以再次关闭类缓存并明确要求无法引用的类。即:

top of development.rb

require 'app/models/user'