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), (*1)
Background
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., (*2)
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., (*3)
see https://github.com/phadej/igbinary
and some benchmark, (*4)
For CakePHP 2.5+
As of CakePHP 2.5, the memcached engine will be included in the core by default. No installation required., (*5)
For CakePHP 2.2, 2.3 and 2.4
Installation
[Manual], (*6)
[GIT Submodule], (*7)
In your app directory type:, (*8)
git submodule add -b master git://github.com/kamisama/CakePHP-Memcached-Engine.git Plugin/kamisama/Memcached
git submodule init
git submodule update
[GIT Clone], (*9)
In your Plugin
directory type:, (*10)
git clone -b master git://github.com/kamisama/CakePHP-Memcached-Engine.git Memcached
[Composer], (*11)
Add kamisama/cakephp-memcached-engine to your composer dependencies, then run, (*12)
composer install
Enable plugin
You need to enable the plugin your app/Config/bootstrap.php
file:, (*13)
CakePlugin::load('Memcached');
If you are already using CakePlugin::loadAll();
, then this is not necessary., (*14)
Usage
Add this line in the head of your core.php:, (*15)
App::uses('MemcachedEngine', 'Memcached.Lib/Cache/Engine');
You can then use it in your cache configuration:, (*16)
Cache::config('default', array('engine' => 'Memcached'));
For CakePHP 2.0 and 2.1
Installation
Since defining cache engine in a plugin is not supported yet on these version, you have to :, (*17)
-
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:, (*18)
CakePlugin::load('Memcached');
If you are already using CakePlugin::loadAll();
, then this is not necessary., (*19)
Cache::config('default', array('engine' => 'Memcached'));
Usage
Cache::config('default', array('engine' => 'Memcached'));
Settings
The memcached cache engine can take the following options:, (*20)
Cache::config('router', array(
# Usual options
'engine' => 'Memcached',
'prefix' => 'mc_',
'duration' => '+7 days', // Expires in 7 days, from now
'servers' => array(
'127.0.0.1', // Default port 11211
'127.0.0.1:11212' // Or you can specify the port
)
# Memcached options
'compress' => false,
'persistent' => false,
'login' => null,
'password => null,
'serialize' => 'php'
));
compress
Default: true
Compress the cached data. Unlike memcache, memcached can increment/decrement compressed keys, (*21)
persistent
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., (*22)
login and password
Default: null
The memcached server credidentials, if using SASL for authentication., (*23)
Memcached need to be compiled with SASL support, else it'll throw a CacheException
., (*24)
serialize
Default: php
The engine used to serialize the data., (*25)
Available serializer supported by memcached:, (*26)
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., (*27)
Using an invalid or not supported serializer engine will throw a CacheException
., (*28)
igbinary is the recommended serializer., (*29)
Notes
Binary protocol is temporary disabled due to a Memcached issue with increment/decrement, (*30)
Changelog
Ver 0.12 (2013-10-06)
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., (*31)
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()
License
This plugin is released under the MIT licence, (*32)