Refactor memcache connection handling

This commit is contained in:
Tom Hughes 2012-02-23 11:16:08 +00:00
parent af13c42322
commit f778b61c03
6 changed files with 39 additions and 21 deletions

View file

@ -38,8 +38,7 @@ gem 'SystemTimer', '>= 1.1.3', :require => 'system_timer', :platforms => :ruby_1
gem 'httpclient' gem 'httpclient'
# Load memcache in case we are using it # Load memcache in case we are using it
gem 'memcache-client' gem 'memcached', '>= 1.4.1'
gem 'memcached'
# Gems needed for running tests # Gems needed for running tests
group :test do group :test do

View file

@ -30,7 +30,7 @@ GEM
i18n (~> 0.6) i18n (~> 0.6)
multi_json (~> 1.0) multi_json (~> 1.0)
addressable (2.2.7) addressable (2.2.7)
arel (3.0.1) arel (3.0.2)
builder (3.0.0) builder (3.0.0)
cocaine (0.2.1) cocaine (0.2.1)
coffee-rails (3.2.2) coffee-rails (3.2.2)
@ -56,7 +56,7 @@ GEM
httpclient (2.2.4) httpclient (2.2.4)
i18n (0.6.0) i18n (0.6.0)
iconv (0.1) iconv (0.1)
journey (1.0.2) journey (1.0.3)
jquery-rails (2.0.0) jquery-rails (2.0.0)
railties (>= 3.2.0.beta, < 5.0) railties (>= 3.2.0.beta, < 5.0)
thor (~> 0.14) thor (~> 0.14)
@ -67,8 +67,7 @@ GEM
i18n (>= 0.4.0) i18n (>= 0.4.0)
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
memcache-client (1.8.5) memcached (1.4.1)
memcached (1.3.7)
mime-types (1.17.2) mime-types (1.17.2)
multi_json (1.1.0) multi_json (1.1.0)
multipart-post (1.1.5) multipart-post (1.1.5)
@ -84,12 +83,12 @@ GEM
multi_json (~> 1.0) multi_json (~> 1.0)
open_id_authentication (1.1.0) open_id_authentication (1.1.0)
rack-openid (~> 1.3) rack-openid (~> 1.3)
paperclip (2.6.0) paperclip (2.7.0)
activerecord (>= 2.3.0) activerecord (>= 2.3.0)
activesupport (>= 2.3.2) activesupport (>= 2.3.2)
cocaine (>= 0.0.2) cocaine (>= 0.0.2)
mime-types mime-types
pg (0.13.1) pg (0.13.2)
polyglot (0.3.3) polyglot (0.3.3)
rack (1.4.1) rack (1.4.1)
rack-cache (1.1) rack-cache (1.1)
@ -164,8 +163,7 @@ DEPENDENCIES
iconv iconv
jquery-rails jquery-rails
libxml-ruby (>= 2.0.5) libxml-ruby (>= 2.0.5)
memcache-client memcached (>= 1.4.1)
memcached
oauth-plugin (>= 0.4.0.pre7) oauth-plugin (>= 0.4.0.pre7)
open_id_authentication (>= 1.1.0) open_id_authentication (>= 1.1.0)
paperclip (~> 2.0) paperclip (~> 2.0)

View file

@ -49,8 +49,7 @@ OpenStreetMap::Application.configure do
# Use a different cache store in production # Use a different cache store in production
if defined?(MEMCACHE_SERVERS) if defined?(MEMCACHE_SERVERS)
MEMCACHE = Memcached::Rails.new(MEMCACHE_SERVERS, :binary_protocol => true) config.cache_store = :mem_cache_store, MemCache.new(:namespace => "rails:cache")
config.cache_store = :mem_cache_store, MEMCACHE
end end
# Enable serving of images, stylesheets, and JavaScripts from an asset server # Enable serving of images, stylesheets, and JavaScripts from an asset server

View file

@ -1,9 +0,0 @@
if defined?(PhusionPassenger) and defined?(MEMCACHE_SERVERS)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
MEMCACHE = MEMCACHE.clone
RAILS_CACHE = ActiveSupport::Cache::MemCacheStore.new(MEMCACHE, :compress => true)
ActionController::Base.cache_store = RAILS_CACHE
end
end
end

31
lib/mem_cache.rb Normal file
View file

@ -0,0 +1,31 @@
class MemCache < Memcached::Rails
DEFAULT_OPTIONS = Memcached::DEFAULTS.merge(Memcached::Rails::DEFAULTS)
MemCacheError = Memcached::Error
@@connections = []
def initialize(options = {})
options.reverse_merge! :binary_protocol => true, :namespace_separator => ":"
super(MEMCACHE_SERVERS, options)
@@connections.push(self)
ObjectSpace.define_finalizer(self, lambda { |connection|
@@connections.remove(connection)
})
end
def self.connections
@@connections
end
end
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
MemCache.connections.each { |connection| connection.reset }
end
end
end

0
lib/memcache.rb Normal file
View file