Skip to content

Commit 9258efd

Browse files
committed
Revert "Cleanup all checks for unsupported libmemcached versions (#295)"
This reverts commit c564fd8.
1 parent e4f4878 commit 9258efd

7 files changed

+153
-17
lines changed

config.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ if test "$PHP_MEMCACHED" != "no"; then
318318
AC_DEFINE(HAVE_MEMCACHED_EXIST, [1], [Whether memcached_exist is defined])
319319
fi
320320

321-
PHP_MEMCACHED_FILES="php_memcached.c g_fmt.c"
321+
PHP_MEMCACHED_FILES="php_memcached.c php_libmemcached_compat.c g_fmt.c"
322322

323323
if test "$PHP_SYSTEM_FASTLZ" != "no"; then
324324
AC_CHECK_HEADERS([fastlz.h], [ac_cv_have_fastlz="yes"], [ac_cv_have_fastlz="no"])

package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Tests
7373
<file role='src' name='php_memcached_private.h'/>
7474
<file role='src' name='php_memcached_session.c'/>
7575
<file role='src' name='php_memcached_session.h'/>
76+
<file role='src' name='php_libmemcached_compat.h'/>
77+
<file role='src' name='php_libmemcached_compat.c'/>
7678
<file role='src' name='php_memcached_server.h'/>
7779
<file role='src' name='php_memcached_server.c'/>
7880
<file role='src' name='g_fmt.c'/>

php_libmemcached_compat.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 2009 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.0 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.php.net/license/3_0.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Andrei Zmievski <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "php_memcached.h"
18+
#include "php_memcached_private.h"
19+
#include "php_libmemcached_compat.h"
20+
21+
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key)
22+
{
23+
#ifdef HAVE_MEMCACHED_EXIST
24+
return memcached_exist (memc, key->val, key->len);
25+
#else
26+
memcached_return rc = MEMCACHED_SUCCESS;
27+
uint32_t flags = 0;
28+
size_t value_length = 0;
29+
char *value = NULL;
30+
31+
value = memcached_get (memc, key->val, key->len, &value_length, &flags, &rc);
32+
if (value) {
33+
free (value);
34+
}
35+
return rc;
36+
#endif
37+
}

php_libmemcached_compat.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 2009 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.0 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.php.net/license/3_0.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Andrei Zmievski <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef PHP_LIBMEMCACHED_COMPAT
18+
#define PHP_LIBMEMCACHED_COMPAT
19+
20+
/* this is the version(s) we support */
21+
#include <libmemcached/memcached.h>
22+
23+
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key);
24+
25+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x00052000
26+
# define MEMCACHED_SERVER_TEMPORARILY_DISABLED (1024 << 2)
27+
#endif
28+
29+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000002
30+
# define HAVE_MEMCACHED_TOUCH 1
31+
#endif
32+
33+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000017
34+
# define HAVE_MEMCACHED_INSTANCE_ST 1
35+
#endif
36+
37+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
38+
# define HAVE_LIBMEMCACHED_CHECK_CONFIGURATION 1
39+
#endif
40+
41+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000002
42+
# define HAVE_MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS 1
43+
#endif
44+
45+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
46+
# define HAVE_LIBMEMCACHED_MEMCACHED 1
47+
#endif
48+
49+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000018
50+
# define HAVE_MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT 1
51+
#endif
52+
53+
#ifdef HAVE_MEMCACHED_INSTANCE_ST
54+
typedef const memcached_instance_st * php_memcached_instance_st;
55+
#else
56+
typedef memcached_server_instance_st php_memcached_instance_st;
57+
#endif
58+
59+
#endif

php_memcached.c

+50-8
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ PHP_METHOD(Memcached, setByKey)
17651765
}
17661766
/* }}} */
17671767

1768+
#ifdef HAVE_MEMCACHED_TOUCH
17681769
/* {{{ Memcached::touch(string key, [, int expiration ])
17691770
Sets a new expiration for the given key */
17701771
PHP_METHOD(Memcached, touch)
@@ -1780,6 +1781,8 @@ PHP_METHOD(Memcached, touchByKey)
17801781
php_memc_store_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, MEMC_OP_TOUCH, 1);
17811782
}
17821783
/* }}} */
1784+
#endif
1785+
17831786

17841787
/* {{{ Memcached::setMulti(array items [, int expiration ])
17851788
Sets the keys/values specified in the items array */
@@ -2319,7 +2322,17 @@ PHP_METHOD(Memcached, addServer)
23192322
MEMC_METHOD_FETCH_OBJECT;
23202323
s_memc_set_status(intern, MEMCACHED_SUCCESS, 0);
23212324

