8000 Added OBJECT command. · jrtkcoder/phpredis@dd40393 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd40393

Browse files
committed
Added OBJECT command.
1 parent 6f6c4ff commit dd40393

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

README.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,26 @@ $redis->slaveof('10.0.1.7', 6379);
13291329
$redis->slaveof();
13301330
</pre>
13311331

1332+
## object
1333+
##### *Description*
1334+
Describes the object pointed to by a key.
1335+
1336+
##### *Parameters*
1337+
The information to retrieve (string) and the key (string). Info can be one of the following:
1338+
* "encoding"
1339+
* "refcount"
1340+
* "idletime"
1341+
1342+
##### *Return value*
1343+
*STRING* for "encoding", *LONG* for "refcount" and "idletime", `FALSE` if the key doesn't exist.
1344+
1345+
##### *Example*
1346+
<pre>
1347+
$redis->object("encoding", "l"); // → ziplist
1348+
$redis->object("refcount", "l"); // → 1
1349+
$redis->object("idletime", "l"); // → 400 (in seconds, with a precision of 10 seconds).
1350+
</pre>
1351+
13321352
## save
13331353
##### *Description*
13341354
Performs a synchronous save.

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ PHP_METHOD(Redis, zUnion);
117117
PHP_METHOD(Redis, expireAt);
118118
PHP_METHOD(Redis, bgrewriteaof);
119119
PHP_METHOD(Redis, slaveof);
120+
PHP_METHOD(Redis, object);
120121

121122
PHP_METHOD(Redis, mset);
122123
PHP_METHOD(Redis, msetnx);

redis.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ static zend_function_entry redis_functions[] = {
124124
PHP_ME(Redis, move, NULL, ZEND_ACC_PUBLIC)
125125
PHP_ME(Redis, bgrewriteaof, NULL, ZEND_ACC_PUBLIC)
126126
PHP_ME(Redis, slaveof, NULL, ZEND_ACC_PUBLIC)
127+
PHP_ME(Redis, object, NULL, ZEND_ACC_PUBLIC)
127128

128129
/* 1.1 */
129130
PHP_ME(Redis, mset, NULL, ZEND_ACC_PUBLIC)
@@ -4939,6 +4940,46 @@ PHP_METHOD(Redis, slaveof)
49394940
}
49404941
/* }}} */
49414942

4943+
/* {{{ proto string Redis::object(key)
4944+
*/
4945+
PHP_METHOD(Redis, object)
4946+
{
4947+
zval *object;
4948+
RedisSock *redis_sock;
4949+
char *cmd = "", *info = NULL, *key = NULL;
4950+
int cmd_len, info_len, key_len;
4951+
long port = 6379;
4952+
4953+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
4954+
&object, redis_ce, &info, &info_len, &key, &key_len) == FAILURE) {
4955+
RETURN_FALSE;
4956+
}
4957+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
4958+
RETURN_FALSE;
4959+
}
4960+
4961+
cmd_len = redis_cmd_format_static(&cmd, "OBJECT", "ss", info, info_len, key, key_len);
4962+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
4963+
4964+
if(info_len == 8 && (strncasecmp(info, "refcount", 8) == 0 || strncasecmp(info, "idletime", 8) == 0)) {
4965+
IF_ATOMIC() {
4966+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
4967+
}
4968+
REDIS_PROCESS_RESPONSE(redis_long_response);
4969+
} else if(info_len == 8 && strncasecmp(info, "encoding", 8) == 0) {
4970+
IF_ATOMIC() {
4971+
redis_string_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
4972+
}
4973+
REDIS_PROCESS_RESPONSE(redis_string_response);
4974+
} else { /* fail */
4975+
IF_ATOMIC() {
4976+
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
4977+
}
4978+
REDIS_PROCESS_RESPONSE(redis_boolean_response);
4979+
}
4980+
}
4981+
/* }}} */
4982+
49424983
/* {{{ proto string Redis::getOption($option)
49434984
*/
49444985
PHP_METHOD(Redis, getOption) {

tests/TestRedis.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,43 @@ public function testSetRange() {
18951895
$this->assertTrue("\x00\x00\x00\x00\x00\x00foo" === $this->redis->get('key'));
18961896
}
18971897

1898+
public function testObject() {
1899+
$this->redis->del('key');
1900+
$this->assertTrue($this->redis->object('encoding', 'key') === FALSE);
1901+
$this->assertTrue($this->redis->object('refcount', 'key') === FALSE);
1902+
$this->assertTrue($this->redis->object('idletime', 'key') === FALSE);
1903+
1904+
$this->redis->set('key', 'value');
1905+
$this->assertTrue($this->redis->object('encoding', 'key') === "raw");
1906+
$this->assertTrue($this->redis->object('refcount', 'key') === 1);
1907+
$this->assertTrue($this->redis->object('idletime', 'key') === 0);
1908+
1909+
$this->redis->del('key');
1910+
$this->redis->lpush('key', 'value');
1911+
$this->assertTrue($this->redis->object('encoding', 'key') === "ziplist");
1912+
$this->assertTrue($this->redis->object('refcount', 'key') === 1);
1913+
$this->assertTrue($this->redis->object('idletime', 'key') === 0);
1914+
1915+
$this->redis->del('key');
1916+
$this->redis->sadd('key', 'value');
1917+
$this->assertTrue($this->redis->object('encoding', 'key') === "hashtable");
1918+
$this->assertTrue($this->redis->object('refcount', 'key') === 1);
1919+
$this->assertTrue($this->redis->object('idletime', 'key') === 0);
1920+
1921+
$this->redis->del('key');
1922+
$this->redis->sadd('key', 42);
1923+
$this->redis->sadd('key', 1729);
1924+
$this->assertTrue($this->redis->object('encoding', 'key') === "intset");
1925+
$this->assertTrue($this->redis->object('refcount', 'key') === 1);
1926+
$this->assertTrue($this->redis->object('idletime', 'key') === 0);
1927+
1928+
$this->redis->del('key');
1929+
$this->redis->lpush('key', str_repeat('A', pow(10,6))); // 1M elements, too big for a ziplist.
1930+
$this->assertTrue($this->redis->object('encoding', 'key') === "linkedlist");
1931+
$this->assertTrue($this->redis->object('refcount', 'key') === 1);
1932+
$this->assertTrue($this->redis->object('idletime', 'key') === 0);
1933+
}
1934+
18981935
public function testMultiExec() {
18991936
$this->sequence(Redis::MULTI);
19001937

0 commit comments

Comments
 (0)
0