Permalink
Browse files

Import libmemcached_store

  • Loading branch information...
Jeff Hardy
Jeff Hardy committed Jun 12, 2008
1 parent 426b7a7 commit ec395872930eaed680b16cfc2cc5fd4015086aec
Showing with 256 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +47 −0 README
  3. +22 −0 Rakefile
  4. +1 −0 init.rb
  5. +108 −0 lib/active_support/cache/libmemcached_store.rb
  6. +58 −0 test/libmemcached_store_test.rb
View
@@ -0,0 +1,20 @@
+Copyright (c) 2008 37signals
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
47 README
@@ -0,0 +1,47 @@
+= LibmemcachedStore
+
+An ActiveSupport cache store that uses the C-based libmemcached client through
+Evan Weaver's Ruby/SWIG wrapper, memcached. libmemcached is fast, lightweight,
+and supports consistent hashing, non-blocking IO, and graceful server failover.
+
+== Prerequisites
+
+You'll need both the libmemcached client and the memcached gem:
+
+* http://tangent.org/552/libmemcached.html
+* http://blog.evanweaver.com/files/doc/fauna/memcached
+
+Make sure you install libmemcached first, before you try installing the gem. If
+you're using OS X, the easiest way to install libmemcached is through MacPorts:
+
+ sudo port install libmemcached @0.20
+
+For other platforms, download and extract the libmemcached tarball and install
+manually:
+
+ ./configure
+ make && sudo make install
+
+Once libmemcached is installed, install the memcached gem:
+
+ gem install memcached --no-rdoc --no-ri
+
+== Usage
+
+This is a drop-in replacement for the memcache store that ships with Rails. To
+enable, set the <tt>config.cache_store</tt> option to <tt>:libmemcached_store</tt>
+in the config for your environment
+
+ config.cache_store = :libmemcached_store
+
+If no servers are specified, localhost is assumed. You can specify a list of
+server addresses, either as hostnames or IP addresses, with or without a port
+designation. If no port is given, 11211 is assumed:
+
+ config.cache_store = :libmemcached_store, %w(cache-01 cache-02 127.0.0.1:11212)
+
+== Props
+
+Thanks to Brian Aker (http://tangent.org) for creating libmemcached, and Evan
+Weaver (http://blog.evanweaver.com) for the Ruby wrapper.
+
View
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the libmemcached_store plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the libmemcached_store plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'LibmemcachedStore'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
@@ -0,0 +1 @@
+config.gem 'memcached', :version => '>=0.10'
@@ -0,0 +1,108 @@
+require 'memcached'
+
+class Memcached
+ # The latest version of memcached (0.10) doesn't support hostname lookups,
+ # however, the underlying libmemcached library does. Unfortunately, set_servers
+ # will raise if a hostname is used, so we overwrite it here.
+ def set_servers(servers)
+ [*servers].each_with_index do |server, index|
+ host, port = server.split(":")
+ Lib.memcached_server_add(@struct, host, port.to_i)
+ end
+ end
+end
+
+module ActiveSupport
+ module Cache
+ class LibmemcachedStore < Store
+ attr_reader :addresses
+
+ DEFAULT_OPTIONS = {
+ :distribution => :consistent,
+ :no_block => true,
+ :failover => true
+ }
+
+ def initialize(*addresses)
+ addresses.flatten!
+ options = addresses.extract_options!
+ addresses = %w(localhost) if addresses.empty?
+
+ @addresses = addresses
+ @cache = Memcached.new(@addresses, options.reverse_merge(DEFAULT_OPTIONS))
+ end
+
+ def read(key, options={})
+ super
+ @cache.get(key, marshal?(options))
+ rescue Memcached::Error => e
+ log_error(e)
+ nil
+ end
+
+ # Set the key to the given value. Pass :unless_exist => true if you want to
+ # skip setting a key that already exists.
+ def write(key, value, options={})
+ super
+ @cache.send(options[:unless_exist] ? :add : :set, key, value, expires_in(options), marshal?(options))
+ true
+ rescue Memcached::Error => e
+ log_error(e)
+ false
+ end
+
+ def delete(key, options={})
+ super
+ @cache.delete(key, expires_in(options))
+ true
+ rescue Memcached::Error => e
+ log_error(e)
+ false
+ end
+
+ def exist?(key, options={})
+ !read(key, options).nil?
+ end
+
+ def increment(key, amount=1)
+ log 'incrementing', key, amount
+ @cache.incr(key, amount)
+ rescue Memcached::Error
+ nil
+ end
+
+ def decrement(key, amount=1)
+ log 'decrementing', key, amount
+ @cache.decr(key, amount)
+ rescue Memcached::Error
+ nil
+ end
+
+ def delete_matched(matcher, options={})
+ super
+ raise NotImplementedError
+ end
+
+ def clear
+ @cache.flush
+ end
+
+ def stats
+ @cache.stats
+ end
+
+ private
+ def expires_in(options)
+ options[:expires_in] || 0
+ end
+
+ def marshal?(options)
+ !options[:raw]
+ end
+
+ def log_error(exception)
+ logger.error "MemcachedError (#{exception.inspect}): #{exception.message}" if logger && !@logger_off
+ end
+ end
+ end
+end
@@ -0,0 +1,58 @@
+require 'test/unit'
+require 'rubygems'
+require 'active_support'
+require 'memcached'
+
+require File.dirname(__FILE__) + '/../lib/active_support/cache/libmemcached_store'
+
+# Make it easier to get at the underlying cache options during testing.
+class ActiveSupport::Cache::LibmemcachedStore
+ delegate :options, :to => '@cache'
+end
+
+class LibmemcachedStoreTest < Test::Unit::TestCase
+ def setup
+ @store = ActiveSupport::Cache.lookup_store :libmemcached_store
+ end
+
+ def test_should_identify_cache_store
+ assert_kind_of ActiveSupport::Cache::LibmemcachedStore, @store
+ end
+
+ def test_should_set_server_addresses_to_localhost_if_none_are_given
+ assert_equal %w(localhost), @store.addresses
+ end
+
+ def test_should_set_custom_server_addresses
+ store = ActiveSupport::Cache.lookup_store :libmemcached_store, 'localhost', '192.168.1.1'
+ assert_equal %w(localhost 192.168.1.1), store.addresses
+ end
+
+ def test_should_enable_consistent_hashing_by_default
+ assert_equal :consistent, @store.options[:distribution]
+ end
+
+ def test_should_enable_non_blocking_io_by_default
+ assert_equal true, @store.options[:no_block]
+ end
+
+ def test_should_enable_server_failover_by_default
+ assert_equal true, @store.options[:failover]
+ end
+
+ def test_should_allow_configuration_of_custom_options
+ options = {
+ :namespace => 'test',
+ :distribution => :modula,
+ :no_block => false,
+ :failover => false
+ }
+
+ store = ActiveSupport::Cache.lookup_store :libmemcached_store, 'localhost', options
+
+ assert_equal 'test', store.options[:namespace]
+ assert_equal :modula, store.options[:distribution]
+ assert_equal false, store.options[:no_block]
+ assert_equal false, store.options[:failover]
+ end
+end

0 comments on commit ec39587

Please sign in to comment.