Skip to content

add consistent hashing option for session handling #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions memcached.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ memcached.sess_lock_wait = 150000
; the default value is "memc.sess.key."
memcached.sess_prefix = "memc.sess.key."

; memcached session consistent hash mode
; if set to On, consistent hashing (libketama) is used
; for session handling.
; When consistent hashing is used, one can add or remove cache
; node(s) without messing up too much with existing keys
; default is Off
memcached.sess_consistent_hash = Off

; memcached session binary mode
memcached.sess_binary = Off

Expand Down
1 change: 1 addition & 0 deletions php_memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ static PHP_INI_MH(OnUpdateSerializer)
PHP_INI_BEGIN()
#ifdef HAVE_MEMCACHED_SESSION
STD_PHP_INI_ENTRY("memcached.sess_locking", "1", PHP_INI_ALL, OnUpdateBool, sess_locking_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_consistent_hash", "0", PHP_INI_ALL, OnUpdateBool, sess_consistent_hash_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_binary", "0", PHP_INI_ALL, OnUpdateBool, sess_binary_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_lock_wait", "150000", PHP_INI_ALL, OnUpdateLongGEZero,sess_lock_wait, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_prefix", "memc.sess.key.", PHP_INI_ALL, OnUpdateString, sess_prefix, zend_php_memcached_globals, php_memcached_globals)
Expand Down
1 change: 1 addition & 0 deletions php_memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
#if HAVE_MEMCACHED_SASL
bool use_sasl;
#endif
zend_bool sess_consistent_hash_enabled;
zend_bool sess_binary_enabled;
ZEND_END_MODULE_GLOBALS(php_memcached)

Expand Down
12 changes: 12 additions & 0 deletions php_memcached_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ PS_OPEN_FUNC(memcached)
if (servers) {
memc_sess->memc_sess = memcached_create(NULL);
if (memc_sess->memc_sess) {
if (MEMC_G(sess_consistent_hash_enabled)) {
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, (uint64_t) 1) == MEMCACHED_FAILURE) {
PS_SET_MOD_DATA(NULL);
if (plist_key) {
efree(plist_key);
}
memcached_free(memc_sess->memc_sess);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to enable memcached consistent hashing");
return FAILURE;
}
}

status = memcached_server_push(memc_sess->memc_sess, servers);
memcached_server_list_free(servers);

Expand Down