@@ -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,79 @@ 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
+ if key_len > 65535 then
414
+ return nil , " key too long"
415
+ end
416
+
417
+ local rc = C .ngx_http_lua_ffi_shdict_get_ttl (zone , key , key_len )
418
+
419
+ if rc == FFI_ERROR then
420
+ return nil , " bad zone"
421
+ end
422
+
423
+ if rc == FFI_DECLINED then
424
+ return nil , " not found"
425
+ end
426
+
427
+ return tonumber (rc ) / 1000
428
+ end
429
+
430
+
431
+ local function shdict_expire (zone , key , exptime )
432
+ zone = check_zone (zone )
433
+
434
+ if not exptime then
435
+ error (' bad "exptime" argument' , 2 )
436
+ end
437
+
438
+ if key == nil then
439
+ return nil , " nil key"
440
+ end
441
+
442
+ if type (key ) ~= " string" then
443
+ key = tostring (key )
444
+ end
445
+
446
+ local key_len = # key
447
+ if key_len == 0 then
448
+ return nil , " empty key"
449
+ end
450
+ if key_len > 65535 then
451
+ return nil , " key too long"
452
+ end
453
+
454
+ local rc = C .ngx_http_lua_ffi_shdict_set_expire (zone , key , key_len ,
455
+ exptime * 1000 )
456
+
457
+ if rc == FFI_ERROR then
458
+ return nil , " bad zone"
459
+ end
460
+
461
+ if rc == FFI_DECLINED then
462
+ return nil , " not found"
463
+ end
464
+
465
+ -- NGINX_OK/FFI_OK
466
+
467
+ return true
468
+ end
469
+
470
+
390
471
if ngx_shared then
391
472
local _ , dict = next (ngx_shared , nil )
392
473
if dict then
@@ -404,6 +485,8 @@ if ngx_shared then
404
485
mt .replace = shdict_replace
405
486
mt .delete = shdict_delete
406
487
mt .flush_all = shdict_flush_all
488
+ mt .ttl = shdict_ttl
489
+ mt .expire = shdict_expire
407
490
end
408
491
end
409
492
end
0 commit comments