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

Skip to content

Commit 9a3fe40

Browse files
Implement new RESTORE options
Add the new RESTORE options REPLACE, ABSTTL, FREQ <freq> and IDLETIME <idletime> Fixes #1410
1 parent 6ea978e commit 9a3fe40

11 files changed

Lines changed: 142 additions & 12 deletions

redis.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3025,8 +3025,7 @@ PHP_METHOD(Redis, dump) {
30253025

30263026
/* {{{ proto Redis::restore(ttl, key, value) */
30273027
PHP_METHOD(Redis, restore) {
3028-
REDIS_PROCESS_KW_CMD("RESTORE", redis_key_long_str_cmd,
3029-
redis_boolean_response);
3028+
REDIS_PROCESS_CMD(restore, redis_boolean_response);
30303029
}
30313030
/* }}} */
30323031

redis.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public function renameNx(string $key_src, string $key_dst);
364364

365365
public function reset(): bool;
366366

367-
public function restore(string $key, int $timeout, string $value): bool;
367+
public function restore(string $key, int $timeout, string $value, ?array $options = NULL): bool;
368368

369369
public function role(): mixed;
370370

redis_arginfo.h

Lines changed: 2 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: f547b5f24c4d373043c89dab57d450d27f959b08 */
2+
* Stub hash: 2c4ee6dc4a5aa66b1700df8859233c349aa00519 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -642,6 +642,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_restore, 0, 3, _IS_B
642642
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
643643
ZEND_ARG_TYPE_INFO(0, timeout, IS_LONG, 0)
644644
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
645+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "NULL")
645646
ZEND_END_ARG_INFO()
646647

647648
#define arginfo_class_Redis_role arginfo_class_Redis_getAuth

redis_cluster.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,7 @@ PHP_METHOD(RedisCluster, pfmerge) {
14181418

14191419
/* {{{ proto boolean RedisCluster::restore(string key, long ttl, string val) */
14201420
PHP_METHOD(RedisCluster, restore) {
1421-
CLUSTER_PROCESS_KW_CMD("RESTORE", redis_key_long_str_cmd,
1422-
cluster_bool_resp, 0);
1421+
CLUSTER_PROCESS_CMD(restore, cluster_bool_resp, 0);
14231422
}
14241423
/* }}} */
14251424

redis_cluster.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function rename(string $key, string $newkey): bool;
248248

249249
public function renamenx(string $key, string $newkey): bool;
250250

251-
public function restore(string $key, int $timeout, string $value): bool;
251+
public function restore(string $key, int $timeout, string $value, ?array $options = NULL): bool;
252252

253253
public function role(string|array $key_or_address): mixed;
254254

redis_cluster_arginfo.h

Lines changed: 7 additions & 2 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: 75e03c96590793af52efbea1d6440d3daa57a5d8 */
2+
* Stub hash: 956f295e74025def86150d0acdf7a11594c72d47 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -526,7 +526,12 @@ ZEND_END_ARG_INFO()
526526

527527
#define arginfo_class_RedisCluster_renamenx arginfo_class_RedisCluster_rename
528528

529-
#define arginfo_class_RedisCluster_restore arginfo_class_RedisCluster_psetex
529+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_restore, 0, 3, _IS_BOOL, 0)
530+
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
531+
ZEND_ARG_TYPE_INFO(0, timeout, IS_LONG, 0)
532+
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
533+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 1, "NULL")
534+
ZEND_END_ARG_INFO()
530535

531536
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_role, 0, 1, IS_MIXED, 0)
532537
ZEND_ARG_TYPE_MASK(0, key_or_address, MAY_BE_STRING|MAY_BE_ARRAY, NULL)

redis_cluster_legacy_arginfo.h

Lines changed: 7 additions & 2 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: 75e03c96590793af52efbea1d6440d3daa57a5d8 */
2+
* Stub hash: 956f295e74025def86150d0acdf7a11594c72d47 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)
@@ -459,7 +459,12 @@ ZEND_END_ARG_INFO()
459459

460460
#define arginfo_class_RedisCluster_renamenx arginfo_class_RedisCluster_rename
461461

462-
#define arginfo_class_RedisCluster_restore arginfo_class_RedisCluster_psetex
462+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_restore, 0, 0, 3)
463+
ZEND_ARG_INFO(0, key)
464+
ZEND_ARG_INFO(0, timeout)
465+
ZEND_ARG_INFO(0, value)
466+
ZEND_ARG_INFO(0, options)
467+
ZEND_END_ARG_INFO()
463468

464469
#define arginfo_class_RedisCluster_role arginfo_class_RedisCluster_bgrewriteaof
465470

redis_commands.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ typedef struct redisLcsOptions {
6565
zend_bool withmatchlen;
6666
} redisLcsOptions;
6767

68+
typedef struct redisRestoreOptions {
69+
zend_bool replace;
70+
zend_bool absttl;
71+
zend_long idletime;
72+
zend_long freq;
73+
} redisRestoreOptions;
74+
6875
/* Local passthrough macro for command construction. Given that these methods
6976
* are generic (so they work whether the caller is Redis or RedisCluster) we
7077
* will always have redis_sock, slot*, and */
@@ -2411,6 +2418,100 @@ int redis_lcs_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
24112418
return SUCCESS;
24122419
}
24132420

