Skip to content

Commit be3253c

Browse files
author
Damien Claisse
committed
add consistent hashing option for session handling
1 parent 9f86643 commit be3253c

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

memcached.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ memcached.sess_lock_wait = 150000
1717
; the default value is "memc.sess.key."
1818
memcached.sess_prefix = "memc.sess.key."
1919

20+
; memcached session consistent hash mode
21+
; if set to On, consistent hashing (libketama) is used
22+
; for session handling.
23+
; When consistent hashing is used, one can add or remove cache
24+
; node(s) without messing up too much with existing keys
25+
; default is Off
26+
memcached.sess_consistent_hash = Off
27+
2028
; memcached session binary mode
2129
memcached.sess_binary = Off
2230

php_memcached.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ static PHP_INI_MH(OnUpdateSerializer)
287287
PHP_INI_BEGIN()
288288
#ifdef HAVE_MEMCACHED_SESSION
289289
STD_PHP_INI_ENTRY("memcached.sess_locking", "1", PHP_INI_ALL, OnUpdateBool, sess_locking_enabled, zend_php_memcached_globals, php_memcached_globals)
290+
STD_PHP_INI_ENTRY("memcached.sess_consistent_hash", "0", PHP_INI_ALL, OnUpdateBool, sess_consistent_hash_enabled, zend_php_memcached_globals, php_memcached_globals)
290291
STD_PHP_INI_ENTRY("memcached.sess_binary", "0", PHP_INI_ALL, OnUpdateBool, sess_binary_enabled, zend_php_memcached_globals, php_memcached_globals)
291292
STD_PHP_INI_ENTRY("memcached.sess_lock_wait", "150000", PHP_INI_ALL, OnUpdateLongGEZero,sess_lock_wait, zend_php_memcached_globals, php_memcached_globals)
292293
STD_PHP_INI_ENTRY("memcached.sess_prefix", "memc.sess.key.", PHP_INI_ALL, OnUpdateString, sess_prefix, zend_php_memcached_globals, php_memcached_globals)

php_memcached.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
7878
#if HAVE_MEMCACHED_SASL
7979
bool use_sasl;
8080
#endif
81+
zend_bool sess_consistent_hash_enabled;
8182
zend_bool sess_binary_enabled;
8283
ZEND_END_MODULE_GLOBALS(php_memcached)
8384

php_memcached_session.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ PS_OPEN_FUNC(memcached)
140140
if (servers) {
141141
memc_sess->memc_sess = memcached_create(NULL);
142142
if (memc_sess->memc_sess) {
143+
if (MEMC_G(sess_consistent_hash_enabled)) {
144+
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, (uint64_t) 1) == MEMCACHED_FAILURE) {
145+
PS_SET_MOD_DATA(NULL);
146+
if (plist_key) {
147+
efree(plist_key);
148+
}
149+
memcached_free(memc_sess->memc_sess);
150+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to enable memcached consistent hashing");
151+
return FAILURE;
152+
}
153+
}
154+
143155
status = memcached_server_push(memc_sess->memc_sess, servers);
144156
memcached_server_list_free(servers);
145157

0 commit comments

Comments
 (0)