Skip to content

doc: documented the shdict:ttl() and shdict:expire() API functions. #1157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3191,6 +3191,8 @@ Nginx API for Lua
* [ngx.shared.DICT.lpop](#ngxshareddictlpop)
* [ngx.shared.DICT.rpop](#ngxshareddictrpop)
* [ngx.shared.DICT.llen](#ngxshareddictllen)
* [ngx.shared.DICT.ttl](#ngxshareddictttl)
* [ngx.shared.DICT.expire](#ngxshareddictexpire)
* [ngx.shared.DICT.flush_all](#ngxshareddictflush_all)
* [ngx.shared.DICT.flush_expired](#ngxshareddictflush_expired)
* [ngx.shared.DICT.get_keys](#ngxshareddictget_keys)
Expand Down Expand Up @@ -6198,6 +6200,8 @@ The resulting object `dict` has the following methods:
* [lpop](#ngxshareddictlpop)
* [rpop](#ngxshareddictrpop)
* [llen](#ngxshareddictllen)
* [ttl](#ngxshareddictttl)
* [expire](#ngxshareddictexpire)
* [flush_all](#ngxshareddictflush_all)
* [flush_expired](#ngxshareddictflush_expired)
* [get_keys](#ngxshareddictget_keys)
Expand Down Expand Up @@ -6543,6 +6547,82 @@ See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.ttl
-------------------
**syntax:** *ttl, err = ngx.shared.DICT:ttl(key)*

**context:** *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**

**requires:** `resty.core.shdict` or `resty.core`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial idea is to introduce this new requires section to make this obvious early in the reading of this method's documentation. There is also a note at the bottom of it with a link to the lua-resty-core library. Open to suggestions!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaultcha That's fine, though we'll soon make lua-resty-core mandatory and loaded by default in the next ngx_lua release anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agentzh Yeah - I remember :) Alright then!


Retrieves the remaining TTL (time-to-live in seconds) of a key-value pair in the shm-based dictionary [ngx.shared.DICT](#ngxshareddict). Returns the TTL as a number if the operation is successfully completed or `nil` and an error message otherwise.

If the key does not exist (or has already expired), this method will return `nil` and the error string `"not found"`.

The TTL is originally determined by the `exptime` argument of the [set](#ngxshareddictset), [add](#ngxshareddictadd), [replace](#ngxshareddictreplace) (and the likes) methods. It has a time resolution of `0.001` seconds. A value of `0` means that the item will never expire.

Example:

```lua

require "resty.core"

local cats = ngx.shared.cats
local succ, err = cats:set("Marry", "a nice cat", 0.5)

ngx.sleep(0.2)

local ttl, err = cats:ttl("Marry")
ngx.say(ttl) -- 0.3
```

This feature was first introduced in the `v0.10.11` release.

**Note:** This method requires the `resty.core.shdict` or `resty.core` modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.

See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.expire
----------------------
**syntax:** *success, err = ngx.shared.DICT:expire(key, exptime)*

**context:** *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**

**requires:** `resty.core.shdict` or `resty.core`

Updates the `exptime` (in second) of a key-value pair in the shm-based dictionary [ngx.shared.DICT](#ngxshareddict). Returns a boolean indicating success if the operation completes or `nil` and an error message otherwise.

If the key does not exist, this method will return `nil` and the error string `"not found"`.

The `exptime` argument has a resolution of `0.001` seconds. If `exptime` is `0`, then the item will never expire.

Example:

```lua

require "resty.core"

local cats = ngx.shared.cats
local succ, err = cats:set("Marry", "a nice cat", 0.1)

succ, err = cats:expire("Marry", 0.5)

ngx.sleep(0.2)

local val, err = cats:get("Marry")
ngx.say(val) -- "a nice cat"
```

This feature was first introduced in the `v0.10.11` release.

**Note:** This method requires the `resty.core.shdict` or `resty.core` modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.

See also [ngx.shared.DICT](#ngxshareddict).

[Back to TOC](#nginx-api-for-lua)

ngx.shared.DICT.flush_all
-------------------------
**syntax:** *ngx.shared.DICT:flush_all()*
Expand Down
70 changes: 70 additions & 0 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -5196,6 +5196,8 @@ The resulting object <code>dict</code> has the following methods:
* [[#ngx.shared.DICT.lpop|lpop]]
* [[#ngx.shared.DICT.rpop|rpop]]
* [[#ngx.shared.DICT.llen|llen]]
* [[#ngx.shared.DICT.ttl|ttl]]
* [[#ngx.shared.DICT.expire|expire]]
* [[#ngx.shared.DICT.flush_all|flush_all]]
* [[#ngx.shared.DICT.flush_expired|flush_expired]]
* [[#ngx.shared.DICT.get_keys|get_keys]]
Expand Down Expand Up @@ -5491,6 +5493,74 @@ This feature was first introduced in the <code>v0.10.6</code> release.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.ttl ==
'''syntax:''' ''ttl, err = ngx.shared.DICT:ttl(key)''

'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''

'''requires:''' <code>resty.core.shdict</code> or <code>resty.core</code>

Retrieves the remaining TTL (time-to-live in seconds) of a key-value pair in the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]]. Returns the TTL as a number if the operation is successfully completed or <code>nil</code> and an error message otherwise.

If the key does not exist (or has already expired), this method will return <code>nil</code> and the error string <code>"not found"</code>.

The TTL is originally determined by the <code>exptime</code> argument of the [[#ngx.shared.DICT.set|set]], [[#ngx.shared.DICT.add|add]], [[#ngx.shared.DICT.replace|replace]] (and the likes) methods. It has a time resolution of <code>0.001</code> seconds. A value of <code>0</code> means that the item will never expire.

Example:

<geshi lang="lua">
require "resty.core"

local cats = ngx.shared.cats
local succ, err = cats:set("Marry", "a nice cat", 0.5)

ngx.sleep(0.2)

local ttl, err = cats:ttl("Marry")
ngx.say(ttl) -- 0.3
</geshi>

This feature was first introduced in the <code>v0.10.11</code> release.

'''Note:''' This method requires the <code>resty.core.shdict</code> or <code>resty.core</code> modules from the [https://github.com/openresty/lua-resty-core lua-resty-core] library.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.expire ==
'''syntax:''' ''success, err = ngx.shared.DICT:expire(key, exptime)''

'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''

'''requires:''' <code>resty.core.shdict</code> or <code>resty.core</code>

Updates the <code>exptime</code> (in second) of a key-value pair in the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]]. Returns a boolean indicating success if the operation completes or <code>nil</code> and an error message otherwise.

If the key does not exist, this method will return <code>nil</code> and the error string <code>"not found"</code>.

The <code>exptime</code> argument has a resolution of <code>0.001</code> seconds. If <code>exptime</code> is <code>0</code>, then the item will never expire.

Example:

<geshi lang="lua">
require "resty.core"

local cats = ngx.shared.cats
local succ, err = cats:set("Marry", "a nice cat", 0.1)

succ, err = cats:expire("Marry", 0.5)

ngx.sleep(0.2)

local val, err = cats:get("Marry")
ngx.say(val) -- "a nice cat"
</geshi>

This feature was first introduced in the <code>v0.10.11</code> release.

'''Note:''' This method requires the <code>resty.core.shdict</code> or <code>resty.core</code> modules from the [https://github.com/openresty/lua-resty-core lua-resty-core] library.

See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.flush_all ==
'''syntax:''' ''ngx.shared.DICT:flush_all()''

Expand Down