Ruby on Rails
MemCached

What is memcached?

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load1.

Memcached and Rails

Rails can use memcache to store cache fragments and also as a session store.

Also checkout cache fu and http://github.com/methodmissing/query_memcached/tree/master

+ http://github.com/37signals/libmemcached_store/tree/master

Installation

  1. install MemCached (http://www.danga.com/memcached/)
    • If your distribution is debian (sid or sarge).
      # apt-get install memcached
    • If your distribution is Gentoo.
      # emerge memcached
    • FreeBSD.
      # cd /usr/ports/databases/memcached && make install clean
  2. install memcache-client
    # gem install memcache-client

Setting up MemCached for session storage

Change session store in config/environment.rb:

config.action_controller.session_store = :mem_cache_store

1 taken directly from the memcached home page.

category: Stub

—-

Compiling and Running memcached on OS X

The memcached server, when running under OS X 10.3 and 10.4 (all releases), due to the fubared OS X implementation of kqueue, needs to be patched and recompiled and also needs to be started in a special way, otherwise the response time will be 200 ms for each query. Check out this page: http://www.petercooper.co.uk/archives/000693.html – but note that the instructions are incomplete. Also note that it is not necessary to install DarwinPorts. You can download libevent and compile it without any problems. Same thing with memcached – but you must remember to patch it correctly. This is what you need to do (courtesy of Noah M Daniels):

First, in memcached.c (in the memcached source directory) add (anywhere above line 105, which reads #ifdef TCP_NOPUSH) the line:

    #undef TCP_NOPUSH

I just added it on the line above the #ifdef line.

Rebuild memcached (just do a make && sudo make install, don’t need to re-run configure if you’ve already done it)

Then just make sure you execute the “export EVENT_NOKQUEUE=yes” command before firing up memcached in sh/bash and derivatives. For csh and derivatives, use setenv EVENT_NOKQUEUE 1. Just set the environment variable EVENT_NOKQUEUE to 1 and you should be set.