Skip to content

Commit 8719dc4

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

File tree

3 files changed

+500
-1
lines changed

3 files changed

+500
-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: 83 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,79 @@ 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+
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+
390471
if ngx_shared then
391472
local _, dict = next(ngx_shared, nil)
392473
if dict then
@@ -404,6 +485,8 @@ if ngx_shared then
404485
mt.replace = shdict_replace
405486
mt.delete = shdict_delete
406487
mt.flush_all = shdict_flush_all
488+
mt.ttl = shdict_ttl
489+
mt.expire = shdict_expire
407490
end
408491
end
409492
end

0 commit comments

Comments
 (0)