]> git.openstreetmap.org Git - rails.git/blobdiff - lib/mem_cache.rb
Refactor memcache connection handling
[rails.git] / lib / mem_cache.rb
diff --git a/lib/mem_cache.rb b/lib/mem_cache.rb
new file mode 100644 (file)
index 0000000..269b0c3
--- /dev/null
@@ -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