From f778b61c0358ad2d0593be4040406a418ae46579 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 23 Feb 2012 11:16:08 +0000 Subject: [PATCH] Refactor memcache connection handling --- Gemfile | 3 +-- Gemfile.lock | 14 ++++++-------- config/environments/production.rb | 3 +-- config/initializers/memcached.rb | 9 --------- lib/mem_cache.rb | 31 +++++++++++++++++++++++++++++++ lib/memcache.rb | 0 6 files changed, 39 insertions(+), 21 deletions(-) delete mode 100644 config/initializers/memcached.rb create mode 100644 lib/mem_cache.rb create mode 100644 lib/memcache.rb diff --git a/Gemfile b/Gemfile index 29094f410..c6654cce2 100644 --- a/Gemfile +++ b/Gemfile @@ -38,8 +38,7 @@ gem 'SystemTimer', '>= 1.1.3', :require => 'system_timer', :platforms => :ruby_1 gem 'httpclient' # Load memcache in case we are using it -gem 'memcache-client' -gem 'memcached' +gem 'memcached', '>= 1.4.1' # Gems needed for running tests group :test do diff --git a/Gemfile.lock b/Gemfile.lock index d9cf77253..5686b2c2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -30,7 +30,7 @@ GEM i18n (~> 0.6) multi_json (~> 1.0) addressable (2.2.7) - arel (3.0.1) + arel (3.0.2) builder (3.0.0) cocaine (0.2.1) coffee-rails (3.2.2) @@ -56,7 +56,7 @@ GEM httpclient (2.2.4) i18n (0.6.0) iconv (0.1) - journey (1.0.2) + journey (1.0.3) jquery-rails (2.0.0) railties (>= 3.2.0.beta, < 5.0) thor (~> 0.14) @@ -67,8 +67,7 @@ GEM i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) - memcache-client (1.8.5) - memcached (1.3.7) + memcached (1.4.1) mime-types (1.17.2) multi_json (1.1.0) multipart-post (1.1.5) @@ -84,12 +83,12 @@ GEM multi_json (~> 1.0) open_id_authentication (1.1.0) rack-openid (~> 1.3) - paperclip (2.6.0) + paperclip (2.7.0) activerecord (>= 2.3.0) activesupport (>= 2.3.2) cocaine (>= 0.0.2) mime-types - pg (0.13.1) + pg (0.13.2) polyglot (0.3.3) rack (1.4.1) rack-cache (1.1) @@ -164,8 +163,7 @@ DEPENDENCIES iconv jquery-rails libxml-ruby (>= 2.0.5) - memcache-client - memcached + memcached (>= 1.4.1) oauth-plugin (>= 0.4.0.pre7) open_id_authentication (>= 1.1.0) paperclip (~> 2.0) diff --git a/config/environments/production.rb b/config/environments/production.rb index 95b45339c..e517d2245 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -49,8 +49,7 @@ OpenStreetMap::Application.configure do # Use a different cache store in production if defined?(MEMCACHE_SERVERS) - MEMCACHE = Memcached::Rails.new(MEMCACHE_SERVERS, :binary_protocol => true) - config.cache_store = :mem_cache_store, MEMCACHE + config.cache_store = :mem_cache_store, MemCache.new(:namespace => "rails:cache") end # Enable serving of images, stylesheets, and JavaScripts from an asset server diff --git a/config/initializers/memcached.rb b/config/initializers/memcached.rb deleted file mode 100644 index 7af419112..000000000 --- a/config/initializers/memcached.rb +++ /dev/null @@ -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 diff --git a/lib/mem_cache.rb b/lib/mem_cache.rb new file mode 100644 index 000000000..269b0c3d6 --- /dev/null +++ b/lib/mem_cache.rb @@ -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 diff --git a/lib/memcache.rb b/lib/memcache.rb new file mode 100644 index 000000000..e69de29bb -- 2.43.2