Skip to content

Commit 90d7522

Browse files
thibaultchaagentzh
authored andcommitted
doc: added the new 'init_ttl' argument to the shdict:incr() documentation.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent a46862a commit 90d7522

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

README.markdown

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6451,10 +6451,12 @@ See also [ngx.shared.DICT](#ngxshareddict).
64516451

64526452
ngx.shared.DICT.incr
64536453
--------------------
6454-
**syntax:** *newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?)*
6454+
**syntax:** *newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?, init_ttl?)*
64556455

64566456
**context:** *init_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
64576457

6458+
**optional requirement:** `resty.core.shdict` or `resty.core`
6459+
64586460
Increments the (numerical) value for `key` in the shm-based dictionary [ngx.shared.DICT](#ngxshareddict) by the step value `value`. Returns the new resulting number if the operation is successfully completed or `nil` and an error message otherwise.
64596461

64606462
When the key does not exist or has already expired in the shared dictionary,
@@ -6464,6 +6466,25 @@ When the key does not exist or has already expired in the shared dictionary,
64646466

64656467
Like the [add](#ngxshareddictadd) method, it also overrides the (least recently used) unexpired items in the store when running out of storage in the shared memory zone.
64666468

6469+
The optional `init_ttl` argument specifies expiration time (in seconds) of the value when it is initialized via the `init` argument. The time resolution is `0.001` seconds. If `init_ttl` takes the value `0` (which is the default), then the item will never expire. This argument cannot be provided without providing the `init` argument as well, and has no effect if the value already exists (e.g., if it was previously inserted via [set](#ngxshareddictset) or the likes).
6470+
6471+
**Note:** Usage of the `init_ttl` argument requires the `resty.core.shdict` or `resty.core` modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library. Example:
6472+
6473+
```lua
6474+
6475+
require "resty.core"
6476+
6477+
local cats = ngx.shared.cats
6478+
local newval, err = cats:incr("black_cats", 1, 0, 0.1)
6479+
6480+
print(newval) -- 1
6481+
6482+
ngx.sleep(0.2)
6483+
6484+
local val, err = cats:get("black_cats")
6485+
print(val) -- nil
6486+
```
6487+
64676488
The `forcible` return value will always be `nil` when the `init` argument is not specified.
64686489

64696490
If this method succeeds in storing the current item by forcibly removing other not-yet-expired items in the dictionary via LRU, the `forcible` return value will be `true`. If it stores the item without forcibly removing other valid items, then the return value `forcible` will be `false`.
@@ -6476,6 +6497,8 @@ This method was first introduced in the `v0.3.1rc22` release.
64766497

64776498
The optional `init` parameter was first added in the `v0.10.6` release.
64786499

6500+
The optional `init_ttl` parameter was introduced in the `v0.10.12rc2` release.
6501+
64796502
See also [ngx.shared.DICT](#ngxshareddict).
64806503

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

doc/HttpLuaModule.wiki

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5412,10 +5412,12 @@ This feature was first introduced in the <code>v0.3.1rc22</code> release.
54125412
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
54135413
54145414
== ngx.shared.DICT.incr ==
5415-
'''syntax:''' ''newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?)''
5415+
'''syntax:''' ''newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?, init_ttl?)''
54165416
54175417
'''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*''
54185418
5419+
'''optional requirement:''' <code>resty.core.shdict</code> or <code>resty.core</code>
5420+
54195421
Increments the (numerical) value for <code>key</code> in the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]] by the step value <code>value</code>. Returns the new resulting number if the operation is successfully completed or <code>nil</code> and an error message otherwise.
54205422
54215423
When the key does not exist or has already expired in the shared dictionary,
@@ -5425,6 +5427,24 @@ When the key does not exist or has already expired in the shared dictionary,
54255427
54265428
Like the [[#ngx.shared.DICT.add|add]] method, it also overrides the (least recently used) unexpired items in the store when running out of storage in the shared memory zone.
54275429
5430+
The optional <code>init_ttl</code> argument specifies expiration time (in seconds) of the value when it is initialized via the <code>init</code> argument. The time resolution is <code>0.001</code> seconds. If <code>init_ttl</code> takes the value <code>0</code> (which is the default), then the item will never expire. This argument cannot be provided without providing the <code>init</code> argument as well, and has no effect if the value already exists (e.g., if it was previously inserted via [[#ngx.shared.DICT.set|set]] or the likes).
5431+
5432+
'''Note:''' Usage of the <code>init_ttl</code> argument 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. Example:
5433+
5434+
<geshi lang="lua">
5435+
require "resty.core"
5436+
5437+
local cats = ngx.shared.cats
5438+
local newval, err = cats:incr("black_cats", 1, 0, 0.1)
5439+
5440+
print(newval) -- 1
5441+
5442+
ngx.sleep(0.2)
5443+
5444+
local val, err = cats:get("black_cats")
5445+
print(val) -- nil
5446+
</geshi>
5447+
54285448
The <code>forcible</code> return value will always be <code>nil</code> when the <code>init</code> argument is not specified.
54295449
54305450
If this method succeeds in storing the current item by forcibly removing other not-yet-expired items in the dictionary via LRU, the <code>forcible</code> return value will be <code>true</code>. If it stores the item without forcibly removing other valid items, then the return value <code>forcible</code> will be <code>false</code>.
@@ -5437,6 +5457,8 @@ This method was first introduced in the <code>v0.3.1rc22</code> release.
54375457
54385458
The optional <code>init</code> parameter was first added in the <code>v0.10.6</code> release.
54395459
5460+
The optional <code>init_ttl</code> parameter was introduced in the <code>v0.10.12rc2</code> release.
5461+
54405462
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
54415463
54425464
== ngx.shared.DICT.lpush ==

0 commit comments

Comments
 (0)