Cache for Performance
If your web-apps are ever dealing with large data-sets or complicated translations of data, caching with memcached may provide a clean and effective solution to improving performance. Caching is the act of remembering things for later use, usually complicated objects that take a long time to prepare, but are needed regularly.
Memcache is a free and open-source memory caching system that can be distributed across many physical machines. It is accessed via TCP, and you can even use Telnet to inspect it very easily.
Memcache runs as a service, and is so named memcached (d for daemon, just like the mysqld service).
It is ideally suited to storing small chunks of data by means of a key to index that data; it’s basically a key-value pair setup (think hash in Perl-speak).
It can be used to store database results, API responses, or even page sections after rendering.
Installing Memcached
Memcached should be available via your systems package manager, so on a RedHat based system, as root...
$ yum install memcached
To make sure it starts on system reboot...
$ chkconfig --list memcached memcached 0:off 1:off 2:off 3:on 4:on 5:on 6:off
or to make it so...
$ chconfig --level 345 memcached on
To run it as a daemon...
$ /etc/rc.d/init.d/memcached start
Configuring Memcached
The config file for memcached is found in /etc/sysconfig/memcached but it’s rare to need to come in here. I’ve only needed to modify settings when storing large data-sets, which probably should have been divided up prior to storing in memcache instead.
Telnet to Memcached
To inspect the cache you can simply Telnet to it since it operates over a TCP connection...
$ telnet localhost 11211 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. get foo VALUE foo 0 2 hi END stats STAT pid 6925 STAT uptime 3563289 STAT time 1352832070 STAT version 1.4.4 STAT pointer_size 64 STAT rusage_user 42.625519 STAT rusage_system 47.951710 STAT curr_connections 47 STAT total_connections 2757 STAT connection_structures 52 STAT cmd_get 270308 STAT cmd_set 319948
Perl and Memcached
Accessing a memcached cache from with your Perl code is relatively straight-forward by using Cache::Memcached.
Don't Cache Everything
It is sometimes very tempting to cache every object you have in your code, this is a prime example of solving a problem before it even exists.
Basic calls to get standing-data from a small database table are usually faster than calling the cache for it.
Databases are usually well optimised to cache queries and results, so memcache may not always be the answer.