Skip to content

Commit b9bd802

Browse files
doujiang24agentzh
authored andcommitted
feature: shdict:incr(): added the optional "init" argument to allow intializing nonexistent keys with an initial value.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent dbb48e3 commit b9bd802

File tree

5 files changed

+519
-22
lines changed

5 files changed

+519
-22
lines changed

README.markdown

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6269,19 +6269,30 @@ See also [ngx.shared.DICT](#ngxshareddict).
62696269

62706270
ngx.shared.DICT.incr
62716271
--------------------
6272-
**syntax:** *newval, err = ngx.shared.DICT:incr(key, value)*
6272+
**syntax:** *newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?)*
62736273

62746274
**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;*
62756275

62766276
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.
62776277

6278-
The key must already exist in the dictionary, otherwise it will return `nil` and `"not found"`.
6278+
When the key does not exist or has already expired in the shared dictionary,
6279+
6280+
1. if the `init` argument is not specified or takes the value `nil`, this method will return `nil` and the error string `"not found"`, or
6281+
1. if the `init` argument takes a number value, this method will create a new `key` with the value `init + value`.
6282+
6283+
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.
6284+
6285+
The `forcible` return value will always be `nil` when the `init` argument is not specified.
6286+
6287+
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`.
62796288

62806289
If the original value is not a valid Lua number in the dictionary, it will return `nil` and `"not a number"`.
62816290

6282-
The `value` argument can be any valid Lua numbers, like negative numbers or floating-point numbers.
6291+
The `value` argument and `init` argument can be any valid Lua numbers, like negative numbers or floating-point numbers.
62836292

6284-
This feature was first introduced in the `v0.3.1rc22` release.
6293+
This method was first introduced in the `v0.3.1rc22` release.
6294+
6295+
The optional `init` parameter was first added in the `v0.10.6` release.
62856296

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

doc/HttpLuaModule.wiki

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5254,19 +5254,30 @@ This feature was first introduced in the <code>v0.3.1rc22</code> release.
52545254
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
52555255
52565256
== ngx.shared.DICT.incr ==
5257-
'''syntax:''' ''newval, err = ngx.shared.DICT:incr(key, value)''
5257+
'''syntax:''' ''newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?)''
52585258
52595259
'''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*''
52605260
52615261
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.
52625262
5263-
The key must already exist in the dictionary, otherwise it will return <code>nil</code> and <code>"not found"</code>.
5263+
When the key does not exist or has already expired in the shared dictionary,
5264+
5265+
# if the <code>init</code> argument is not specified or takes the value <code>nil</code>, this method will return <code>nil</code> and the error string <code>"not found"</code>, or
5266+
# if the <code>init</code> argument takes a number value, this method will create a new <code>key</code> with the value <code>init + value</code>.
5267+
5268+
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.
5269+
5270+
The <code>forcible</code> return value will always be <code>nil</code> when the <code>init</code> argument is not specified.
5271+
5272+
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>.
52645273
52655274
If the original value is not a valid Lua number in the dictionary, it will return <code>nil</code> and <code>"not a number"</code>.
52665275
5267-
The <code>value</code> argument can be any valid Lua numbers, like negative numbers or floating-point numbers.
5276+
The <code>value</code> argument and <code>init</code> argument can be any valid Lua numbers, like negative numbers or floating-point numbers.
52685277
5269-
This feature was first introduced in the <code>v0.3.1rc22</code> release.
5278+
This method was first introduced in the <code>v0.3.1rc22</code> release.
5279+
5280+
The optional `init` parameter was first added in the <code>v0.10.6</code> release.
52705281
52715282
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
52725283

0 commit comments

Comments
 (0)