Skip to content

Commit c4a5370

Browse files
committed
Add ECHO.
Covered by unit test; fixes GitHub issue phpredis#165.
1 parent 882c7dc commit c4a5370

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ PHP_METHOD(Redis, connect);
2929
PHP_METHOD(Redis, pconnect);
3030
PHP_METHOD(Redis, close);
3131
PHP_METHOD(Redis, ping);
32+
PHP_METHOD(Redis, echo);
3233
PHP_METHOD(Redis, get);
3334
PHP_METHOD(Redis, set);
3435
PHP_METHOD(Redis, setex);

redis.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static zend_function_entry redis_functions[] = {
7676
PHP_ME(Redis, pconnect, NULL, ZEND_ACC_PUBLIC)
7777
PHP_ME(Redis, close, NULL, ZEND_ACC_PUBLIC)
7878
PHP_ME(Redis, ping, NULL, ZEND_ACC_PUBLIC)
79+
PHP_ME(Redis, echo, NULL, ZEND_ACC_PUBLIC)
7980
PHP_ME(Redis, get, NULL, ZEND_ACC_PUBLIC)
8081
PHP_ME(Redis, set, NULL, ZEND_ACC_PUBLIC)
8182
PHP_ME(Redis, setex, NULL, ZEND_ACC_PUBLIC)
@@ -778,6 +779,39 @@ PHP_METHOD(Redis, randomKey)
778779
}
779780
/* }}} */
780781

782+
/* {{{ proto string Redis::echo(string key)
783+
*/
784+
PHP_METHOD(Redis, echo)
785+
{
786+
zval *object;
787+
RedisSock *redis_sock;
788+
char *key = NULL, *cmd;
789+
int key_len, cmd_len;
790+
int key_free;
791+
792+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
793+
&object, redis_ce,
794+
&key, &key_len) == FAILURE) {
795+
RETURN_FALSE;
796+
}
797+
798+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
799+
RETURN_FALSE;
800+
}
801+
802+
key_free = redis_key_prefix(redis_sock, &key, &key_len TSRMLS_CC);
803+
cmd_len = redis_cmd_format_static(&cmd, "ECHO", "s", key, key_len);
804+
if(key_free) efree(key);
805+
806+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
807+
IF_ATOMIC() {
808+
redis_string_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
809+
}
810+
REDIS_PROCESS_RESPONSE(redis_string_response);
811+
812+
}
813+
/* }}} */
814+
781815
/* {{{ proto string Redis::renameKey(string key_src, string key_dst)
782816
*/
783817
PHP_METHOD(Redis, renameKey)

tests/TestRedis.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function test1000() {
115115
$this->assertEquals($s, $this->redis->get('x'));
116116
}
117117

118+
public function testEcho() {
119+
$this->assertEquals($this->redis->echo("hello"), "hello");
120+
$this->assertEquals($this->redis->echo(""), "");
121+
$this->assertEquals($this->redis->echo(" 0123 "), " 0123 ");
122+
}
123+
118124
public function testErr() {
119125

120126
$this->redis->set('x', '-ERR');

0 commit comments

Comments
 (0)