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.rb\Z/ } load 'memcache.rb' else require 'memcache' end
If someone knows of a cleaner way to do this, please let me know.
13 responses so far ↓
1 JGeiger // Mar 3, 2009 at 10:03 am
Thanks.
2 fazibear // Mar 13, 2009 at 6:09 am
It’s cleaner if you move it to config/initializers/memcache_fix.rb
3 mperham // Mar 13, 2009 at 6:26 am
Thanks, good idea. I forgot that load order doesn’t matter since we’re unloading everything. I’ve updated the post.
4 fazibear // Mar 13, 2009 at 6:44 am
Wait! I think it does matter. It won’t work from initializers scripts! But why ?
5 Jerrett // May 13, 2009 at 7:32 pm
Yikes, that sucks. Hopefully they will add the 1.7.x to rails so we don’t have to do this
6 eventually consistent » Blog Archive » Rails MemCacheStore Fu // Jun 10, 2009 at 10:46 pm
[...] Mike’s instructions to performs brain surgery on the Ruby environment to override ActiveSupport [...]
7 Colin Surprenant // Jun 10, 2009 at 11:23 pm
Thanks, that worked for me with memcache-client 1.7.4 and Rails 2.2.2.
I have also written a quick tutorial on how to do this and also control the MemCacheStore options using a memcached.yml config file (from cache_fu/acts_as_cached) on http://eventuallyconsistent.com/blog/2009/06/rails-memcachestore-fu/
Colin.
8 chris // Jul 1, 2009 at 6:04 pm
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?
9 mperham // Jul 1, 2009 at 6:45 pm
I guess not.
10 FoundChris » Blog Archive » Using the latest memcache-client in Rails // Jul 2, 2009 at 6:12 pm
[...] Perham, the memcache-client maintainer, documented how to do this (”Using memcache-client 1.6.x in Rails < 2.3″). His hack works great if the [...]
11 chris // Jul 2, 2009 at 6:13 pm
Here is how I got 1.7.4 going without relying on an external gem:
http://www.foundchris.com/2009/07/02/using-the-latest-memcache-client-in-rails/
Cheers.
12 Mike Subelsky // Jul 27, 2009 at 3:16 pm
Here’s how I’m using the vendored gem in Rails 2.2.x:
http://gist.github.com/156741
13 Ruby Memcache Client using Unix Sockets « Blog | Confiz Solutions // Nov 2, 2010 at 6:40 am
[...] memcache-client in Rails < 2.3, we need to perform some brain-surgery originally described here, but modified slightly to fit our [...]
Leave a Comment