Skip to content

Commit 1162d10

Browse files
committed
Merge pull request #23 from dclaisse/session_consistent_hashing
add consistent hashing option for session handling
2 parents 7e144fa + be3253c commit 1162d10

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
@@ -285,6 +285,7 @@ static PHP_INI_MH(OnUpdateSerializer)
285285
PHP_INI_BEGIN()
286286
#ifdef HAVE_MEMCACHED_SESSION
287287
STD_PHP_INI_ENTRY("memcached.sess_locking", "1", PHP_INI_ALL, OnUpdateBool, sess_locking_enabled, zend_php_memcached_globals, php_memcached_globals)
288+
STD_PHP_INI_ENTRY("memcached.sess_consistent_hash", "0", PHP_INI_ALL, OnUpdateBool, sess_consistent_hash_enabled, zend_php_memcached_globals, php_memcached_globals)
288289
STD_PHP_INI_ENTRY("memcached.sess_binary", "0", PHP_INI_ALL, OnUpdateBool, sess_binary_enabled, zend_php_memcached_globals, php_memcached_globals)
289290
STD_PHP_INI_ENTRY("memcached.sess_lock_wait", "150000", PHP_INI_ALL, OnUpdateLongGEZero,sess_lock_wait, zend_php_memcached_globals, php_memcached_globals)
290291
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
@@ -80,6 +80,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
8080
#if HAVE_MEMCACHED_SASL
8181
bool use_sasl;
8282
#endif
83+
zend_bool sess_consistent_hash_enabled;
8384
zend_bool sess_binary_enabled;
8485
ZEND_END_MODULE_GLOBALS(php_memcached)
8586

php_memcached_session.c

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

0 commit comments

Comments
 (0)