Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c8e45f

Browse files
committed
SETEX patch thanks to Zakay Danial.
1 parent 43b43fc commit 7c8e45f

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.markdown

+18
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ Set the string value in argument as value of the key.
8989
$redis->set('key', 'value')
9090
</pre>
9191

92+
## setex
93+
##### Description
94+
95+
Set the string value in argument as value of the key, with a time to live.
96+
97+
##### Parameters
98+
*Key*
99+
*TTL*
100+
*Value*
101+
102+
##### Return value
103+
*Bool* `TRUE` if the command is successful.
104+
105+
##### Examples
106+
107+
<pre>
108+
$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.
109+
</pre>
92110
## setnx
93111
##### Description
94112
Set the string value in argument as value of the key if the key doesn't already exist in the database.

php_redis.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PHP_METHOD(Redis, close);
2727
PHP_METHOD(Redis, ping);
2828
PHP_METHOD(Redis, get);
2929
PHP_METHOD(Redis, set);
30+
PHP_METHOD(Redis, setex);
3031
PHP_METHOD(Redis, setnx);
3132
PHP_METHOD(Redis, getSet);
3233
PHP_METHOD(Redis, randomKey);

redis.c

+44
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static zend_function_entry redis_functions[] = {
5151
PHP_ME(Redis, ping, NULL, ZEND_ACC_PUBLIC)
5252
PHP_ME(Redis, get, NULL, ZEND_ACC_PUBLIC)
5353
PHP_ME(Redis, set, NULL, ZEND_ACC_PUBLIC)
54+
PHP_ME(Redis, setex, NULL, ZEND_ACC_PUBLIC)
5455
PHP_ME(Redis, setnx, NULL, ZEND_ACC_PUBLIC)
5556
PHP_ME(Redis, getSet, NULL, ZEND_ACC_PUBLIC)
5657
PHP_ME(Redis, randomKey, NULL, ZEND_ACC_PUBLIC)
@@ -461,8 +462,51 @@ PHP_METHOD(Redis, set)
461462
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
462463
}
463464
REDIS_PROCESS_RESPONSE(redis_boolean_response);
465+
}
466+
467+
/* {{{ proto boolean Redis::setex(string key, long expire, string value)
468+
*/
469+
PHP_METHOD(Redis, setex)
470+
{
471+
zval *object;
472+
RedisSock *redis_sock;
473+
char *key = NULL, *val = NULL, *cmd;
474+
int key_len, val_len, cmd_len;
475+
long expire;
476+
477+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osls",
478+
&object, redis_ce, &key, &key_len,
479+
&expire, &val, &val_len) == FAILURE) {
480+
RETURN_FALSE;
481+
}
464482

483+
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
484+
RETURN_FALSE;
485+
}
465486

487+
cmd_len = redis_cmd_format(&cmd,
488+
"*4" _NL
489+
"$5" _NL
490+
"SETEX" _NL
491+
492+
"$%d" _NL /* key_len */
493+
"%s" _NL /* key */
494+
495+
"$%d" _NL /* expire_len */
496+
"%d" _NL /* expire */
497+
498+
"$%d" _NL /* val_len */
499+
"%s" _NL /* val */
500+
501+
, key_len, key, key_len
502+
, integer_length(expire), expire
503+
, val_len, val, val_len);
504+
505+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
506+
IF_ATOMIC() {
507+
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
508+
}
509+
REDIS_PROCESS_RESPONSE(redis_boolean_response);
466510
}
467511

468512
/* {{{ proto boolean Redis::setnx(string key, string value)

tests/TestRedis.php

+7
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ public function testExpireAt() {
246246
$this->assertEquals(FALSE, $this->redis->get('key'));
247247
}
248248

249+
public function testSetEx() {
250+
251+
$this->redis->delete('key');
252+
$this->assertTrue($this->redis->setex('key', 7, 'val') === TRUE);
253+
$this->assertTrue($this->redis->ttl('key') ===7);
254+
$this->assertTrue($this->redis->get('key') === 'val');
255+
}
249256

250257
public function testSetNX() {
251258

0 commit comments

Comments
 (0)