2325+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000002
2326+
if (ZSTR_VAL(host)[0] == '/') { /* unix domain socket */
2327+
status = memcached_server_add_unix_socket_with_weight(intern->memc, ZSTR_VAL(host), weight);
2328+
} else if (memcached_behavior_get(intern->memc, MEMCACHED_BEHAVIOR_USE_UDP)) {
2329+
status = memcached_server_add_udp_with_weight(intern->memc, ZSTR_VAL(host), port, weight);
2330+
} else {
2331+
status = memcached_server_add_with_weight(intern->memc, ZSTR_VAL(host), port, weight);
2332+
}
2333+
#else
23222334
status = memcached_server_add_with_weight(intern->memc, ZSTR_VAL(host), port, weight);
2335+
#endif
23232336

23242337
if (s_memc_status_handle_result_code(intern, status) == FAILURE) {
23252338
RETURN_FALSE;
@@ -2515,6 +2528,7 @@ PHP_METHOD(Memcached, flushBuffers)
25152528
}
25162529
/* }}} */
25172530

2531+
#ifdef HAVE_LIBMEMCACHED_CHECK_CONFIGURATION
25182532
/* {{{ Memcached::getLastErrorMessage()
25192533
Returns the last error message that occurred */
25202534
PHP_METHOD(Memcached, getLastErrorMessage)
@@ -2562,6 +2576,7 @@ PHP_METHOD(Memcached, getLastErrorErrno)
25622576
RETURN_LONG(memcached_last_error_errno(intern->memc));
25632577
}
25642578
/* }}} */
2579+
#endif
25652580

25662581
/* {{{ Memcached::getLastDisconnectedServer()
25672582
Returns the last disconnected server
@@ -2801,7 +2816,11 @@ static PHP_METHOD(Memcached, getOption)
28012816

28022817
result = memcached_callback_get(intern->memc, MEMCACHED_CALLBACK_PREFIX_KEY, &retval);
28032818
if (retval == MEMCACHED_SUCCESS && result) {
2819+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX == 0x00049000
2820+
RETURN_STRINGL(result, strlen(result));
2821+
#else
28042822
RETURN_STRING(result);
2823+
#endif
28052824
} else {
28062825
RETURN_EMPTY_STRING();
28072826
}
@@ -2866,11 +2885,23 @@ int php_memc_set_option(php_memc_object_t *intern, long option, zval *value)
28662885
{
28672886
zend_string *str;
28682887
char *key;
2888+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX == 0x00049000
2889+
char tmp[MEMCACHED_PREFIX_KEY_MAX_SIZE - 1];
2890+
#endif
28692891
str = zval_get_string(value);
28702892
if (ZSTR_LEN(str) == 0) {
28712893
key = NULL;
28722894
} else {
2895+
/*
2896+
work-around a bug in libmemcached in version 0.49 that truncates the trailing
2897+
character of the key prefix, to avoid the issue we pad it with a '0'
2898+
*/
2899+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX == 0x00049000
2900+
snprintf(tmp, sizeof(tmp), "%s0", ZSTR_VAL(str));
2901+
key = tmp;
2902+
#else
28732903
key = ZSTR_VAL(str);
2904+
#endif
28742905
}
28752906
if (memcached_callback_set(intern->memc, MEMCACHED_CALLBACK_PREFIX_KEY, key) == MEMCACHED_BAD_KEY_PROVIDED) {
28762907
zend_string_release(str);
@@ -2899,9 +2930,14 @@ int php_memc_set_option(php_memc_object_t *intern, long option, zval *value)
28992930
* (non-weighted) case. We have to clean up ourselves.
29002931
*/
29012932
if (!lval) {
2902-
(void)memcached_behavior_set_key_hash(intern->memc, MEMCACHED_HASH_DEFAULT);
2903-
(void)memcached_behavior_set_distribution_hash(intern->memc, MEMCACHED_HASH_DEFAULT);
2904-
(void)memcached_behavior_set_distribution(intern->memc, MEMCACHED_DISTRIBUTION_MODULA);
2933+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX > 0x00037000
2934+
(void)memcached_behavior_set_key_hash(intern->memc, MEMCACHED_HASH_DEFAULT);
2935+
(void)memcached_behavior_set_distribution_hash(intern->memc, MEMCACHED_HASH_DEFAULT);
2936+
(void)memcached_behavior_set_distribution(intern->memc, MEMCACHED_DISTRIBUTION_MODULA);
2937+
#else
2938+
intern->memc->hash = 0;
2939+
intern->memc->distribution = 0;
2940+
#endif
29052941
}
29062942
break;
29072943

@@ -3984,10 +4020,10 @@ static zend_function_entry memcached_class_methods[] = {
39844020

39854021
MEMC_ME(set, arginfo_set)
39864022
MEMC_ME(setByKey, arginfo_setByKey)
3987-
4023+
#ifdef HAVE_MEMCACHED_TOUCH
39884024
MEMC_ME(touch, arginfo_touch)
39894025
MEMC_ME(touchByKey, arginfo_touchByKey)
3990-
4026+
#endif
39914027
MEMC_ME(setMulti, arginfo_setMulti)
39924028
MEMC_ME(setMultiByKey, arginfo_setMultiByKey)
39934029

@@ -4019,10 +4055,11 @@ static zend_function_entry memcached_class_methods[] = {
40194055
MEMC_ME(quit, arginfo_quit)
40204056
MEMC_ME(flushBuffers, arginfo_flushBuffers)
40214057

4058+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
40224059
MEMC_ME(getLastErrorMessage, arginfo_getLastErrorMessage)
40234060
MEMC_ME(getLastErrorCode, arginfo_getLastErrorCode)
40244061
MEMC_ME(getLastErrorErrno, arginfo_getLastErrorErrno)
4025-
4062+
#endif
40264063
MEMC_ME(getLastDisconnectedServer, arginfo_getLastDisconnectedServer)
40274064

40284065
MEMC_ME(getStats, arginfo_getStats)
@@ -4215,8 +4252,9 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
42154252
REGISTER_MEMC_CLASS_CONST_LONG(OPT_DISTRIBUTION, MEMCACHED_BEHAVIOR_DISTRIBUTION);
42164253
REGISTER_MEMC_CLASS_CONST_LONG(DISTRIBUTION_MODULA, MEMCACHED_DISTRIBUTION_MODULA);
42174254
REGISTER_MEMC_CLASS_CONST_LONG(DISTRIBUTION_CONSISTENT, MEMCACHED_DISTRIBUTION_CONSISTENT);
4255+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00049000
42184256
REGISTER_MEMC_CLASS_CONST_LONG(DISTRIBUTION_VIRTUAL_BUCKET, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET);
4219-
4257+
#endif
42204258
REGISTER_MEMC_CLASS_CONST_LONG(OPT_LIBKETAMA_COMPATIBLE, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
42214259
REGISTER_MEMC_CLASS_CONST_LONG(OPT_LIBKETAMA_HASH, MEMCACHED_BEHAVIOR_KETAMA_HASH);
42224260
REGISTER_MEMC_CLASS_CONST_LONG(OPT_TCP_KEEPALIVE, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE);
@@ -4242,10 +4280,14 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
42424280
REGISTER_MEMC_CLASS_CONST_LONG(OPT_SORT_HOSTS, MEMCACHED_BEHAVIOR_SORT_HOSTS);
42434281
REGISTER_MEMC_CLASS_CONST_LONG(OPT_VERIFY_KEY, MEMCACHED_BEHAVIOR_VERIFY_KEY);
42444282
REGISTER_MEMC_CLASS_CONST_LONG(OPT_USE_UDP, MEMCACHED_BEHAVIOR_USE_UDP);
4283+
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x00037000
42454284
REGISTER_MEMC_CLASS_CONST_LONG(OPT_NUMBER_OF_REPLICAS, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS);
42464285
REGISTER_MEMC_CLASS_CONST_LONG(OPT_RANDOMIZE_REPLICA_READ, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ);
4286+
#endif
4287+
#ifdef HAVE_MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS
42474288
REGISTER_MEMC_CLASS_CONST_LONG(OPT_REMOVE_FAILED_SERVERS, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS);
4248-
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000018
4289+
#endif
4290+
#ifdef HAVE_MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT
42494291
REGISTER_MEMC_CLASS_CONST_LONG(OPT_SERVER_TIMEOUT_LIMIT, MEMCACHED_BEHAVIOR_SERVER_TIMEOUT_LIMIT);
42504292
#endif
42514293

php_memcached_private.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@
2525
# include "config.h"
2626
#endif
2727

28-
#include <libmemcached/memcached.h>
29-
30-
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX >= 0x01000017
31-
typedef const memcached_instance_st * php_memcached_instance_st;
32-
#else
33-
typedef memcached_server_instance_st php_memcached_instance_st;
34-
#endif
28+
#include "php_libmemcached_compat.h"
3529

3630
#include <stdlib.h>
3731
#include <string.h>
@@ -214,6 +208,8 @@ PHP_MINFO_FUNCTION(memcached);
214208

215209
char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache);
216210

211+
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key);
212+
217213
zend_bool php_memc_init_sasl_if_needed();
218214

219215
#endif /* PHP_MEMCACHED_PRIVATE_H */

php_memcached_session.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ PS_VALIDATE_SID_FUNC(memcached)
530530
{
531531
memcached_st *memc = PS_GET_MOD_DATA();
532532

533-
if (memcached_exist(memc, key->val, key->len) == MEMCACHED_SUCCESS) {
533+
if (php_memcached_exist(memc, key) == MEMCACHED_SUCCESS) {
534534
return SUCCESS;
535535
} else {
536536
return FAILURE;

0 commit comments

Comments
 (0)