Published on 2011-01-27 by John Collins. Please follow me on Twitter for more:
According to their website:
"Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering."
Memcached is heavily used in medium and large scale web applications, to improve performance by removing unnecessary calls to the database when possible. The basic approach is as follows:
In most cases when data that is frequently accessed is already in the cache, step three will not be required therefore overall performance is improved, as reading from the cache (Memcached in RAM) will always be quicker than reading from the database (MySQL, Postgres, Oracle etc.).
The following sections describe the installation and testing of Memcached on Fedora 8, but are also applicable for many other Linux variants as we will be building the application from source.
You can begin by downloading the version you require from the Memcached home page. In my case I needed 1.2.5 for compatibility reasons with existing applications, however you should use the latest stable version for new green-field projects.
If like me you need an older version, you can download them from here: http://code.google.com/p/memcached/downloads/list
Now cd to where you downloaded the .taz.gz file to and extract it:
[jcollins@redhat ~]$ cd ~/Downloads/memcached [jcollins@redhat memcached]$ tar -zxvf memcached-1.2.5.tar.gz
Now cd into the extracted directory and make and then install (as the root user) Memcached:
[jcollins@redhat memcached]$ cd memcached-1.2.5 [jcollins@redhat memcached-1.2.5]$ ./configure [jcollins@redhat memcached-1.2.5]$ make [jcollins@redhat memcached-1.2.5]$ su [root@redhat memcached-1.2.5]# make install [root@redhat memcached-1.2.5]# exit exit
Now it is time to start Memcached. Here I am running the service under my own user account (jcollins), giving it 256 MB of RAM, on port 11211 on the local IP address:
[jcollins@redhat memcached-1.2.5]$ memcached -d -m 256 -u jcollins -l 127.0.0.1 -p 11211
The first this to do is confirm that it is actually running in memory using the ps command:
[jcollins@redhat memcached-1.2.5]$ ps -ef | grep memcached jcollins 11950 1 0 11:29 ? 00:00:00 memcached -d -m 256 -u jcollins -l 127.0.0.1 -p 11211
If the service is listed as above, then we know that it started up and is running. But what about confirming that it is actually working as a cache? In order to actually use the cache like a normal consumer, I am going to use a simple command line Ruby script that connection to our running Memcached, stores a value there, then reads the value back, which is the basic use case for Memcached.
The script makes use of the Ruby Memcache gem, which is available: http://deveiate.org/projects/RMemCache
You can install this as root (note here I am using JRuby which is my preference, this command would look different in regular Ruby):
[root@redhat memcached-1.2.5]# jruby --command gem install Ruby-MemCache
Now exit back to your usual account, and create this script:
require 'rubygems' require 'memcache' cache = MemCache::new('127.0.0.1:11211', :debug => false, :c_threshold => 100_000, :compression => false, :namespace => 'test') # this value will only live for 1 second in the cache cache.set('author', 'John Collins', 1) puts 'First time from the cache: '+cache.get('author').to_s # now if we wait 2 seconds and try again, the value will have expired from the cache sleep(2) puts 'Second time from the cache: '+cache.get('author').to_s
And when you run the script it will attempt to connect to your new Memcached instance and store the string value "John Collins" there using the unique key "author", with an expiry time of one second. The script will then read the value from the cache within one second which will succeed, and will then attempt to read the same value after two seconds which will fail because the value will have expired from the cache. Here is what the output of running the script looks like:
[jcollins@redhat memcached]$ jruby TestMemcache.rb First time from the cache: John Collins Second time from the cache:
If you are having issues getting this script to work against your local Memcached, try setting the debug flag to true in the constructor on the MemCache object to get a lot more output when running the script:
cache = MemCache::new('127.0.0.1:11211', :debug => true, :c_threshold => 100_000, :compression => false, :namespace => 'test')