Using memcache-client 1.6.x in Rails < 2.3

Jemery Kemper recently upgraded Rails 2.3′s vendored copy of memcache-client to the 1.6.4 release. But what do you do if you are running Rails 2.1/2.2 and want to take advantage of the massive speedup in 1.6.x? You write some really ugly code that performs brain surgery on the Ruby environment to override ActiveSupport. Create config/initializers/memcache-client-upgrade.rb with this code:

require 'rubygems'

# Brain surgery to use our own version of memcache-client without
# having to modify activesupport directly.
# Unload any previous instance of the class
if Object.const_defined? :MemCache
  Object.instance_eval { remove_const :MemCache }
end
# Pull in the exact version we want
gem 'memcache-client', '1.6.5'
# Ensure that the memcache-client path is at the front of the loadpath
$LOAD_PATH.each do |path|
  if path =~ /memcache-client/
    $LOAD_PATH.delete(path)
    $LOAD_PATH.unshift(path)
  end
end
# If Ruby thinks it's already loaded memcache.rb, force
# a reload otherwise just require.
if $".find { |file| file =~ /Amemcache.rbZ/ }
  load 'memcache.rb'
else
  require 'memcache'
end

If someone knows of a cleaner way to do this, please let me know.

13 thoughts on “Using memcache-client 1.6.x in Rails < 2.3”

  1. Does this work if memcache-client 1.7.4 is vendored?

    I am getting this error:
    Could not find RubyGem memcache-client (= 1.7.4)

    On this line:
    # Pull in the exact version we want
    gem ‘memcache-client’, ’1.7.4′

    Any tips?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>