Commit 10a4c31c authored by Jeremy's avatar Jeremy

Issue #2696129 by Jeremy: Add support for msgpack serialize/unserialize

parent 9cf27b7a
......@@ -298,41 +298,55 @@ except for the 'cache_page' bin which will use the 'something_else_unique'
prefix. Not that if using a keyed array for specifying prefix, you must specify
the 'default' prefix.
## EXPERIMENTAL - ALTERNATIVE SERIALIZE: IGBINARY ##
## EXPERIMENTAL - ALTERNATIVE SERIALIZE ##
This is a new experimental feature added to the memcache module in version
7.x-1.6 and should be tested carefully before utilizing in production.
To optimize how data is serialized before it is written to memcache, you can
enable the igbinary PHP extension which converts from using PHP's human
readable serialized data structures to a compact binary format. The igbinary
documentation claims on average a 50% reducation in storage requirements,
reducing the amount of traffic sent over the network. It goes on to explain:
"Unserialization performance is at least on par with the standard PHP
serializer. Serialization performance depends on the 'compact_strings'
option which enables duplicate string tracking. String are inserted to a
hash table which adds some overhead. In usual scenarios this does not have
much significance since usage pattern is 'serialize rarely, unserialize
often'. With 'compact_strings' option igbinary is usually a bit slower
than the standard serializer. Without it, a bit faster."
If the igbinary extension is enabled, the memcache modue will use it by
default. You can verify which serialize function is being used by enabling the
memcache_admin module and visiting admin/reports/memcache. To disable igbinary
when the php extension is installed add the following to your settings.php and
then restart all memcached daemons:
$conf['memcache_enable_igbinary'] = FALSE;
The project is maintained on GitHub:
enable either the igbinary or msgpack PECL extension. Both switch from using
PHP's own human-readable serialized data strucutres to more compact binary
formats.
No specicial configuration is required. If both extensions are enabled,
memcache will automatically use the igbinary extension. If only one extension
is enabled, memcache will automatically use that extension.
You can optionally specify which extension is used by adding one of the
following to your settings.php:
// Force memcache to use PHP's core serialize functions
$conf['memcache_serialize'] = 'serialize';
// Force memcache to use the igbinary serialize functions (if available)
$conf['memcache_serialize'] = 'igbinary';
// Force memcache to use the msgpack serialize functions (if available)
$conf['memcache_serialize'] = 'msgpack';
To review which serialize function is being used, enable the memcache_admin
module and visit admin/reports/memcache.
IGBINARY:
The igbinary project is maintained on GitHub:
- https://github.com/phadej/igbinary
The official PECL package can be found at:
The official igbinary PECL extension can be found at:
- https://pecl.php.net/package/igbinary
Version 2.0.1 or greater is recommended.
MSGPACK:
The msgpack project is maintained at:
- https://msgpack.org
The official msgpack PECL extension can be found at:
- https://pecl.php.net/package/msgpack
Version 2.0.2 or greater is recommended.
## MAXIMUM LENGTHS ##
If the length of your prefix + key + bin combine to be more than 250 characters,
......
......@@ -149,12 +149,8 @@ function _dmemcache_set_pieces($key, $value, $exp = 0, $bin = 'cache', $mc = NUL
$serialized = FALSE;
}
else {
if (extension_loaded('igbinary') && variable_get('memcache_enable_igbinary', TRUE)) {
$data = igbinary_serialize($value);
}
else {
$data = serialize($value);
}
$serialize_function = dmemcache_serialize();
$data = $serialize_function($value);
$serialized = TRUE;
}
......@@ -335,12 +331,8 @@ function _dmemcache_get_pieces($item, $key, $bin = 'cache', $mc = NULL) {
return $data;
}
else {
if (extension_loaded('igbinary') && variable_get('memcache_enable_igbinary', TRUE)) {
return igbinary_unserialize($data);
}
else {
return unserialize($data);
}
$unserialize_function = dmemcache_unserialize();
return $unserialize_function($value);
}
}
......@@ -640,6 +632,78 @@ function dmemcache_extension() {
return $extension;
}
/**
* Determine which serialize extension to use: serialize (none), igbinary,
* or msgpack.
*
* By default we prefer the igbinary extension, then the msgpack extension,
* then the core serialize functions. This can be overridden in settings.php.
*/
function dmemcache_serialize_extension() {
static $extension = NULL;
if ($extension === NULL) {
$preferred = strtolower(variable_get('memcache_serialize', NULL));
// Fastpath if we're forcing php's own serialize function.
if ($preferred == 'serialize') {
$extension = $preferred;
}
// Otherwise, find an available extension favoring configuration.
else {
$igbinary_available = extension_loaded('igbinary');
$msgpack_available = extension_loaded('msgpack');
if ($preferred == 'igbinary' && $igbinary_available) {
$extension = $preferred;
}
elseif ($preferred == 'msgpack' && $msgpack_available) {
$extension = $preferred;
}
else {
// No (valid) serialize extension specified, try igbinary.
if ($igbinary_available) {
$extension = 'igbinary';
}
// Next try msgpack.
else if ($msgpack_available) {
$extension = 'msgpack';
}
// Finally fall back to core serialize.
else {
$extension = 'serialize';
}
}
}
}
return $extension;
}
/**
* Return proper serialize function.
*/
function dmemcache_serialize() {
switch (dmemcache_serialize_extension()) {
case 'igbinary':
return 'igbinary_serialize';
case 'msgpack':
return 'msgpack_pack';
default:
return 'serialize';
}
}
/**
* Return proper unserialize function.
*/
function dmemcache_unserialize() {
switch (dmemcache_serialize_extension()) {
case 'igbinary':
return 'igbinary_unserialize';
case 'msgpack':
return 'msgpack_unpack';
default:
return 'unserialize';
}
}
/**
* Return a new memcache instance.
*/
......
......@@ -338,11 +338,9 @@ function memcache_admin_stats($bin = 'default') {
$item = array(
'label' => t('Serialize function')
);
if (extension_loaded('igbinary') && variable_get('memcache_enable_igbinary', TRUE)) {
$serialize_function = t('igbinary_serialize v!version', array('!version' => phpversion('igbinary')));
}
else {
$serialize_function = 'serialize';
$serialize_function = dmemcache_serialize();
if ($serialize_function != 'serialize') {
$serialize_function = t('!function v!version', array('!function' => $serialize_function, '!version' => phpversion(dmemcache_serialize_extension())));
}
if (count($aggregate)) {
$report['uptime'][] = $item;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment