Skip to content

Commit 5509b54

Browse files
committed
Small changes to CONFIG contribution.
Code cleanup, removed serialize and key prefix, added extra checks. Now returning an associative array on GET, bool on SET.
1 parent 8f34100 commit 5509b54

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

redis.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5271,47 +5271,55 @@ PHP_METHOD(Redis, setOption) {
52715271
}
52725272
/* }}} */
52735273

5274-
/* {{{ proto boolean Redis::config(string operation, string key [, mixed value])
5274+
/* {{{ proto boolean Redis::config(string op, string key [, mixed value])
52755275
*/
52765276
PHP_METHOD(Redis, config)
52775277
{
52785278
zval *object;
52795279
RedisSock *redis_sock;
5280-
char *key = NULL, *val = NULL, *cmd, *operation = NULL;
5281-
int key_len, val_len, cmd_len, operation_len;
5282-
int val_free = 0, key_free = 0;
5283-
zval *z_value = NULL;
5284-
long expire = -1;
5280+
char *key = NULL, *val = NULL, *cmd, *op = NULL;
5281+
int key_len, val_len, cmd_len, op_len;
5282+
enum {CFG_GET, CFG_SET} mode;
52855283

5286-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss|z",
5287-
&object, redis_ce, &operation, &operation_len, &key, &key_len,
5288-
&z_value) == FAILURE) {
5284+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss|s",
5285+
&object, redis_ce, &op, &op_len, &key, &key_len,
5286+
&val, &val_len) == FAILURE) {
52895287
RETURN_FALSE;
52905288
}
52915289

5290+
/* op must be GET or SET */
5291+
if(strncasecmp(op, "GET", 3) == 0) {
5292+
mode = CFG_GET;
5293+
} else if(strncasecmp(op, "SET", 3) == 0) {
5294+
mode = CFG_SET;
5295+
} else {
5296+
RETURN_FALSE;
5297+
}
5298+
52925299
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
52935300
RETURN_FALSE;
52945301
}
52955302

5296-
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
5297-
5298-
if (z_value == NULL) {
5299-
cmd_len = redis_cmd_format_static(&cmd, "CONFIG", "ss", operation, operation_len, key, key_len);
5300-
}
5301-
else {
5302-
val_free = redis_serialize(redis_sock, z_value, &val, &val_len TSRMLS_CC);
5303-
cmd_len = redis_cmd_format_static(&cmd, "CONFIG", "sss", operation, operation_len, key, key_len, val, val_len);
5304-
if(val_free) efree(val);
5305-
}
5303+
if (mode == CFG_GET && val == NULL) {
5304+
cmd_len = redis_cmd_format_static(&cmd, "CONFIG", "ss", op, op_len, key, key_len);
53065305

5307-
if(key_free) efree(key);
5306+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
5307+
IF_ATOMIC() {
5308+
redis_sock_read_multibulk_reply_zipped_strings(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
5309+
}
5310+
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply_zipped_strings);
53085311

5309-
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
5312+
} else if(mode == CFG_SET && val != NULL) {
5313+
cmd_len = redis_cmd_format_static(&cmd, "CONFIG", "sss", op, op_len, key, key_len, val, val_len);
53105314

5311-
IF_ATOMIC() {
5312-
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
5313-
}
5314-
REDIS_PROCESS_RESPONSE(redis_boolean_response);
5315+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len)
5316+
IF_ATOMIC() {
5317+
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
5318+
}
5319+
REDIS_PROCESS_RESPONSE(redis_boolean_response);
5320+
} else {
5321+
RETURN_FALSE;
5322+
}
53155323
}
53165324
/* }}} */
53175325

0 commit comments

Comments
 (0)