Skip to content

Commit 9a81a68

Browse files
thibaultchaagentzh
authored andcommitted
feature: added pure C functions for shdict:ttl() and shdict:expire() API functions.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent c338c7b commit 9a81a68

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

src/ngx_http_lua_shdict.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,6 +2843,146 @@ ngx_http_lua_ffi_shdict_flush_all(ngx_shm_zone_t *zone)
28432843

28442844
return NGX_OK;
28452845
}
2846+
2847+
2848+
static ngx_int_t
2849+
ngx_http_lua_shdict_peek(ngx_shm_zone_t *shm_zone, ngx_uint_t hash,
2850+
u_char *kdata, size_t klen, ngx_http_lua_shdict_node_t **sdp)
2851+
{
2852+
ngx_int_t rc;
2853+
ngx_rbtree_node_t *node, *sentinel;
2854+
ngx_http_lua_shdict_ctx_t *ctx;
2855+
ngx_http_lua_shdict_node_t *sd;
2856+
2857+
ctx = shm_zone->data;
2858+
2859+
node = ctx->sh->rbtree.root;
2860+
sentinel = ctx->sh->rbtree.sentinel;
2861+
2862+
while (node != sentinel) {
2863+
2864+
if (hash < node->key) {
2865+
node = node->left;
2866+
continue;
2867+
}
2868+
2869+
if (hash > node->key) {
2870+
node = node->right;
2871+
continue;
2872+
}
2873+
2874+
/* hash == node->key */
2875+
2876+
sd = (ngx_http_lua_shdict_node_t *) &node->color;
2877+
2878+
rc = ngx_memn2cmp(kdata, sd->data, klen, (size_t) sd->key_len);
2879+
2880+
if (rc == 0) {
2881+
*sdp = sd;
2882+
2883+
return NGX_OK;
2884+
}
2885+
2886+
node = (rc < 0) ? node->left : node->right;
2887+
}
2888+
2889+
*sdp = NULL;
2890+
2891+
return NGX_DECLINED;
2892+
}
2893+
2894+
2895+
int
2896+
ngx_http_lua_ffi_shdict_get_ttl(ngx_shm_zone_t *zone, u_char *key,
2897+
size_t key_len)
2898+
{
2899+
uint32_t hash;
2900+
uint64_t now;
2901+
uint64_t expires;
2902+
ngx_int_t rc;
2903+
ngx_time_t *tp;
2904+
ngx_http_lua_shdict_ctx_t *ctx;
2905+
ngx_http_lua_shdict_node_t *sd;
2906+
2907+
if (zone == NULL) {
2908+
return NGX_ERROR;
2909+
}
2910+
2911+
ctx = zone->data;
2912+
hash = ngx_crc32_short(key, key_len);
2913+
2914+
ngx_shmtx_lock(&ctx->shpool->mutex);
2915+
2916+
rc = ngx_http_lua_shdict_peek(zone, hash, key, key_len, &sd);
2917+
2918+
if (rc == NGX_DECLINED) {
2919+
ngx_shmtx_unlock(&ctx->shpool->mutex);
2920+
2921+
return NGX_DECLINED;
2922+
}
2923+
2924+
/* rc == NGX_OK */
2925+
2926+
expires = sd->expires;
2927+
2928+
ngx_shmtx_unlock(&ctx->shpool->mutex);
2929+
2930+
if (expires == 0) {
2931+
return 0;
2932+
}
2933+
2934+
tp = ngx_timeofday();
2935+
now = (uint64_t) tp->sec * 1000 + tp->msec;
2936+
2937+
return expires - now;
2938+
}
2939+
2940+
2941+
int
2942+
ngx_http_lua_ffi_shdict_set_expire(ngx_shm_zone_t *zone, u_char *key,
2943+
size_t key_len, int exptime)
2944+
{
2945+
uint32_t hash;
2946+
ngx_int_t rc;
2947+
ngx_time_t *tp = NULL;
2948+
ngx_http_lua_shdict_ctx_t *ctx;
2949+
ngx_http_lua_shdict_node_t *sd;
2950+
2951+
if (zone == NULL) {
2952+
return NGX_ERROR;
2953+
}
2954+
2955+
if (exptime > 0) {
2956+
tp = ngx_timeofday();
2957+
}
2958+
2959+
ctx = zone->data;
2960+
hash = ngx_crc32_short(key, key_len);
2961+
2962+
ngx_shmtx_lock(&ctx->shpool->mutex);
2963+
2964+
rc = ngx_http_lua_shdict_peek(zone, hash, key, key_len, &sd);
2965+
2966+
if (rc == NGX_DECLINED) {
2967+
ngx_shmtx_unlock(&ctx->shpool->mutex);
2968+
2969+
return NGX_DECLINED;
2970+
}
2971+
2972+
/* rc == NGX_OK */
2973+
2974+
if (exptime > 0) {
2975+
sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
2976+
+ (uint64_t) exptime;
2977+
2978+
} else {
2979+
sd->expires = 0;
2980+
}
2981+
2982+
ngx_shmtx_unlock(&ctx->shpool->mutex);
2983+
2984+
return NGX_OK;
2985+
}
28462986
#endif /* NGX_LUA_NO_FFI_API */
28472987

28482988

0 commit comments

Comments
 (0)