Rework CLUSTER_PROCESS_CMD to use an underlying function

In theory this should reduce PhpRedis' code size and likely doesn't
affect performance in a measurable way.
This commit is contained in:
michael-grunder
2025-08-14 11:20:39 -07:00
committed by Michael Grunder
parent d564e8cf3c
commit 1db3908914
3 changed files with 46 additions and 19 deletions
+33 -1
View File
@@ -43,6 +43,38 @@ zend_class_entry *redis_cluster_exception_ce;
#include "redis_cluster_arginfo.h"
#endif
void
cluster_process_cmd(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c,
redis_cmd_cb cmd_cb, cluster_cb resp_cb, int readonly)
{
void *ctx = NULL;
int cmd_len;
short slot;
char *cmd;
c->readonly = readonly && CLUSTER_IS_ATOMIC(c);
if (cmd_cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, c->flags, &cmd, &cmd_len, &slot,
&ctx) == FAILURE)
{
RETURN_FALSE;
}
if (cluster_send_command(c, slot, cmd, cmd_len) < 0 || c->err != NULL) {
efree(cmd);
RETURN_FALSE;
}
efree(cmd);
if (c->flags->mode == MULTI) {
CLUSTER_ENQUEUE_RESPONSE(c, slot, resp_cb, ctx);
RETURN_ZVAL(getThis(), 1, 0);
}
resp_cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, ctx);
}
PHP_MINIT_FUNCTION(redis_cluster)
{
redis_cluster_ce = register_class_RedisCluster();
@@ -3245,7 +3277,7 @@ PHP_METHOD(RedisCluster, command) {
}
PHP_METHOD(RedisCluster, copy) {
CLUSTER_PROCESS_CMD(copy, cluster_1_resp, 0)
CLUSTER_PROCESS_CMD(copy, cluster_1_resp, 0);
}
/* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4: */
+6 -18
View File
@@ -2,6 +2,7 @@
#define REDIS_CLUSTER_H
#include "cluster_library.h"
#include "redis_commands.h"
#include <php.h>
#include <stddef.h>
@@ -52,25 +53,12 @@
c->flags->watching = 0; \
c->flags->mode = ATOMIC; \
/* Simple 1-1 command -> response macro */
void cluster_process_cmd(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c,
redis_cmd_cb cmd_cb, cluster_cb resp_cb, int readonly);
#define CLUSTER_PROCESS_CMD(cmdname, resp_func, readcmd) \
redisCluster *c = GET_CONTEXT(); \
c->readonly = CLUSTER_IS_ATOMIC(c) && readcmd; \
char *cmd; int cmd_len; short slot; void *ctx=NULL; \
if(redis_##cmdname##_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,c->flags, &cmd, \
&cmd_len, &slot, &ctx)==FAILURE) { \
RETURN_FALSE; \
} \
if(cluster_send_command(c,slot,cmd,cmd_len)<0 || c->err!=NULL) {\
efree(cmd); \
RETURN_FALSE; \
} \
efree(cmd); \
if(c->flags->mode == MULTI) { \
CLUSTER_ENQUEUE_RESPONSE(c, slot, resp_func, ctx); \
RETURN_ZVAL(getThis(), 1, 0); \
} \
resp_func(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, ctx);
cluster_process_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, GET_CONTEXT(), \
redis_##cmdname##_cmd, resp_func, readcmd)
/* More generic processing, where only the keyword differs */
#define CLUSTER_PROCESS_KW_CMD(kw, cmdfunc, resp_func, readcmd) \
+7
View File
@@ -31,6 +31,13 @@ int redis_build_raw_cmd(zval *z_args, int argc, char **cmd, int *cmd_len);
/* Construct a script command */
smart_string *redis_build_script_cmd(smart_string *cmd, int argc, zval *z_args);
typedef int (*redis_cmd_cb)(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx);
typedef int (*redis_kw_cmd_cb)(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx);
/* Redis command generics. Many commands share common prototypes meaning that
* we can write one function to handle all of them. For example, there are
* many COMMAND key value commands, or COMMAND key commands. */