Skip to content

Commit 23b1a9d

Browse files
committed
Enable slot caching for session cluster
1 parent 5c7bc39 commit 23b1a9d

File tree

4 files changed

+96
-82
lines changed

4 files changed

+96
-82
lines changed

cluster_library.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <zend_exceptions.h>
88

99
extern zend_class_entry *redis_cluster_exception_ce;
10+
int le_cluster_slot_cache;
1011

1112
/* Debugging methods/
1213
static void cluster_dump_nodes(redisCluster *c) {
@@ -2767,4 +2768,85 @@ int mbulk_resp_loop_assoc(RedisSock *redis_sock, zval *z_result,
27672768
return SUCCESS;
27682769
}
27692770

2771+
/* Turn a seed array into a zend_string we can use to look up a slot cache */
2772+
zend_string *cluster_hash_seeds(HashTable *ht) {
2773+
smart_str hash = {0};
2774+
zend_string *zstr;
2775+
zval *z_seed;
2776+
2777+
ZEND_HASH_FOREACH_VAL(ht, z_seed) {
2778+
zstr = zval_get_string(z_seed);
2779+
smart_str_appendc(&hash, '[');
2780+
smart_str_appendl(&hash, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
2781+
smart_str_appendc(&hash, ']');
2782+
zend_string_release(zstr);
2783+
} ZEND_HASH_FOREACH_END();
2784+
2785+
/* Not strictly needed but null terminate anyway */
2786+
smart_str_0(&hash);
2787+
2788+
/* smart_str is a zend_string internally */
2789+
return hash.s;
2790+
}
2791+
2792+
2793+
#define SLOT_CACHING_ENABLED() (INI_INT("redis.clusters.cache_slots") == 1)
2794+
PHP_REDIS_API redisCachedCluster *cluster_cache_load(HashTable *ht_seeds) {
2795+
zend_resource *le;
2796+
zend_string *h;
2797+
2798+
/* Short circuit if we're not caching slots or if our seeds don't have any
2799+
* elements, since it doesn't make sense to cache an empty string */
2800+
if (!SLOT_CACHING_ENABLED() || zend_hash_num_elements(ht_seeds) == 0)
2801+
return NULL;
2802+
2803+
/* Look for cached slot information */
2804+
h = cluster_hash_seeds(ht_seeds);
2805+
le = zend_hash_str_find_ptr(&EG(persistent_list), ZSTR_VAL(h), ZSTR_LEN(h));
2806+
zend_string_release(h);
2807+
2808+
if (le != NULL) {
2809+
/* Sanity check on our list type */
2810+
if (le->type != le_cluster_slot_cache) {
2811+
php_error_docref(0, E_WARNING, "Invalid slot cache resource");
2812+
return NULL;
2813+
}
2814+
2815+
/* Success, return the cached entry */
2816+
return le->ptr;
2817+
}
2818+
2819+
/* Not found */
2820+
return NULL;
2821+
}
2822+
2823+
/* Cache a cluster's slot information in persistent_list if it's enabled */
2824+
PHP_REDIS_API int cluster_cache_store(HashTable *ht_seeds, HashTable *nodes) {
2825+
redisCachedCluster *cc;
2826+
zend_string *hash;
2827+
2828+
/* Short circuit if caching is disabled or there aren't any seeds */
2829+
if (!SLOT_CACHING_ENABLED() || zend_hash_num_elements(ht_seeds) == 0)
2830+
return !SLOT_CACHING_ENABLED() ? SUCCESS : FAILURE;
2831+
2832+
/* Construct our cache */
2833+
hash = cluster_hash_seeds(ht_seeds);
2834+
cc = cluster_cache_create(hash, nodes);
2835+
zend_string_release(hash);
2836+
2837+
/* Set up our resource */
2838+
#if PHP_VERSION_ID < 70300
2839+
zend_resource le;
2840+
le.type = le_cluster_slot_cache;
2841+
le.ptr = cc;
2842+
2843+
zend_hash_update_mem(&EG(persistent_list), cc->hash, (void*)&le, sizeof(zend_resource));
2844+
#else
2845+
zend_register_persistent_resource_ex(cc->hash, cc, le_cluster_slot_cache);
2846+
#endif
2847+
2848+
return SUCCESS;
2849+
}
2850+
2851+
27702852
/* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4: */

cluster_library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ PHP_REDIS_API void cluster_init_cache(redisCluster *c, redisCachedCluster *rcc);
397397

398398
PHP_REDIS_API char **cluster_sock_read_multibulk_reply(RedisSock *redis_sock,
399399
int *len);
400+
PHP_REDIS_API int cluster_cache_store(HashTable *ht_seeds, HashTable *nodes);
401+
PHP_REDIS_API redisCachedCluster *cluster_cache_load(HashTable *ht_seeds);
400402

401403
/*
402404
* Redis Cluster response handlers. Our response handlers generally take the

redis_cluster.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <SAPI.h>
3434

3535
zend_class_entry *redis_cluster_ce;
36-
int le_cluster_slot_cache;
3736

3837
/* Exception handler */
3938
zend_class_entry *redis_cluster_exception_ce;
@@ -343,85 +342,6 @@ void free_cluster_context(zend_object *object) {
343342
zend_object_std_dtor(&cluster->std);
344343
}
345344

346-
/* Turn a seed array into a zend_string we can use to look up a slot cache */
347-
static zend_string *cluster_hash_seeds(HashTable *ht) {
348-
smart_str hash = {0};
349-
zend_string *zstr;
350-
zval *z_seed;
351-
352-
ZEND_HASH_FOREACH_VAL(ht, z_seed) {
353-
zstr = zval_get_string(z_seed);
354-
smart_str_appendc(&hash, '[');
355-
smart_str_appendl(&hash, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
356-
smart_str_appendc(&hash, ']');
357-
zend_string_release(zstr);
358-
} ZEND_HASH_FOREACH_END();
359-
360-
/* Not strictly needed but null terminate anyway */
361-
smart_str_0(&hash);
362-
363-
/* smart_str is a zend_string internally */
364-
return hash.s;
365-
}
366-
367-
#define SLOT_CACHING_ENABLED() (INI_INT("redis.clusters.cache_slots") == 1)
368-
static redisCachedCluster *cluster_cache_load(HashTable *ht_seeds) {
369-
zend_resource *le;
370-
zend_string *h;
371-
372-
/* Short circuit if we're not caching slots or if our seeds don't have any
373-
* elements, since it doesn't make sense to cache an empty string */
374-
if (!SLOT_CACHING_ENABLED() || zend_hash_num_elements(ht_seeds) == 0)
375-
return NULL;
376-
377-
/* Look for cached slot information */
378-
h = cluster_hash_seeds(ht_seeds);
379-
le = zend_hash_str_find_ptr(&EG(persistent_list), ZSTR_VAL(h), ZSTR_LEN(h));
380-
zend_string_release(h);
381-
382-
if (le != NULL) {
383-
/* Sanity check on our list type */
384-
if (le->type != le_cluster_slot_cache) {
385-
php_error_docref(0, E_WARNING, "Invalid slot cache resource");
386-
return NULL;
387-
}
388-
389-
/* Success, return the cached entry */
390-
return le->ptr;
391-
}
392-
393-
/* Not found */
394-
return NULL;
395-
}
396-
397-
/* Cache a cluster's slot information in persistent_list if it's enabled */
398-
static int cluster_cache_store(HashTable *ht_seeds, HashTable *nodes) {
399-
redisCachedCluster *cc;
400-
zend_string *hash;
401-
402-
/* Short circuit if caching is disabled or there aren't any seeds */
403-
if (!SLOT_CACHING_ENABLED() || zend_hash_num_elements(ht_seeds) == 0)
404-
return !SLOT_CACHING_ENABLED() ? SUCCESS : FAILURE;
405-
406-
/* Construct our cache */
407-
hash = cluster_hash_seeds(ht_seeds);
408-
cc = cluster_cache_create(hash, nodes);
409-
zend_string_release(hash);
410-
411-
/* Set up our resource */
412-
#if PHP_VERSION_ID < 70300
413-
zend_resource le;
414-
le.type = le_cluster_slot_cache;
415-
le.ptr = cc;
416-
417-
zend_hash_update_mem(&EG(persistent_list), cc->hash, (void*)&le, sizeof(zend_resource));
418-
#else
419-
zend_register_persistent_resource_ex(cc->hash, cc, le_cluster_slot_cache);
420-
#endif
421-
422-
return SUCCESS;
423-
}
424-
425345
/* Validate redis cluster construction arguments */
426346
static int
427347
cluster_validate_args(double timeout, double read_timeout, HashTable *seeds) {

redis_session.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,10 +959,20 @@ PS_OPEN_FUNC(rediscluster) {
959959
if (auth && auth_len > 0) {
960960
c->auth = zend_string_init(auth, auth_len, 0);
961961
}
962-
if (!cluster_init_seeds(c, ht_seeds) && !cluster_map_keyspace(c)) {
962+
963+
redisCachedCluster *cc;
964+
965+
/* Attempt to load from cache */
966+
if ((cc = cluster_cache_load(ht_seeds))) {
967+
cluster_init_cache(c, cc);
963968
/* Set up our prefix */
964969
c->flags->prefix = zend_string_init(prefix, prefix_len, 0);
965-
970+
PS_SET_MOD_DATA(c);
971+
retval = SUCCESS;
972+
} else if (!cluster_init_seeds(c, ht_seeds) && !cluster_map_keyspace(c)) {
973+
/* Set up our prefix */
974+
c->flags->prefix = zend_string_init(prefix, prefix_len, 0);
975+
cluster_cache_store(ht_seeds, c->nodes);
966976
PS_SET_MOD_DATA(c);
967977
retval = SUCCESS;
968978
} else {

0 commit comments

Comments
 (0)