いいえRails.cache.fetch
を使ってキャッシュを実装しています。しかし、ある特定のインスタンスでは、時々私は例外が発生します。Rails.cache.fetch例外:TypeError(<ModelName>は参照できません)
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'
フェッチは、常に最初の試行で(上で)爆発し、その後限りフェッチが有効期限内であると正常に動作しますようです。私は何かが欠けていることを知っているので、新鮮な目のペアがいいでしょう。ここで
は、キャッシュがフェッチ呼び出す方法があります:
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
が必要です。今はうまくいくと思われますが、私は経験した例外について誰か他の人が何か言いたいことを学ぶことに興味があります。 –
私はこれを 'Rails.cache.write()'と '.read()'で今度だけ再度実行しました: "' TypeError(は参照できません) '" –
ロードされるEmloクラス、またはレールはmemcacheに含まれているものを逆シリアル化する方法を知らない。 –