Skip to content

Commit cde7124

Browse files
committed
feature: implemented shdict:ttl() and shdict:expire() APIs
1 parent db65d22 commit cde7124

File tree

3 files changed

+502
-1
lines changed

3 files changed

+502
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ install:
5656
- git clone https://github.com/openresty/openresty.git ../openresty
5757
- git clone https://github.com/openresty/openresty-devel-utils.git
5858
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
59-
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
59+
- git clone -b feat/shdict https://github.com/thibaultcha/lua-nginx-module.git ../lua-nginx-module
6060
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
6161
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
6262
- git clone https://github.com/openresty/lua-resty-lrucache.git

lib/resty/core/shdict.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ local type = type
1717
local error = error
1818
local ngx_shared = ngx.shared
1919
local getmetatable = getmetatable
20+
local FFI_ERROR = base.FFI_ERROR
21+
local FFI_DECLINED = base.FFI_DECLINED
2022

2123

2224
ffi.cdef[[
@@ -36,6 +38,12 @@ ffi.cdef[[
3638
int *forcible);
3739

3840
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);
3947
]]
4048

4149

@@ -387,6 +395,81 @@ local function shdict_flush_all(zone)
387395
end
388396

389397

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+
390473
if ngx_shared then
391474
local _, dict = next(ngx_shared, nil)
392475
if dict then
@@ -404,6 +487,8 @@ if ngx_shared then
404487
mt.replace = shdict_replace
405488
mt.delete = shdict_delete
406489
mt.flush_all = shdict_flush_all
490+
mt.ttl = shdict_ttl
491+
mt.expire = shdict_expire
407492
end
408493
end
409494
end

0 commit comments

Comments
 (0)