Rails MemCacheStore Fu

Or how to use the latest memcache-client (1.7.4 as of this writing) over the vendored copy in Rails and control the MemCacheStore initialization using the cache_fu/act_as_cached memcached.yml config file.

  1. get Mike Perham’s latest memcache-client on github or simply:
    sudo gem install memcache-client

    make sure to remove any other memcache-client gems from your system.

  2. follow Mike’s instructions to performs brain surgery on the Ruby environment to override ActiveSupport memcache-client
  3. to control the MemCacheStore initialization using the options in your memcached.yml file, grab my memcached-config code from github
  4. include the following code in your environments/*.rb files:
    
    unless (memcached_config = MemcachedConfig.new).disabled?
      memcached_options = memcached_config.options
      config.cache_store = :mem_cache_store, memcached_options.delete(:servers), memcached_options
    end
    

By using MemcachedConfig this way, all the recognized options by memcache-client in your memcached.yml config file, such as :namespace, will be used. You can also add specific memcache-client options such as :failover or :timeout in you memcached.yml.

Tags: ,

3 Responses to “Rails MemCacheStore Fu”

  1. chris says:

    Have you set this up for session storage?

    Also, have you set this up with passenger (e.g. PhusionPassenger.on_event(:starting_worker_process))?

    Thanks.

  2. chris says:

    Also, have you tried this with a vendored version of the memcache-client gem?

    Thanks.

  3. colin says:

    @chris, I haven’t used it for session storage but it should just work. I did not try it with Passenger either.

    As for a vendored version of the memcache-client gem, it should also just work. You can verify in script/console if the correct client has been installed using the following:

    >> MemCache::VERSION
    => “1.7.4″

    >> RAILS_CACHE.instance_variable_get(”@data”).class::VERSION
    => “1.7.4″

    Colin.

Leave a Reply