memcache-client-stats

Rubyforge Project:

rubyforge.org/projects/memcacheclientstats

About

memcache-client-stats is an add on to memcache-client that adds the ability to query memcache server stats from the client.

Installing memcache-client-stats

Just install the gem

$ sudo gem install memcache-client-stats

Using memcache-client-stats

Create a new MemCache instance

>> require 'memcache_client_stats'
>> CACHE = MemCache.new :c_threshold => 10_000,
                     :compression => true,
                     :debug => false,
                     :namespace => 'my_namespace',
                     :readonly => false,
                     :urlencode => false
>> CACHE.servers = ['192.168.0.100:11211', '192.168.0.101:11211']

You can then query the cluster for the stats. You can request all the stats for all the servers in the memcache cluster:

>> CACHE.stats
=> {"192.168.0.100:11211" =>
     {"bytes" => "211986835",
      "pid" => "1942",
      "connection_structures" => "75",
      "time" => "1151612629",
      "limit_maxbytes" => "268435456",
      "cmd_get" => "6484010",
      "version" => "1.1.12",
      "bytes_written" => "930827121",
      "cmd_set" => "6274655",
      "get_misses" => "1930031",
      "total_connections" => "115",
      "curr_connections" => "17",
      "curr_items" => "1040445",
      "uptime" => "2486978",
      "get_hits" => "4553979",
      "total_items" => "6274655",
      "rusage_system" => "781.900865",
      "rusage_user" => "392.372521",
      "bytes_read" => "1476127655
     },
    "192.168.0.101:11211" =>
     {"bytes" => "210778808",
      "pid" => "9993",
      "connection_structures" => "237",
      "time" => "1151612630",
      "limit_maxbytes" => "268435456",
      "cmd_get" => "23683207",
      "version" => "1.1.12",
      "bytes_written" => "3407983178",
      "cmd_set" => "22942962",
      "get_misses" => "5708419",
      "total_connections" => "1948",
      "curr_connections" => "17",
      "curr_items" => "1073593",
      "uptime" => "5782293",
      "get_hits" => "17974788",
      "total_items" => "22942962",
      "rusage_system" => "2279.002428",
      "rusage_user" => "1287.636472",
      "bytes_read" => "5160467749
     }
   }

Or you can request a single stat from all servers:

>> CACHE.stats('bytes')
=> {"192.168.0.53:11211" = >"211985845", "192.168.0.50:11211" => "210776435"}

You can also have the client summarize the statistic across the cluster:

>> CACHE.summed_stat('curr_items')
=> 2114039
>> CACHE.summed_stat('bytes') / 1.megabyte
=> 403

Using memcache-client-stats with Rails

memcache-client-stats includes the MemCache.load_config method that will automatically load the memcache configuration and initialize the client based on a .yml configuration file (think database.yml for memcache-client). An example file is available at examples/memcache.yml.

You can configure Rails to use memcache-client by adding the following to config/environment.rb or config/environment/*.rb

require 'memcache_client_stats'
CACHE = MemCache.load_config("#{RAILS_ROOT}/config/memcache.yml")
config.action_controller.session_store = :mem_cache_store
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge! :cache => CACHE