Skip to content

Commit 616b1fc

Browse files
author
Mitch Hagstrand
committed
2 parents 0cc2949 + e781e16 commit 616b1fc

File tree

6 files changed

+141
-8
lines changed

6 files changed

+141
-8
lines changed

README.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ memcached is a high-performance, distributed memory object caching system,
1212
generic in nature, but intended for use in speeding up dynamic web applications
1313
by alleviating database load.
1414

15+
Building
16+
--------
17+
$ phpize
18+
$ ./configure
19+
$ make
20+
$ make test
21+
1522
Resources
1623
---------
1724
* [libmemcached](http://tangent.org/552/libmemcached.html)

memcached-api.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Memcached {
5353

5454
const OPT_RETRY_TIMEOUT;
5555

56+
const OPT_DEAD_TIMEOUT;
57+
5658
const OPT_SND_TIMEOUT;
5759

5860
const OPT_RCV_TIMEOUT;
@@ -257,6 +259,14 @@ public function getServerList( ) {}
257259

258260
public function getServerByKey( $server_key ) {}
259261

262+
public function getLastErrorMessage( ) {}
263+
264+
public function getLastErrorCode( ) {}
265+
266+
public function getLastErrorErrno( ) {}
267+
268+
public function getLastDisconnectedServer( ) {}
269+
260270
public function flush( $delay = 0 ) {}
261271

262272
public function getStats( ) {}
@@ -271,6 +281,8 @@ public function isPersistent( ) {}
271281

272282
public function isPristine( ) {}
273283

284+
public function setSaslAuthData( $username, $password ) {}
285+
274286
}
275287

276288
class MemcachedException extends Exception {

memcached.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ memcached.sess_binary = Off
4444
; memcached session replica read randomize
4545
memcached.sess_randomize_replica_read = Off
4646

47+
; memcached connect timeout value
48+
; In non-blocking mode this changes the value of the timeout
49+
; during socket connection in milliseconds. Specifying -1 means an infinite timeout.
50+
memcached.sess_connect_timeout = 1000
51+
4752
; Set the compression type
4853
; valid values are: fastlz, zlib
4954
; the default is fastlz
@@ -79,3 +84,8 @@ memcached.compression_threshold = 2000
7984
;
8085
; The default is igbinary if available, then msgpack if available, then php otherwise.
8186
memcached.serializer = "igbinary"
87+
88+
; Use SASL authentication for connections
89+
; valid values: On, Off
90+
; the default is Off
91+
memcached.use_sasl = Off

php_memcached.c

Lines changed: 106 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ PHP_INI_BEGIN()
302302
STD_PHP_INI_ENTRY("memcached.sess_number_of_replicas", "0", PHP_INI_ALL, OnUpdateLongGEZero, sess_number_of_replicas, zend_php_memcached_globals, php_memcached_globals)
303303
STD_PHP_INI_ENTRY("memcached.sess_randomize_replica_read", "0", PHP_INI_ALL, OnUpdateBool, sess_randomize_replica_read, zend_php_memcached_globals, php_memcached_globals)
304304
STD_PHP_INI_ENTRY("memcached.sess_remove_failed", "0", PHP_INI_ALL, OnUpdateBool, sess_remove_failed_enabled, zend_php_memcached_globals, php_memcached_globals)
305+
STD_PHP_INI_ENTRY("memcached.sess_connect_timeout", "1000", PHP_INI_ALL, OnUpdateLong, sess_connect_timeout, zend_php_memcached_globals, php_memcached_globals)
305306
#endif
306307
STD_PHP_INI_ENTRY("memcached.compression_type", "fastlz", PHP_INI_ALL, OnUpdateCompressionType, compression_type, zend_php_memcached_globals, php_memcached_globals)
307308
STD_PHP_INI_ENTRY("memcached.compression_factor", "1.3", PHP_INI_ALL, OnUpdateReal, compression_factor, zend_php_memcached_globals, php_memcached_globals)
@@ -1973,7 +1974,7 @@ PHP_METHOD(Memcached, getServerByKey)
19731974
{
19741975
char *server_key;
19751976
int server_key_len;
1976-
memcached_server_st *server;
1977+
memcached_server_instance_st *server_instance;
19771978
memcached_return error;
19781979
MEMC_METHOD_INIT_VARS;
19791980

@@ -1989,16 +1990,16 @@ PHP_METHOD(Memcached, getServerByKey)
19891990
RETURN_FALSE;
19901991
}
19911992

1992-
server = memcached_server_by_key(m_obj->memc, server_key, server_key_len, &error);
1993-
if (server == NULL) {
1993+
server_instance = memcached_server_by_key(m_obj->memc, server_key, server_key_len, &error);
1994+
if (server_instance == NULL) {
19941995
php_memc_handle_error(i_obj, error TSRMLS_CC);
19951996
RETURN_FALSE;
19961997
}
19971998

19981999
array_init(return_value);
1999-
add_assoc_string(return_value, "host", server->hostname, 1);
2000-
add_assoc_long(return_value, "port", server->port);
2001-
add_assoc_long(return_value, "weight", server->weight);
2000+
add_assoc_string(return_value, "host", (char*) memcached_server_name(server_instance), 1);
2001+
add_assoc_long(return_value, "port", memcached_server_port(server_instance));
2002+
add_assoc_long(return_value, "weight", 0);
20022003
}
20032004
/* }}} */
20042005

@@ -2036,6 +2037,81 @@ PHP_METHOD(Memcached, quit)
20362037
}
20372038
/* }}} */
20382039

2040+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
2041+
/* {{{ Memcached::getLastErrorMessage()
2042+
Returns the last error message that occurred */
2043+
PHP_METHOD(Memcached, getLastErrorMessage)
2044+
{
2045+
MEMC_METHOD_INIT_VARS;
2046+
2047+
if (zend_parse_parameters_none() == FAILURE) {
2048+
return;
2049+
}
2050+
2051+
MEMC_METHOD_FETCH_OBJECT;
2052+
2053+
RETURN_STRING(memcached_last_error_message(m_obj->memc), 1);
2054+
}
2055+
/* }}} */
2056+
2057+
/* {{{ Memcached::getLastErrorCode()
2058+
Returns the last error code that occurred */
2059+
PHP_METHOD(Memcached, getLastErrorCode)
2060+
{
2061+
MEMC_METHOD_INIT_VARS;
2062+
2063+
if (zend_parse_parameters_none() == FAILURE) {
2064+
return;
2065+
}
2066+
2067+
MEMC_METHOD_FETCH_OBJECT;
2068+
2069+
RETURN_LONG(memcached_last_error(m_obj->memc));
2070+
}
2071+
/* }}} */
2072+
2073+
/* {{{ Memcached::getLastErrorErrno()
2074+
Returns the last error errno that occurred */
2075+
PHP_METHOD(Memcached, getLastErrorErrno)
2076+
{
2077+
MEMC_METHOD_INIT_VARS;
2078+
2079+
if (zend_parse_parameters_none() == FAILURE) {
2080+
return;
2081+
}
2082+
2083+
MEMC_METHOD_FETCH_OBJECT;
2084+
2085+
RETURN_LONG(memcached_last_error_errno(m_obj->memc));
2086+
}
2087+
/* }}} */
2088+
#endif
2089+
2090+
/* {{{ Memcached::getLastDisconnectedServer()
2091+
Returns the last disconnected server
2092+
Was added in 0.34 according to libmemcached's Changelog */
2093+
PHP_METHOD(Memcached, getLastDisconnectedServer)
2094+
{
2095+
memcached_server_instance_st *server_instance;
2096+
MEMC_METHOD_INIT_VARS;
2097+
2098+
if (zend_parse_parameters_none() == FAILURE) {
2099+
return;
2100+
}
2101+
2102+
MEMC_METHOD_FETCH_OBJECT;
2103+
2104+
server_instance = memcached_server_get_last_disconnect(m_obj->memc);
2105+
if (server_instance == NULL) {
2106+
RETURN_FALSE;
2107+
}
2108+
2109+
array_init(return_value);
2110+
add_assoc_string(return_value, "host", (char*) memcached_server_name(server_instance), 1);
2111+
add_assoc_long(return_value, "port", memcached_server_port(server_instance));
2112+
}
2113+
/* }}} */
2114+
20392115
/* {{{ Memcached::getStats()
20402116
Returns statistics for the memcache servers */
20412117
PHP_METHOD(Memcached, getStats)
@@ -2606,7 +2682,7 @@ static memcached_return php_memc_do_serverlist_callback(const memcached_st *ptr,
26062682

26072683
MAKE_STD_ZVAL(array);
26082684
array_init(array);
2609-
add_assoc_string(array, "host", memcached_server_name(instance), 1);
2685+
add_assoc_string(array, "host", (char*) memcached_server_name(instance), 1);
26102686
add_assoc_long(array, "port", memcached_server_port(instance));
26112687
/*
26122688
* API does not allow to get at this field.
@@ -3092,8 +3168,8 @@ static void php_memc_init_globals(zend_php_memcached_globals *php_memcached_glob
30923168
MEMC_G(sess_locked) = 0;
30933169
MEMC_G(sess_lock_key) = NULL;
30943170
MEMC_G(sess_lock_key_len) = 0;
3095-
MEMC_G(sess_number_of_replicas) = 0;
30963171
MEMC_G(sess_randomize_replica_read) = 0;
3172+
MEMC_G(sess_connect_timeout) = 1000;
30973173
#endif
30983174
MEMC_G(serializer_name) = NULL;
30993175
MEMC_G(serializer) = SERIALIZER_DEFAULT;
@@ -3528,6 +3604,18 @@ ZEND_BEGIN_ARG_INFO(arginfo_getServerByKey, 0)
35283604
ZEND_ARG_INFO(0, server_key)
35293605
ZEND_END_ARG_INFO()
35303606

3607+
ZEND_BEGIN_ARG_INFO(arginfo_getLastErrorMessage, 0)
3608+
ZEND_END_ARG_INFO()
3609+
3610+
ZEND_BEGIN_ARG_INFO(arginfo_getLastErrorCode, 0)
3611+
ZEND_END_ARG_INFO()
3612+
3613+
ZEND_BEGIN_ARG_INFO(arginfo_getLastErrorErrno, 0)
3614+
ZEND_END_ARG_INFO()
3615+
3616+
ZEND_BEGIN_ARG_INFO(arginfo_getLastDisconnectedServer, 0)
3617+
ZEND_END_ARG_INFO()
3618+
35313619
ZEND_BEGIN_ARG_INFO(arginfo_getOption, 0)
35323620
ZEND_ARG_INFO(0, option)
35333621
ZEND_END_ARG_INFO()
@@ -3615,6 +3703,13 @@ static zend_function_entry memcached_class_methods[] = {
36153703
MEMC_ME(resetServerList, arginfo_resetServerList)
36163704
MEMC_ME(quit, arginfo_quit)
36173705

3706+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
3707+
MEMC_ME(getLastErrorMessage, arginfo_getLastErrorMessage)
3708+
MEMC_ME(getLastErrorCode, arginfo_getLastErrorCode)
3709+
MEMC_ME(getLastErrorErrno, arginfo_getLastErrorErrno)
3710+
#endif
3711+
MEMC_ME(getLastDisconnectedServer, arginfo_getLastDisconnectedServer)
3712+
36183713
MEMC_ME(getStats, arginfo_getStats)
36193714
MEMC_ME(getVersion, arginfo_getVersion)
36203715
MEMC_ME(getAllKeys, arginfo_getAllKeys)
@@ -3765,6 +3860,9 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
37653860
REGISTER_MEMC_CLASS_CONST_LONG(OPT_SOCKET_RECV_SIZE, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
37663861
REGISTER_MEMC_CLASS_CONST_LONG(OPT_CONNECT_TIMEOUT, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT);
37673862
REGISTER_MEMC_CLASS_CONST_LONG(OPT_RETRY_TIMEOUT, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT);
3863+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000003
3864+
REGISTER_MEMC_CLASS_CONST_LONG(OPT_DEAD_TIMEOUT, MEMCACHED_BEHAVIOR_DEAD_TIMEOUT);
3865+
#endif
37683866
REGISTER_MEMC_CLASS_CONST_LONG(OPT_SEND_TIMEOUT, MEMCACHED_BEHAVIOR_SND_TIMEOUT);
37693867
REGISTER_MEMC_CLASS_CONST_LONG(OPT_RECV_TIMEOUT, MEMCACHED_BEHAVIOR_RCV_TIMEOUT);
37703868
REGISTER_MEMC_CLASS_CONST_LONG(OPT_POLL_TIMEOUT, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);

php_memcached.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
7474
int sess_number_of_replicas;
7575
zend_bool sess_randomize_replica_read;
7676
zend_bool sess_remove_failed_enabled;
77+
long sess_connect_timeout;
7778
zend_bool sess_consistent_hash_enabled;
7879
zend_bool sess_binary_enabled;
7980
#endif

php_memcached_session.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ PS_OPEN_FUNC(memcached)
228228
}
229229
}
230230

231+
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, (uint64_t) MEMC_G(sess_connect_timeout)) == MEMCACHED_FAILURE) {
232+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set memcached connection timeout");
233+
return FAILURE;
234+
}
235+
231236
/* Allow libmemcached remove failed servers */
232237
if (MEMC_G(sess_remove_failed_enabled)) {
233238
if (memcached_behavior_set(memc_sess->memc_sess, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, (uint64_t) 1) == MEMCACHED_FAILURE) {

0 commit comments

Comments
 (0)