Memcache

Memcache is a basic key value pair, it can be installed as either the standard memcache package or a whole host of drop-in replacements like couchbase redis or even Amazon’s ElastiCache.

Whether it is installed on a dedicated or a shared server, memcache

Basics

Usage is fast with gets and sets at 100K per second, and usage is simple with basic commands like get, set, delete and flush.

When the memory is full, it uses a least-recently-used algorithm to evict old items.

Basic php example:

<?php
/* connect to memcached server */
$memcache_obj = memcache_connect(‘localhost’, 11211);
$key = ‘key’;
/* Get our item */
$value = memcache_get($memcache_obj, ‘key’);
/* Check if the items exists */
if ($value === false){
$value = ‘testValue’;
memcache_set($memcache_obj, ‘key’, $value, 0, 30);
}
echo $value;

?>

WordPress

WordPress has a built-in object cache not many people know about. By default, it uses non-persistant cache, so it’s useless out of the box.

It caches a tons of wordpress queries and settings to reduces the amount of database interaction.

Depending on how it is set up, it can store objects in memory or disk with a variety of different persistent cache plugins.

In this case, we will use memcache as the object cache.

Setup

  1. Download the wordpress Memcached Object Cache
  2. Move the object-cache.php file to the wp-content root directory
  3. Add your memcache server to wp-config.php (see Memcache Object Cache readme.txt)
  4. Pat yourself on the back.

Benchmark

Running some apacheBench tests on a small local apache virtual server:

With no object cache:

Concurrency Level: 1
Time taken for tests: 10.359 seconds
Complete requests: 10
Failed requests: 0
Write errors: 0
Total transferred: 85790 bytes
HTML transferred: 83520 bytes
Requests per second: 0.97 [#/sec] (mean)
Time per request: 1035.879 [ms] (mean)
Time per request: 1035.879 [ms] (mean, across all concurrent requests)
Transfer rate: 8.09 [Kbytes/sec] received

With Object cache turned on

Concurrency Level: 1
Time taken for tests: 2.168 seconds
Complete requests: 10
Failed requests: 0
Write errors: 0
Total transferred: 86750 bytes
HTML transferred: 84480 bytes
Requests per second: 4.61 [#/sec] (mean)
Time per request: 216.781 [ms] (mean)
Time per request: 216.781 [ms] (mean, across all concurrent requests)
Transfer rate: 39.08 [Kbytes/sec] received

Web Server Optimizations