2421+
void redis_get_restore_options(redisRestoreOptions *dst, HashTable *ht) {
2422+
zend_string *key;
2423+
zend_long lval;
2424+
zval *zv;
2425+
2426+
ZEND_ASSERT(dst != NULL);
2427+
2428+
memset(dst, 0, sizeof(*dst));
2429+
dst->idletime = dst->freq = -1;
2430+
2431+
if (ht == NULL)
2432+
return;
2433+
2434+
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, zv) {
2435+
ZVAL_DEREF(zv);
2436+
2437+
if (key) {
2438+
if (zend_string_equals_literal_ci(key, "IDLETIME")) {
2439+
lval = zval_get_long(zv);
2440+
if (lval < 0) {
2441+
php_error_docref(NULL, E_WARNING, "IDLETIME must be >= 0");
2442+
} else {
2443+
dst->idletime = lval;
2444+
dst->freq = -1;
2445+
}
2446+
} else if (zend_string_equals_literal_ci(key, "FREQ")) {
2447+
lval = zval_get_long(zv);
2448+
if (lval < 0 || lval > 255) {
2449+
php_error_docref(NULL, E_WARNING, "FREQ must be >= 0 and <= 255");
2450+
} else {
2451+
dst->freq = lval;
2452+
dst->idletime = -1;
2453+
}
2454+
} else {
2455+
php_error_docref(NULL, E_WARNING, "Unknown RESTORE option '%s'", ZSTR_VAL(key));
2456+
}
2457+
} else if (Z_TYPE_P(zv) == IS_STRING) {
2458+
if (zend_string_equals_literal_ci(Z_STR_P(zv), "REPLACE")) {
2459+
dst->replace = 1;
2460+
} else if (zend_string_equals_literal_ci(Z_STR_P(zv), "ABSTTL")) {
2461+
dst->absttl = 1;
2462+
} else {
2463+
php_error_docref(NULL, E_WARNING, "Unknown RESTORE option '%s'", Z_STRVAL_P(zv));
2464+
}
2465+
}
2466+
} ZEND_HASH_FOREACH_END();
2467+
}
2468+
2469+
/* RESTORE */
2470+
int redis_restore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
2471+
char **cmd, int *cmd_len, short *slot, void **ctx)
2472+
{
2473+
zend_string *key, *value = NULL;
2474+
smart_string cmdstr = {0};
2475+
HashTable *options = NULL;
2476+
redisRestoreOptions opt;
2477+
zend_long timeout = 0;
2478+
int argc;
2479+
2480+
ZEND_PARSE_PARAMETERS_START(3, 4) {
2481+
Z_PARAM_STR(key)
2482+
Z_PARAM_LONG(timeout)
2483+
Z_PARAM_STR(value)
2484+
Z_PARAM_OPTIONAL
2485+
Z_PARAM_ARRAY_HT_OR_NULL(options)
2486+
} ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
2487+
2488+
redis_get_restore_options(&opt, options);
2489+
2490+
argc = 3 + (opt.idletime>-1?2:0) + (opt.freq>-1?2:0) + !!opt.absttl + !!opt.replace;
2491+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "RESTORE");
2492+
2493+
redis_cmd_append_sstr_key(&cmdstr, ZSTR_VAL(key), ZSTR_LEN(key), redis_sock, slot);
2494+
redis_cmd_append_sstr_long(&cmdstr, timeout);
2495+
redis_cmd_append_sstr_zstr(&cmdstr, value);
2496+
2497+
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.replace, "REPLACE");
2498+
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.absttl, "ABSTTL");
2499+
2500+
if (opt.idletime > -1) {
2501+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "IDLETIME");
2502+
redis_cmd_append_sstr_long(&cmdstr, opt.idletime);
2503+
}
2504+
2505+
if (opt.freq > -1) {
2506+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FREQ");
2507+
redis_cmd_append_sstr_long(&cmdstr, opt.freq);
2508+
}
2509+
2510+
*cmd = cmdstr.c;
2511+
*cmd_len = cmdstr.len;
2512+
2513+
return SUCCESS;
2514+
}
24142515

24152516
/* BITPOS */
24162517
int redis_bitpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,

redis_commands.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ int redis_lcs_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
135135
int redis_mpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
136136
char **cmd, int *cmd_len, short *slot, void **ctx);
137137

138+
int redis_restore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
139+
char **cmd, int *cmd_len, short *slot, void **ctx);
140+
138141
int redis_subscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
139142
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx);
140143

redis_legacy_arginfo.h

Lines changed: 2 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: f547b5f24c4d373043c89dab57d450d27f959b08 */
2+
* Stub hash: 2c4ee6dc4a5aa66b1700df8859233c349aa00519 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -552,6 +552,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_restore, 0, 0, 3)
552552
ZEND_ARG_INFO(0, key)
553553
ZEND_ARG_INFO(0, timeout)
554554
ZEND_ARG_INFO(0, value)
555+
ZEND_ARG_INFO(0, options)
555556
ZEND_END_ARG_INFO()
556557

557558
#define arginfo_class_Redis_role arginfo_class_Redis___destruct

0 commit comments

Comments
 (0)