Skip to content

Commit f1fc0aa

Browse files
NormBliviuchircu
authored andcommitted
cachedb_redis: add Redis cluster hash tag support to redisHash
Fix redisHash() to extract hash tags per the Redis cluster spec and use the spec-mandated CRC16(key) mod 16384 bitmask.
1 parent e7d09a1 commit f1fc0aa

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

modules/cachedb_redis/cachedb_redis_utils.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,35 @@ uint16_t crc16(const char *buf, int len)
8181

8282
unsigned int redisHash(redis_con *con, str* key)
8383
{
84-
return crc16(key->s,key->len) & con->slots_assigned;
84+
int s, e;
85+
char *k;
86+
int klen;
87+
88+
if (!key || !key->s || key->len <= 0) {
89+
LM_ERR("redisHash called with invalid key\n");
90+
return 0;
91+
}
92+
93+
k = key->s;
94+
klen = key->len;
95+
96+
/* Hash tag extraction per Redis cluster spec:
97+
* If key contains {substring}, hash only the substring */
98+
for (s = 0; s < klen; s++)
99+
if (k[s] == '{') break;
100+
101+
if (s < klen) {
102+
for (e = s + 1; e < klen; e++)
103+
if (k[e] == '}') break;
104+
105+
if (e < klen && e != s + 1) {
106+
LM_DBG("hash tag detected: hashing '%.*s' from key '%.*s'\n",
107+
e - s - 1, k + s + 1, klen, k);
108+
return crc16(k + s + 1, e - s - 1) & 16383;
109+
}
110+
}
111+
112+
return crc16(k, klen) & 16383;
85113
}
86114

87115
cluster_node *get_redis_connection(redis_con *con,str *key)

0 commit comments

Comments
 (0)