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

Skip to content

Commit 981c693

Browse files
Add GETEX to README docs + minor change to command.
* Adds `GETEX` to the README.md documentation. * Allow the user to send `PERSIST` either as an array key or just in the array, to conform with similar methods. * Implement getEx for `RedisCluster` Fixes #2512
1 parent 74f9e80 commit 981c693

7 files changed

Lines changed: 85 additions & 10 deletions

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ $redis->slowLog('len');
787787
* [bitOp](#bitop) - Perform bitwise operations between strings
788788
* [decr, decrBy](#decr-decrby) - Decrement the value of a key
789789
* [get](#get) - Get the value of a key
790+
* [getEx](#getex) - Get the value of a key and set its expiration
790791
* [getBit](#getbit) - Returns the bit value at offset in the string value stored at key
791792
* [getRange](#getrange) - Get a substring of the string stored at a key
792793
* [getSet](#getset) - Set the string value of a key and return its old value
@@ -841,6 +842,28 @@ _**Description**_: Get the value related to the specified key
841842
$redis->get('key');
842843
~~~
843844

845+
### getEx
846+
-----
847+
_**Description**_: Get the value related to the specified key and set its expiration
848+
849+
##### *Parameters*
850+
*key*
851+
*options array* (optional) with the following keys:
852+
* `EX` - expire time in seconds
853+
* `PX` - expire time in milliseconds
854+
* `EXAT` - expire time in seconds since UNIX epoch
855+
* `PXAT` - expire time in milliseconds since UNIX epoch
856+
* `PERSIST` - remove the expiration from the key
857+
858+
##### *Return value*
859+
*String* or *Bool*: If key didn't exist, `FALSE` is returned. Otherwise, the value related to this key is returned.
860+
861+
##### *Examples*
862+
863+
~~~php
864+
$redis->getEx('key', ['EX' => 10]); // get key and set its expiration to 10 seconds
865+
~~~
866+
844867
### set
845868
-----
846869
_**Description**_: Set the string value in argument as value of the key. If you're using Redis >= 2.6.12, you can pass extended options as explained below

redis_cluster.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ PHP_METHOD(RedisCluster, msetnx) {
727727
}
728728
/* }}} */
729729

730+
PHP_METHOD(RedisCluster, getex) {
731+
CLUSTER_PROCESS_CMD(getex, cluster_bulk_resp, 0);
732+
}
733+
730734
/* {{{ proto bool RedisCluster::setex(string key, string value, int expiry) */
731735
PHP_METHOD(RedisCluster, setex) {
732736
CLUSTER_PROCESS_KW_CMD("SETEX", redis_key_long_val_cmd, cluster_bool_resp, 0);

redis_cluster.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ public function geosearchstore(string $dst, string $src, array|string $position,
390390
*/
391391
public function get(string $key): mixed;
392392

393+
/**
394+
* @see Redis::getEx
395+
*/
396+
public function getex(string $key, array $options = []): RedisCluster|string|false;
397+
393398
/**
394399
* @see Redis::getbit
395400
*/

redis_cluster_arginfo.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: c19108e54b637b6c76a529c1285104a0c38da220 */
2+
* Stub hash: 5713c5b2f88ddead50088f14026447801120fa33 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -325,6 +325,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_get, 0, 1, IS
325325
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
326326
ZEND_END_ARG_INFO()
327327

328+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_getex, 0, 1, RedisCluster, MAY_BE_STRING|MAY_BE_FALSE)
329+
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
330+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "[]")
331+
ZEND_END_ARG_INFO()
332+
328333
#define arginfo_class_RedisCluster_getbit arginfo_class_RedisCluster_decrby
329334

330335
#define arginfo_class_RedisCluster_getlasterror arginfo_class_RedisCluster__redir
@@ -1109,6 +1114,7 @@ ZEND_METHOD(RedisCluster, georadiusbymember_ro);
11091114
ZEND_METHOD(RedisCluster, geosearch);
11101115
ZEND_METHOD(RedisCluster, geosearchstore);
11111116
ZEND_METHOD(RedisCluster, get);
1117+
ZEND_METHOD(RedisCluster, getex);
11121118
ZEND_METHOD(RedisCluster, getbit);
11131119
ZEND_METHOD(RedisCluster, getlasterror);
11141120
ZEND_METHOD(RedisCluster, getmode);
@@ -1335,6 +1341,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
13351341
ZEND_ME(RedisCluster, geosearch, arginfo_class_RedisCluster_geosearch, ZEND_ACC_PUBLIC)
13361342
ZEND_ME(RedisCluster, geosearchstore, arginfo_class_RedisCluster_geosearchstore, ZEND_ACC_PUBLIC)
13371343
ZEND_ME(RedisCluster, get, arginfo_class_RedisCluster_get, ZEND_ACC_PUBLIC)
1344+
ZEND_ME(RedisCluster, getex, arginfo_class_RedisCluster_getex, ZEND_ACC_PUBLIC)
13381345
ZEND_ME(RedisCluster, getbit, arginfo_class_RedisCluster_getbit, ZEND_ACC_PUBLIC)
13391346
ZEND_ME(RedisCluster, getlasterror, arginfo_class_RedisCluster_getlasterror, ZEND_ACC_PUBLIC)
13401347
ZEND_ME(RedisCluster, getmode, arginfo_class_RedisCluster_getmode, ZEND_ACC_PUBLIC)

redis_cluster_legacy_arginfo.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: c19108e54b637b6c76a529c1285104a0c38da220 */
2+
* Stub hash: 5713c5b2f88ddead50088f14026447801120fa33 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)
@@ -295,6 +295,11 @@ ZEND_END_ARG_INFO()
295295

296296
#define arginfo_class_RedisCluster_get arginfo_class_RedisCluster__prefix
297297

298+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_getex, 0, 0, 1)
299+
ZEND_ARG_INFO(0, key)
300+
ZEND_ARG_INFO(0, options)
301+
ZEND_END_ARG_INFO()
302+
298303
#define arginfo_class_RedisCluster_getbit arginfo_class_RedisCluster_append
299304

300305
#define arginfo_class_RedisCluster_getlasterror arginfo_class_RedisCluster__masters
@@ -363,10 +368,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_hscan, 0, 0, 2)
363368
ZEND_ARG_INFO(0, count)
364369
ZEND_END_ARG_INFO()
365370

366-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_hrandfield, 0, 0, 1)
367-
ZEND_ARG_INFO(0, key)
368-
ZEND_ARG_INFO(0, options)
369-
ZEND_END_ARG_INFO()
371+
#define arginfo_class_RedisCluster_hrandfield arginfo_class_RedisCluster_getex
370372

371373
#define arginfo_class_RedisCluster_hset arginfo_class_RedisCluster_hincrby
372374

@@ -636,9 +638,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_smove, 0, 0, 3)
636638
ZEND_ARG_INFO(0, member)
637639
ZEND_END_ARG_INFO()
638640

639-
#define arginfo_class_RedisCluster_sort arginfo_class_RedisCluster_hrandfield
641+
#define arginfo_class_RedisCluster_sort arginfo_class_RedisCluster_getex
640642

641-
#define arginfo_class_RedisCluster_sort_ro arginfo_class_RedisCluster_hrandfield
643+
#define arginfo_class_RedisCluster_sort_ro arginfo_class_RedisCluster_getex
642644

643645
#define arginfo_class_RedisCluster_spop arginfo_class_RedisCluster_lpop
644646

@@ -824,7 +826,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_zrangestore, 0, 0, 4)
824826
ZEND_ARG_INFO(0, options)
825827
ZEND_END_ARG_INFO()
826828

827-
#define arginfo_class_RedisCluster_zrandmember arginfo_class_RedisCluster_hrandfield
829+
#define arginfo_class_RedisCluster_zrandmember arginfo_class_RedisCluster_getex
828830

829831
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_zrangebylex, 0, 0, 3)
830832
ZEND_ARG_INFO(0, key)
@@ -954,6 +956,7 @@ ZEND_METHOD(RedisCluster, georadiusbymember_ro);
954956
ZEND_METHOD(RedisCluster, geosearch);
955957
ZEND_METHOD(RedisCluster, geosearchstore);
956958
ZEND_METHOD(RedisCluster, get);
959+
ZEND_METHOD(RedisCluster, getex);
957960
ZEND_METHOD(RedisCluster, getbit);
958961
ZEND_METHOD(RedisCluster, getlasterror);
959962
ZEND_METHOD(RedisCluster, getmode);
@@ -1180,6 +1183,7 @@ static const zend_function_entry class_RedisCluster_methods[] = {
11801183
ZEND_ME(RedisCluster, geosearch, arginfo_class_RedisCluster_geosearch, ZEND_ACC_PUBLIC)
11811184
ZEND_ME(RedisCluster, geosearchstore, arginfo_class_RedisCluster_geosearchstore, ZEND_ACC_PUBLIC)
11821185
ZEND_ME(RedisCluster, get, arginfo_class_RedisCluster_get, ZEND_ACC_PUBLIC)
1186+
ZEND_ME(RedisCluster, getex, arginfo_class_RedisCluster_getex, ZEND_ACC_PUBLIC)
11831187
ZEND_ME(RedisCluster, getbit, arginfo_class_RedisCluster_getbit, ZEND_ACC_PUBLIC)
11841188
ZEND_ME(RedisCluster, getlasterror, arginfo_class_RedisCluster_getlasterror, ZEND_ACC_PUBLIC)
11851189
ZEND_ME(RedisCluster, getmode, arginfo_class_RedisCluster_getmode, ZEND_ACC_PUBLIC)

redis_commands.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,11 @@ redis_getex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
24762476
persist = zval_is_true(z_ele);
24772477
exp_type = NULL;
24782478
}
2479+
} else if (Z_TYPE_P(z_ele) == IS_STRING &&
2480+
zend_string_equals_literal_ci(Z_STR_P(z_ele), "PERSIST"))
2481+
{
2482+
persist = zval_is_true(z_ele);
2483+
exp_type = NULL;
24792484
}
24802485
} ZEND_HASH_FOREACH_END();
24812486
}

