@@ -17,6 +17,8 @@ local type = type
17
17
local error = error
18
18
local ngx_shared = ngx .shared
19
19
local getmetatable = getmetatable
20
+ local FFI_ERROR = base .FFI_ERROR
21
+ local FFI_DECLINED = base .FFI_DECLINED
20
22
21
23
22
24
ffi .cdef [[
@@ -36,6 +38,12 @@ ffi.cdef[[
36
38
int * forcible );
37
39
38
40
int ngx_http_lua_ffi_shdict_flush_all (void * zone );
41
+
42
+ int ngx_http_lua_ffi_shdict_get_ttl (void * zone ,
43
+ const unsigned char * key , size_t key_len );
44
+
45
+ int ngx_http_lua_ffi_shdict_set_expire (void * zone ,
46
+ const unsigned char * key , size_t key_len , int exptime );
39
47
]]
40
48
41
49
@@ -387,6 +395,81 @@ local function shdict_flush_all(zone)
387
395
end
388
396
389
397
398
+ local function shdict_ttl (zone , key )
399
+ zone = check_zone (zone )
400
+
401
+ if key == nil then
402
+ return nil , " nil key"
403
+ end
404
+
405
+ if type (key ) ~= " string" then
406
+ key = tostring (key )
407
+ end
408
+
409
+ local key_len = # key
410
+ if key_len == 0 then
411
+ return nil , " empty key"
412
+ end
413
+
414
+ if key_len > 65535 then
415
+ return nil , " key too long"
416
+ end
417
+
418
+ local rc = C .ngx_http_lua_ffi_shdict_get_ttl (zone , key , key_len )
419
+
420
+ if rc == FFI_ERROR then
421
+ return nil , " bad zone"
422
+ end
423
+
424
+ if rc == FFI_DECLINED then
425
+ return nil , " not found"
426
+ end
427
+
428
+ return tonumber (rc ) / 1000
429
+ end
430
+
431
+
432
+ local function shdict_expire (zone , key , exptime )
433
+ zone = check_zone (zone )
434
+
435
+ if not exptime then
436
+ error (' bad "exptime" argument' , 2 )
437
+ end
438
+
439
+ if key == nil then
440
+ return nil , " nil key"
441
+ end
442
+
443
+ if type (key ) ~= " string" then
444
+ key = tostring (key )
445
+ end
446
+
447
+ local key_len = # key
448
+ if key_len == 0 then
449
+ return nil , " empty key"
450
+ end
451
+
452
+ if key_len > 65535 then
453
+ return nil , " key too long"
454
+ end
455
+
456
+ local rc = C .ngx_http_lua_ffi_shdict_set_expire (zone , key , key_len ,
457
+ exptime * 1000 )
458
+
459
+ if rc == FFI_ERROR then
460
+ return nil , " bad zone"
461
+ end
462
+
463
+ if rc == FFI_DECLINED then
464
+ return nil , " not found"
465
+ end
466
+
467
+ -- NGINX_OK/FFI_OK
468
+
469
+ return true
470
+ end
471
+
472
+
390
473
if ngx_shared then
391
474
local _ , dict = next (ngx_shared , nil )
392
475
if dict then
@@ -404,6 +487,8 @@ if ngx_shared then
404
487
mt .replace = shdict_replace
405
488
mt .delete = shdict_delete
406
489
mt .flush_all = shdict_flush_all
490
+ mt .ttl = shdict_ttl
491
+ mt .expire = shdict_expire
407
492
end
408
493
end
409
494
end
0 commit comments