mirror of
https://github.com/phpredis/phpredis.git
synced 2026-06-19 07:35:31 +00:00
Auto rehash keys on read miss + config.ini
This commit is contained in:
@@ -63,6 +63,7 @@ PHP_INI_BEGIN()
|
||||
PHP_INI_ENTRY("redis.arrays.previous", "", PHP_INI_ALL, NULL)
|
||||
PHP_INI_ENTRY("redis.arrays.functions", "", PHP_INI_ALL, NULL)
|
||||
PHP_INI_ENTRY("redis.arrays.index", "", PHP_INI_ALL, NULL)
|
||||
PHP_INI_ENTRY("redis.arrays.autorehash", "", PHP_INI_ALL, NULL)
|
||||
PHP_INI_END()
|
||||
|
||||
ZEND_DECLARE_MODULE_GLOBALS(redis)
|
||||
|
||||
+8
-4
@@ -229,7 +229,7 @@ PHP_METHOD(RedisArray, __construct)
|
||||
}
|
||||
|
||||
static void
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args) {
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) {
|
||||
|
||||
zval **zp_tmp, z_tmp;
|
||||
char *key;
|
||||
@@ -301,8 +301,12 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
|
||||
|
||||
// check if we have an error.
|
||||
if(failed && ra->prev && !b_write_cmd) { // there was an error reading, try with prev ring.
|
||||
/* php_printf("ERROR, FALLBACK TO PREVIOUS RING.\n"); */
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args);
|
||||
/* ERROR, FALLBACK TO PREVIOUS RING and forward a reference to the first redis instance we were looking at. */
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args, z_new_target?z_new_target:redis_inst);
|
||||
}
|
||||
|
||||
if(!failed && !b_write_cmd && z_new_target && ra->auto_rehash) { /* move key from old ring to new ring */
|
||||
ra_move_key(key, key_len, redis_inst, z_new_target TSRMLS_CC);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +332,7 @@ PHP_METHOD(RedisArray, __call)
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmd_len, z_args);
|
||||
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmd_len, z_args, NULL);
|
||||
}
|
||||
|
||||
PHP_METHOD(RedisArray, _hosts)
|
||||
|
||||
@@ -32,6 +32,7 @@ typedef struct RedisArray_ {
|
||||
zval *z_fun; /* key extractor */
|
||||
zval *z_pure_cmds; /* hash table */
|
||||
|
||||
int auto_rehash; /* migrate keys on read operations */
|
||||
struct RedisArray_ *prev;
|
||||
} RedisArray;
|
||||
|
||||
|
||||
+16
-2
@@ -152,9 +152,10 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
|
||||
zval *z_params_prev, **z_prev;
|
||||
zval *z_params_funs, **z_data_pp, *z_fun = NULL;
|
||||
zval *z_params_index;
|
||||
zval *z_params_autorehash;
|
||||
RedisArray *ra = NULL;
|
||||
|
||||
zend_bool b_index = 0;
|
||||
zend_bool b_index = 0, b_autorehash = 0;
|
||||
HashTable *hHosts = NULL, *hPrev = NULL;
|
||||
|
||||
/* find entry */
|
||||
@@ -197,8 +198,19 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
|
||||
}
|
||||
}
|
||||
|
||||
/* find autorehash option */
|
||||
MAKE_STD_ZVAL(z_params_autorehash);
|
||||
array_init(z_params_autorehash);
|
||||
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.autorehash")), z_params_autorehash TSRMLS_CC);
|
||||
if (zend_hash_find(Z_ARRVAL_P(z_params_autorehash), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
|
||||
if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) {
|
||||
b_autorehash = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* create RedisArray object */
|
||||
ra = ra_make_array(hHosts, z_fun, hPrev, b_index TSRMLS_CC);
|
||||
ra->auto_rehash = b_autorehash;
|
||||
|
||||
/* cleanup */
|
||||
zval_dtor(z_params_hosts);
|
||||
@@ -209,6 +221,8 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
|
||||
efree(z_params_funs);
|
||||
zval_dtor(z_params_index);
|
||||
efree(z_params_index);
|
||||
zval_dtor(z_params_autorehash);
|
||||
efree(z_params_autorehash);
|
||||
|
||||
return ra;
|
||||
}
|
||||
@@ -760,7 +774,7 @@ ra_move_list(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
|
||||
return ra_move_collection(key, key_len, z_from, z_to, 3, cmd_list, 1, cmd_add TSRMLS_CC);
|
||||
}
|
||||
|
||||
static void
|
||||
void
|
||||
ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC) {
|
||||
|
||||
long type = ra_get_key_type(z_from, key, key_len TSRMLS_CC);
|
||||
|
||||
@@ -11,6 +11,7 @@ RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, HashTable *hosts_prev,
|
||||
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC);
|
||||
void ra_init_function_table(RedisArray *ra);
|
||||
|
||||
void ra_move_key(const char *key, int key_len, zval *z_from, zval *z_to TSRMLS_DC);
|
||||
char * ra_find_key(RedisArray *ra, zval *z_args, const char *cmd, int *key_len);
|
||||
void ra_index_multi(zval *z_redis TSRMLS_DC);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user