tests/RedisTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,35 @@ public function testExpiretime() {
790790
$this->redis->del('key1');
791791
}
792792

793-
public function testSetEx() {
793+
public function testGetEx() {
794+
if (version_compare($this->version, '6.2.0') < 0)
795+
$this->markTestSkipped();
796+
797+
$this->assertTrue($this->redis->set('key', 'value'));
798+
799+
$this->assertEquals('value', $this->redis->getEx('key', ['EX' => 100]));
800+
$this->assertBetween($this->redis->ttl('key'), 95, 100);
801+
802+
$this->assertEquals('value', $this->redis->getEx('key', ['PX' => 100000]));
803+
$this->assertBetween($this->redis->pttl('key'), 95000, 100000);
804+
805+
$this->assertEquals('value', $this->redis->getEx('key', ['EXAT' => time() + 200]));
806+
$this->assertBetween($this->redis->ttl('key'), 195, 200);
794807

808+
$this->assertEquals('value', $this->redis->getEx('key', ['PXAT' => (time()*1000) + 25000]));
809+
$this->assertBetween($this->redis->pttl('key'), 24000, 25000);
810+
811+
$this->assertEquals('value', $this->redis->getEx('key', ['PERSIST' => true]));
812+
$this->assertEquals(-1, $this->redis->ttl('key'));
813+
814+
$this->assertTrue($this->redis->expire('key', 100));
815+
$this->assertBetween($this->redis->ttl('key'), 95, 100);
816+
817+
$this->assertEquals('value', $this->redis->getEx('key', ['PERSIST']));
818+
$this->assertEquals(-1, $this->redis->ttl('key'));
819+
}
820+
821+
public function testSetEx() {
795822
$this->redis->del('key');
796823
$this->assertTrue($this->redis->setex('key', 7, 'val'));
797824
$this->assertEquals(7, $this->redis->ttl('key'));

0 commit comments

Comments
 (0)