Skip to content

Commit 07d7282

Browse files
committed
update feature for cache key auto alloc and free when using nginx share memory cache
1 parent 41fc619 commit 07d7282

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/ngx_http_c_func_module.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ ngx_http_c_func_shm_free_locked(void *shared_mem, void *ptr) {
11651165

11661166
void*
11671167
ngx_http_c_func_cache_get(void *shared_mem, const char* key) {
1168-
ngx_str_t str_key = ngx_string(key);
1168+
ngx_str_t str_key = { ngx_strlen(key), (u_char *) key };
11691169
uint32_t hash = ngx_crc32_long(str_key.data, str_key.len);
11701170
ngx_http_c_func_http_shm_t *_cache = (ngx_http_c_func_http_shm_t *)shared_mem;
11711171
ngx_http_c_func_http_cache_value_node_t *cvnt = (ngx_http_c_func_http_cache_value_node_t *)
@@ -1183,10 +1183,11 @@ ngx_http_c_func_cache_get(void *shared_mem, const char* key) {
11831183
*/
11841184
void*
11851185
ngx_http_c_func_cache_put(void *shared_mem, const char* key, void* value) {
1186+
ngx_http_c_func_http_shm_t *_cache = (ngx_http_c_func_http_shm_t *)shared_mem;
1187+
11861188
void *old_value;
1187-
ngx_str_t str_key = ngx_string(key);
1189+
ngx_str_t str_key = { ngx_strlen(key), (u_char *) key };
11881190
uint32_t hash = ngx_crc32_long(str_key.data, str_key.len);
1189-
ngx_http_c_func_http_shm_t *_cache = (ngx_http_c_func_http_shm_t *)shared_mem;
11901191
ngx_http_c_func_http_cache_value_node_t *cvnt = (ngx_http_c_func_http_cache_value_node_t *)
11911192
ngx_str_rbtree_lookup(&_cache->rbtree, &str_key, hash);
11921193
if (cvnt) {
@@ -1201,43 +1202,53 @@ ngx_http_c_func_cache_put(void *shared_mem, const char* key, void* value) {
12011202
}
12021203
cvnt->value = value;
12031204
cvnt->sn.node.key = hash;
1204-
ngx_str_set(&cvnt->sn.str , key);
1205+
1206+
ngx_str_t *new_str_key = &(cvnt->sn.str);
1207+
new_str_key->len = str_key.len;
1208+
new_str_key->data = (u_char*) ngx_slab_alloc_locked(_cache->shpool, sizeof(u_char) * (new_str_key->len + 1) );
1209+
ngx_memcpy(new_str_key->data, str_key.data, new_str_key->len);
1210+
new_str_key->data[new_str_key->len] = 0;
12051211
ngx_rbtree_insert(&_cache->rbtree, &cvnt->sn.node);
12061212
return NULL;
12071213
}
12081214
}
12091215

12101216
void*
12111217
ngx_http_c_func_cache_new(void *shared_mem, const char* key, size_t size) {
1212-
ngx_str_t str_key = ngx_string(key);
1213-
uint32_t hash = ngx_crc32_long(str_key.data, str_key.len);
12141218
ngx_http_c_func_http_shm_t *_cache = (ngx_http_c_func_http_shm_t *)shared_mem;
1215-
12161219
ngx_http_c_func_http_cache_value_node_t *cvnt = (ngx_http_c_func_http_cache_value_node_t *)
12171220
ngx_slab_alloc_locked(_cache->shpool, sizeof(ngx_http_c_func_http_cache_value_node_t));
12181221

12191222
if (cvnt == NULL) {
12201223
return NULL;
12211224
}
12221225

1226+
ngx_str_t *str_key = &(cvnt->sn.str);
1227+
str_key->len = ngx_strlen(key);
1228+
str_key->data = (u_char*) ngx_slab_alloc_locked(_cache->shpool, sizeof(u_char) * (str_key->len + 1) );
1229+
ngx_memcpy(str_key->data, key, str_key->len);
1230+
str_key->data[str_key->len] = 0;
1231+
1232+
uint32_t hash = ngx_crc32_long(str_key->data, str_key->len);
1233+
12231234
cvnt->value = ngx_slab_alloc_locked(_cache->shpool, size);
12241235
cvnt->sn.node.key = hash;
1225-
ngx_str_set(&cvnt->sn.str , key);
12261236
ngx_rbtree_insert(&_cache->rbtree, &cvnt->sn.node);
12271237
return cvnt->value;
12281238
}
12291239

12301240
void*
12311241
ngx_http_c_func_cache_remove(void *shared_mem, const char* key) {
12321242
void *old_value;
1233-
ngx_str_t str_key = ngx_string(key);
1243+
ngx_str_t str_key = { ngx_strlen(key), (u_char *) key };
12341244
uint32_t hash = ngx_crc32_long(str_key.data, str_key.len);
12351245
ngx_http_c_func_http_shm_t *_cache = (ngx_http_c_func_http_shm_t *)shared_mem;
12361246
ngx_http_c_func_http_cache_value_node_t *cvnt = (ngx_http_c_func_http_cache_value_node_t *)
12371247
ngx_str_rbtree_lookup(&_cache->rbtree, &str_key, hash);
12381248

12391249
if (cvnt) {
12401250
old_value = cvnt->value;
1251+
ngx_slab_free_locked(_cache->shpool, cvnt->sn.str.data);
12411252
ngx_rbtree_delete(&_cache->rbtree, &cvnt->sn.node);
12421253
return old_value;
12431254
}

0 commit comments

Comments
 (0)