Memcached storage engine for CakePHP, using the memcached extension

Installs: 373

Dependents: 0

Stars: 23

Watchers: 1

Forks: 10

Open Issues: 2

Language: PHP

0.12 2013-10-07 02:04 UTC


This is an alternative memcached cache engine to the memcache engine shipped by default with cakePhp. Default one uses the memcache extension, whereas this one uses the memcached extension. (notice the d)


Benefits of Memcached over Memcache extension

  • Allow binary protocol
  • Increment/Decrement a compressed key
  • Uses igbinary for serialization (memcached has to be compiled with --enable-igbinary)

Igbinary is the big win of the memcached extension.

Igbinary is a drop in replacement for the standard php serializer. Instead of time and space consuming textual representation, igbinary stores php data structures in a compact binary form. Savings are significant when using memcached or similar memory based storages for serialized data. You can expect about 50% reduction in storage requirement and speed is at least on par with the standard PHP serializer. Specific numbers depend on your data, of course.

see https://github.com/phadej/igbinary and some benchmark

For CakePHP 2.5+

As of CakePHP 2.5, the memcached engine will be included in the core by default. No installation required.

For CakePHP 2.2, 2.3 and 2.4



[GIT Submodule]

In your app directory type:

git submodule add -b master git://github.com/kamisama/CakePHP-Memcached-Engine.git Plugin/kamisama/Memcached
git submodule init
git submodule update

[GIT Clone]

In your Plugin directory type:

git clone -b master git://github.com/kamisama/CakePHP-Memcached-Engine.git Memcached


Add kamisama/cakephp-memcached-engine to your composer dependencies, then run

composer install

Enable plugin

You need to enable the plugin your app/Config/bootstrap.php file:


If you are already using CakePlugin::loadAll();, then this is not necessary.


Add this line in the head of your core.php:

App::uses('MemcachedEngine', 'Memcached.Lib/Cache/Engine');

You can then use it in your cache configuration:

Cache::config('default', array('engine' => 'Memcached'));

For CakePHP 2.0 and 2.1


Since defining cache engine in a plugin is not supported yet on these version, you have to :

  • Download the plugin
  • Copy the MemcachedEngine.php file located the Lib/Cache/Engine directory to the Lib/Cache/Engine directory (create it if needed) in your app folder.

Enable plugin

You need to enable the plugin your app/Config/bootstrap.php file:


If you are already using CakePlugin::loadAll();, then this is not necessary.

Cache::config('default', array('engine' => 'Memcached'));


Cache::config('default', array('engine' => 'Memcached'));


The memcached cache engine can take the following options:

Cache::config('router', array(
    # Usual options
    'engine' => 'Memcached',
    'prefix' => 'mc_',
    'duration' => '+7 days', // Expires in 7 days, from now
    'servers' => array(
        '', // Default port 11211
        '' // Or you can specify the port

    # Memcached options
    'compress' => false,
    'persistent' => false,
    'login' => null,
    'password => null,
    'serialize' => 'php'


Default: true
Compress the cached data. Unlike memcache, memcached can increment/decrement compressed keys


Default: false
Use a persistent connection to the memcached server. To enable, set persistent to an unique string, to identify the connection. All configurations using the same persistent value will share a single underlying connection.

login and password

Default: null
The memcached server credidentials, if using SASL for authentication.

Memcached need to be compiled with SASL support, else it'll throw a CacheException.


Default: php
The engine used to serialize the data.

Available serializer supported by memcached:

  • php
  • json
  • igbinary

The memcached extension is by default compiled with the php serializer. To use the other serializer, the memcached extension must be compiled with the appropriate options. Refer to Memcached documentation.

Using an invalid or not supported serializer engine will throw a CacheException.

igbinary is the recommended serializer.


Binary protocol is temporary disabled due to a Memcached issue with increment/decrement


Ver 0.12 (2013-10-06)
  • Code optimization
Ver 0.11 (2013-09-19)
  • Rename serializer setting to serialize
  • Merge persistent_id setting with persitent, which now takes a string as argument
Ver 0.10 (2013-09-18)
  • Add options to select the serializer engine
Ver 0.9 (2013-09-04)
  • Fix copy/paste mistake preventing test to run
Ver 0.8 (2013-09-04)
  • Minor code optimization
  • Throw a CacheException when trying to use authentication with Memcached extension installed without SASL support

NOTE: As of CakePHP 2.5, MemcachedEngine v0.8 will be included into the core.

Ver 0.7 (2013-08-26)
  • Merge AmazonElastiCache support back into MemcachedEngine
  • Coding standard fixes
Ver 0.6 (2013-08-26)
  • Disable binary protocol due to a Memcached issue with increment/decrement view issue
  • Add tests
  • Add missing comma
  • Add groups support
  • Use Memcached::getAllKeys() to manage cache clearing
Ver 0.5 (2013-08-26)
  • Pluginize cache engine (@josegonzalez)
  • Add support for SASL authentication (@josegonzalez)
Ver 0.4 (2013-08-18)
  • Fix #6: init() a second persistent connection returns false
  • Add persistent_id option to create separate persistent connection
  • Skip duplicate when searching for key to clear
Ver 0.3 (2012-08-29)
  • Code formatted to Cake standard
Ver 0.2 (2012-03-22)
  • Implemented Cache::clear()


This plugin is released under the MIT licence