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 }
# 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/
# If Ruby thinks it's already loaded memcache.rb, force
# a reload otherwise just require.
if $".find { |file| file =~ /Amemcache.rbZ/ }
  load 'memcache.rb'
  require 'memcache'

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