Skip to content

Commit abd0cae

Browse files
committed
Add INI option to set the default serializer.
Added new and updated tests to match ini setting change.
1 parent 39d0bee commit abd0cae

File tree

5 files changed

+117
-5
lines changed

5 files changed

+117
-5
lines changed

php_memcached.c

+30-5
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@ static PHP_INI_MH(OnUpdateCompressionType)
227227
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
228228
}
229229

230+
static PHP_INI_MH(OnUpdateSerializer)
231+
{
232+
if (!new_value) {
233+
MEMC_G(serializer) = SERIALIZER_DEFAULT;
234+
} else if (new_value_length == sizeof("php") - 1 &&
235+
strncmp(new_value, "php", sizeof("php") - 1) == 0) {
236+
MEMC_G(serializer) = SERIALIZER_PHP;
237+
#ifdef HAVE_MEMCACHE_IGBINARY
238+
} else if (new_value_length == sizeof("igbinary") - 1 &&
239+
strncmp(new_value, "igbinary", sizeof("igbinary") - 1) == 0) {
240+
MEMC_G(serializer) = SERIALIZER_IGBINARY;
241+
#endif // IGBINARY
242+
#ifdef HAVE_JSON_API
243+
} else if (new_value_length == sizeof("json") - 1 &&
244+
strncmp(new_value, "json", sizeof("json") - 1) == 0) {
245+
MEMC_G(serializer) = SERIALIZER_JSON;
246+
} else if (new_value_length == sizeof("json_array") - 1 &&
247+
strncmp(new_value, "json_array", sizeof("json_array") - 1) == 0) {
248+
MEMC_G(serializer) = SERIALIZER_JSON_ARRAY;
249+
#endif // JSON
250+
} else {
251+
return FAILURE;
252+
}
253+
254+
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
255+
}
256+
230257
/* {{{ INI entries */
231258
PHP_INI_BEGIN()
232259
#if HAVE_MEMCACHED_SESSION
@@ -238,6 +265,7 @@ PHP_INI_BEGIN()
238265
STD_PHP_INI_ENTRY("memcached.compression_factor", "1.3", PHP_INI_ALL, OnUpdateReal, compression_factor, zend_php_memcached_globals, php_memcached_globals)
239266
STD_PHP_INI_ENTRY("memcached.compression_threshold", "2000", PHP_INI_ALL, OnUpdateLong, compression_threshold, zend_php_memcached_globals, php_memcached_globals)
240267

268+
STD_PHP_INI_ENTRY("memcached.serializer", SERIALIZER_DEFAULT_NAME, PHP_INI_ALL, OnUpdateSerializer, serializer_name, zend_php_memcached_globals, php_memcached_globals)
241269
PHP_INI_END()
242270
/* }}} */
243271

