问题
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