Skip to content

Commit b76474c

Browse files
author
Nicolas Favre-Felix
committed
Added expireAt + test + documentation.
1 parent a40ec71 commit b76474c

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

README.markdown

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ $redis->get('x'); // → `FALSE`
836836
##### *Description*
837837
Same as rename, but will not replace a key if the destination already exists. This is the same behaviour as setNx.
838838

839-
## setTimeout
839+
## setTimeout, expire
840840
##### *Description*
841841
Sets an expiration date (a timeout) on an item.
842842

@@ -855,6 +855,26 @@ sleep(5); // wait 5 seconds
855855
$this->get('x'); // will return `FALSE`, as 'x' has expired.
856856
</pre>
857857

858+
## expireAt
859+
##### *Description*
860+
Sets an expiration date (a timestamp) on an item.
861+
862+
##### *Parameters*
863+
*Key*: key. The key that will disappear.
864+
865+
*Integer*: Unix timestamp. The key's date of death, in seconds from Epoch time.
866+
867+
##### *Return value*
868+
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
869+
##### *Example*
870+
<pre>
871+
$redis->set('x', '42');
872+
$now = time(NULL); // current timestamp
873+
$redis->setTimeout('x', $now + 3); // x will disappear in 3 seconds.
874+
sleep(5); // wait 5 seconds
875+
$this->get('x'); // will return `FALSE`, as 'x' has expired.
876+
</pre>
877+
858878
## getKeys
859879
##### *Description*
860880
Returns the keys that match a certain pattern.

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ PHP_METHOD(Redis, zDeleteRangeByScore);
8888
PHP_METHOD(Redis, zCard);
8989
PHP_METHOD(Redis, zScore);
9090
PHP_METHOD(Redis, zIncrBy);
91+
PHP_METHOD(Redis, expireAt);
9192

9293
PHP_METHOD(Redis, mset);
9394
PHP_METHOD(Redis, rpoplpush);

redis.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static zend_function_entry redis_functions[] = {
108108
PHP_ME(Redis, zCard, NULL, ZEND_ACC_PUBLIC)
109109
PHP_ME(Redis, zScore, NULL, ZEND_ACC_PUBLIC)
110110
PHP_ME(Redis, zIncrBy, NULL, ZEND_ACC_PUBLIC)
111+
PHP_ME(Redis, expireAt, NULL, ZEND_ACC_PUBLIC)
111112

112113
/* aliases */
113114
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
@@ -2148,27 +2149,27 @@ PHP_METHOD(Redis, sortDescAlpha)
21482149
}
21492150
/* }}} */
21502151

2151-
/* {{{ proto array Redis::setTimeout(string key, int timeout)
2152-
*/
2153-
PHP_METHOD(Redis, setTimeout) {
2154-
2152+
PHPAPI void generic_expire_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len TSRMLS_DC) {
21552153
zval *object;
21562154
RedisSock *redis_sock;
21572155
char *key = NULL, *cmd;
21582156
int key_len, cmd_len;
2159-
long timeout;
2157+
long t;
21602158

21612159
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl",
21622160
&object, redis_ce, &key, &key_len,
2163-
&timeout) == FAILURE) {
2161+
&t) == FAILURE) {
21642162
RETURN_FALSE;
21652163
}
21662164

21672165
if (redis_sock_get(object, &redis_sock TSRMLS_CC) < 0) {
21682166
RETURN_FALSE;
21692167
}
21702168

2171-
cmd_len = spprintf(&cmd, 0, "EXPIRE %s %d\r\n", key, (int)timeout);
2169+
cmd_len = redis_cmd_format(&cmd, "%s %s %d\r\n",
2170+
keyword, keyword_len,
2171+
key, key_len,
2172+
(int)t);
21722173

21732174
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
21742175
efree(cmd);
@@ -2177,8 +2178,22 @@ PHP_METHOD(Redis, setTimeout) {
21772178
efree(cmd);
21782179
redis_1_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock TSRMLS_CC);
21792180
}
2181+
2182+
/* {{{ proto array Redis::setTimeout(string key, int timeout)
2183+
*/
2184+
PHP_METHOD(Redis, setTimeout) {
2185+
generic_expire_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "EXPIRE", sizeof("EXPIRE")-1 TSRMLS_CC);
2186+
}
21802187
/* }}} */
21812188

2189+
/* {{{ proto array Redis::expireAt(string key, int timestamp)
2190+
*/
2191+
PHP_METHOD(Redis, expireAt) {
2192+
generic_expire_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "EXPIREAT", sizeof("EXPIREAT")-1 TSRMLS_CC);
2193+
}
2194+
/* }}} */
2195+
2196+
21822197
/* {{{ proto array Redis::lSet(string key, int index, string value)
21832198
*/
21842199
PHP_METHOD(Redis, lSet) {

tests/TestRedis.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ public function testSetTimeout() {
235235
$this->assertEquals(False, $this->redis->get('key'));
236236
}
237237

238+
public function testExpireAt() {
239+
240+
$this->redis->delete('key');
241+
$this->redis->set('key', 'value');
242+
$now = time(NULL);
243+
$this->redis->expireAt('key', $now + 1);
244+
$this->assertEquals('value', $this->redis->get('key'));
245+
sleep(2);
246+
$this->assertEquals(FALSE, $this->redis->get('key'));
247+
}
248+
238249
public function testSetNX() {
239250

240251
$this->redis->set('key', 42);

0 commit comments

Comments
 (0)