memcached snippets

How to install memcached on OSX Snow Leopard

Tagged memcached, snowleopard, install  Languages bash

These instructions are from wincent.com:

cd /usr/local/src
curl -O http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar xzvf libevent-1.4.12-stable.tar.gz 
cd libevent-1.4.12-stable
./configure
make
make verify
sudo make install


cd /usr/local/src
curl -O http://memcached.googlecode.com/files/memcached-1.4.1.tar.gz
tar xzvf memcached-1.4.1.tar.gz 
 cd memcached-1.4.1
./configure
make
make test
sudo make install

memcached -d -P pidfile -l 127.0.0.1

Rails Memcached Status Plugin

Tagged status, rails, middleware, rack, memcached  Languages ruby

Use this Rack Middleware to check the status of memcached:

require 'socket'

class MemcachedStatus
  def initialize(app, uri)
    @app = app
    @uri = uri
  end

  def call(env)
    if env['PATH_INFO'] == @uri
      status 
    else
      @app.call(env)
    end
  end
  
  def status
    socket = TCPSocket.open('localhost', '11211')
    socket.send("stats\r\n", 0)

    statistics = []
    loop do
      data = socket.recv(4096)
      if !data || data.length == 0
        break
      end
      statistics << data
      if statistics.join.split(/\n/)[-1] =~ /END/
        break
      end
    end

    out = "
<html>
<body>
<pre>
#{statistics.join}
</pre>
</body>
</html>
"
    [200, {"Content-Type" => "text/html"}, [out]]
  ensure
    socket.close
  end
end

Add the following to config/application.rb:

config.middleware.use "MemcachedStatus", "/memcached_status"

Access status from http://localhost/memcached_status

Socket code from http://barkingiguana.com/2009/03/04/memcache-statistics-from-the-command-line/