@@ -2565,11 +2593,8 @@ static void php_memc_init_globals(zend_php_memcached_globals *php_memcached_glob
25652593
MEMC_G(sess_lock_key) = NULL;
25662594
MEMC_G(sess_lock_key_len) = 0;
25672595
#endif
2568-
#ifdef HAVE_MEMCACHE_IGBINARY
2569-
MEMC_G(serializer) = SERIALIZER_IGBINARY;
2570-
#else
2571-
MEMC_G(serializer) = SERIALIZER_PHP;
2572-
#endif
2596+
MEMC_G(serializer_name) = NULL;
2597+
MEMC_G(serializer) = SERIALIZER_DEFAULT;
25732598
MEMC_G(compression_type) = NULL;
25742599
MEMC_G(compression_type_real) = COMPRESSION_TYPE_ZLIB;
25752600
MEMC_G(compression_factor) = 1.30;

php_memcached.h

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ enum memcached_serializer {
4343
SERIALIZER_JSON = 3,
4444
SERIALIZER_JSON_ARRAY = 4,
4545
};
46+
#ifdef HAVE_MEMCACHED_IGBINARY
47+
#define SERIALIZER_DEFAULT SERIALIZER_IGBINARY
48+
#define SERIALIZER_DEFAULT_NAME "igbinary"
49+
#else
50+
#define SERIALIZER_DEFAULT SERIALIZER_PHP
51+
#define SERIALIZER_DEFAULT_NAME "php"
52+
#endif //HAVE_MEMCACHED_IGBINARY
4653

4754
ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
4855
#if HAVE_MEMCACHED_SESSION
@@ -53,6 +60,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
5360
char* sess_lock_key;
5461
int sess_lock_key_len;
5562
#endif
63+
char *serializer_name;
5664
enum memcached_serializer serializer;
5765

5866
char *compression_type;

tests/experimental/moduleinfo.phpt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ memcached support => enabled
1919
memcached.compression_factor => %f => %f
2020
memcached.compression_threshold => %d => %d
2121
memcached.compression_type => %s => %s
22+
memcached.serializer => %s => %s
2223
memcached.sess_lock_wait => %d => %d
2324
memcached.sess_locking => %d => %d
2425
memcached.sess_prefix => %s => %s
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
Set default serializer
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
function comp_serializer(Memcached $old_m, $serializer, $m_serializer) {
9+
$old_s = $old_m->getOption(Memcached::OPT_SERIALIZER);
10+
$ok = ini_set('memcached.serializer', $serializer);
11+
if ($ok !== false) {
12+
$ok = ini_get('memcached.serializer') == $serializer;
13+
}
14+
15+
if ($ok) {
16+
$m = new Memcached();
17+
$s = $m->getOption(Memcached::OPT_SERIALIZER);
18+
if ($s != $m_serializer) {
19+
echo "Serializer is $s, should be $m_serializer ($serializer)\n";
20+
}
21+
}
22+
23+
if ($old_m->getOption(Memcached::OPT_SERIALIZER) != $old_s) {
24+
echo "Should not change instantiated objects.\n";
25+
}
26+
}
27+
28+
29+
$m = new Memcached();
30+
$serializer = ini_get('memcached.serializer');
31+
$s = $m->getOption(Memcached::OPT_SERIALIZER);
32+
if ($serializer == "igbinary" && $s == Memcached::SERIALIZER_IGBINARY
33+
|| $serializer == "php" && $s == Memcached::SERIALIZER_PHP) {
34+
echo "OK: $serializer\n";
35+
} else {
36+
echo "Differing serializer: $serializer vs. $s\n";
37+
}
38+
39+
comp_serializer($m, 'json', Memcached::SERIALIZER_JSON);
40+
comp_serializer($m, 'igbinary', Memcached::SERIALIZER_IGBINARY);
41+
comp_serializer($m, 'json_array', Memcached::SERIALIZER_JSON_ARRAY);
42+
comp_serializer($m, 'php', Memcached::SERIALIZER_PHP);
43+
44+
--EXPECTF--
45+
OK: %rigbinary|php%r
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Set invalid serializer
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$default = ini_get('memcached.serializer');
9+
$ok = ini_set('memcached.serializer', 'asdfasdffad');
10+
if ($ok === false || ini_get('memcached.serializer') === $default) {
11+
echo "OK: set failed\n";
12+
}
13+
14+
$ok = ini_set('memcached.serializer', null);
15+
if ($ok === false || ini_get('memcached.serializer') === $default) {
16+
echo "OK: set failed\n";
17+
}
18+
19+
$ok = ini_set('memcached.serializer', '');
20+
if ($ok === false || ini_get('memcached.serializer') === $default) {
21+
echo "OK: set failed\n";
22+
}
23+
24+
$ok = ini_set('memcached.serializer', 'php.');
25+
if ($ok === false || ini_get('memcached.serializer') === $default) {
26+
echo "OK: set failed\n";
27+
}
28+
29+
--EXPECTF--
30+
OK: set failed
31+
OK: set failed
32+
OK: set failed
33+
OK: set failed

0 commit comments

Comments
 (0)