memcache fetching wrong values while using acts_as_cached plugin

家住魔仙堡 提交于 2019-12-11 01:45:38

问题


I have installed memcached gem and acts_as_cached plugin.

I have few models like

class Bike < ActiveRecord::Base
acts_as_cached
..........
end

class Car < ActiveRecord::Base
acts_as_cached
..........
end
like this i have more models


Bike.get_cache("key") { Bike.find(...) }
Car.get_cache("key") { Car.find(...) }

Even i am maintaining different keys its fetching wrong objects instead of required objects.


回答1:


Phusion Passenger spawns a new servers by forking an existing process. After that the different processes share code. On the one hand this saves a lot of memory compared to mongrel servers for example. On the other side this may confuse memcached if the connection is shared is not properly reseted.

I once had a similar experience when user sessions mixed up. Really scary.

There are two solutions for this problem:

1) Configure Passenger to use "Conservative Spawning" instead of "Smart Spawning". But than you will loose the advantage of shared memory at all. Or

2) Make sure that the memcached connection is reseted if a Passenger spawns a new server process. I use the following code to do so. Put it into config/initializers/memcached_reset.rb

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # Close duplicated memcached connections - they will open themselves
      Rails.cache.instance_variable_get(:"@data").try(:reset) if Rails.cache.instance_variable_get(:"@data").respond_to?(:reset)
    end
  end
end

Find a slightly different example in this blog post.



来源:https://stackoverflow.com/questions/19135856/memcache-fetching-wrong-values-while-using-acts-as-cached-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!