Skip to content

ngx.shared.DICT.incr optional expire argument #358

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
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a1df068
ngx.shared.DICT.incr optional expire argument
splitice Apr 5, 2014
a315d79
tabs fixed, replaced with spaces
splitice Apr 5, 2014
8b5d786
Merge branch 'upstream/master'
splitice Mar 31, 2015
c11df99
Update code to meet nginx coding standards
splitice Apr 1, 2015
b4c12d6
Documentation updated to include incr's exptime parameter
splitice Apr 1, 2015
7626688
Fixed stray tab
splitice Apr 2, 2015
3caef80
nil handling
splitice Apr 2, 2015
c84f1c9
Merge branch 'openresty/master'
splitice Apr 8, 2015
248afcf
Merge branch 'openresty/master'
splitice May 8, 2015
aaf49e6
Merge remote-tracking branch 'openresty/master'
splitice Sep 13, 2015
e4f79de
fix commited conflict
splitice Sep 13, 2015
8ecf938
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Nov 21, 2015
ba156b9
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Dec 17, 2015
0fea525
add exptime argument to ffi method with the same semantics as :incr i…
splitice Dec 28, 2015
16427d0
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Dec 28, 2015
4e618cf
add tp local variable was undefined
splitice Dec 28, 2015
81907d7
feature: add a new api ngx.shared.DICT.cas
doujiang24 Oct 14, 2015
632f95f
doc: fix confusing description
doujiang24 Feb 3, 2016
d9393be
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Feb 4, 2016
96eaf4a
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Feb 18, 2016
eb88b62
Merge pull request #1 from doujiang24/shdict_cas
splitice May 11, 2016
ddeea25
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice May 12, 2016
efd0717
add user_flags to incr return value
splitice May 12, 2016
bae63ad
user flags not defined
splitice May 23, 2016
83be5d4
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice May 23, 2016
ebd0d57
add see other to ngx.redirect
splitice May 23, 2016
4cc4a08
fix previous commit
splitice May 23, 2016
c37cedc
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Jul 5, 2016
d47fb20
Merge remote-tracking branch 'refs/remotes/openresty/master'
splitice Sep 23, 2016
e3b2c09
fix incr init bug
splitice Dec 15, 2016
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
58 changes: 58 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3055,6 +3055,7 @@ Nginx API for Lua
* [ngx.shared.DICT.add](#ngxshareddictadd)
* [ngx.shared.DICT.safe_add](#ngxshareddictsafe_add)
* [ngx.shared.DICT.replace](#ngxshareddictreplace)
* [ngx.shared.DICT.cas](#ngxshareddictcas)
* [ngx.shared.DICT.delete](#ngxshareddictdelete)
* [ngx.shared.DICT.incr](#ngxshareddictincr)
* [ngx.shared.DICT.lpush](#ngxshareddictlpush)
Expand Down Expand Up @@ -6054,6 +6055,7 @@ The resulting object `dict` has the following methods:
* [add](#ngxshareddictadd)
* [safe_add](#ngxshareddictsafe_add)
* [replace](#ngxshareddictreplace)
* [cas](#ngxshareddictcas)
* [delete](#ngxshareddictdelete)
* [incr](#ngxshareddictincr)
* [lpush](#ngxshareddictlpush)
Expand Down Expand Up @@ -6277,6 +6279,62 @@ See also [ngx.shared.DICT](#ngxshareddict).

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

ngx.shared.DICT.cas
-------------------
**syntax:** *success, err, forcible, current_value?, current_flags? = ngx.shared.DICT:cas(key, value, exptime, flags, old_value, old_flags?)*

**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*, certificate_by_lua**

Just like the [replace](#ngxshareddictreplace) method, but only stores the key-value pair into the dictionary [ngx.shared.DICT](#ngxshareddict) if and only if the `old_value` argument and `old_flags` argument *do* match the value and flags in the dictionary [ngx.shared.DICT](#ngxshareddict).

The `old_value` argument can be `nil` only when `old_flags` argument is specified, in which case only `flags` will be checked.

If `old_flags` argument is not specified, only `value` will be checked.

The optional `old_flags` argument can be `nil`, and it means `0`.

If they do *not* match, the `success` return value will be `false` and the `err` return value will be `"not matched"`. The `current_value` return value and `current_flags` return value will be the current `value` and current `flags` in the dictionary [ngx.shared.DICT](#ngxshareddict), just like [get](#ngxshareddictget) does.

This function is often used to avoid race condition between [get](#ngxshareddictget) and [set](#ngxshareddictset) across multipe nginx worker processes, and below is an example:

```lua

local cats = ngx.shared.cats
cats:set("foo", 1, 1)

local old_value, old_flags = cats:get("foo")

while true do
local newvalue = calculate(old_value) -- some logic
local newflags = (old_flags or 0) + 1

local success, err, forcibly, current_value, current_flags
= cats:cas("foo", newvalue, 0, newflags, old_value, old_flags)

if success then
break

elseif err == "not matched" then
old_value = current_value
old_flags = current_flags

elseif err == "not found" then
-- add or some other handle
cats:add("foo", newvalue, 0, newflags)
break

else
-- "no memory" or some other error
-- just log or some other handle
break
end
end
```

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

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

ngx.shared.DICT.delete
----------------------
**syntax:** *ngx.shared.DICT:delete(key)*
Expand Down
55 changes: 54 additions & 1 deletion doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -5067,6 +5067,7 @@ The resulting object <code>dict</code> has the following methods:
* [[#ngx.shared.DICT.add|add]]
* [[#ngx.shared.DICT.safe_add|safe_add]]
* [[#ngx.shared.DICT.replace|replace]]
* [[#ngx.shared.DICT.cas|cas]]
* [[#ngx.shared.DICT.delete|delete]]
* [[#ngx.shared.DICT.incr|incr]]
* [[#ngx.shared.DICT.lpush|lpush]]
Expand Down Expand Up @@ -5261,6 +5262,58 @@ This feature was first introduced in the <code>v0.3.1rc22</code> release.

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

== ngx.shared.DICT.cas ==
'''syntax:''' ''success, err, forcible, current_value?, current_flags? = ngx.shared.DICT:cas(key, value, exptime, flags, old_value, old_flags?)''

'''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*, certificate_by_lua*''

Just like the [[#ngx.shared.DICT.replace|replace]] method, but only stores the key-value pair into the dictionary [[#ngx.shared.DICT|ngx.shared.DICT]] if and only if the <code>old_value</code> argument and <code>old_flags</code> argument ''do'' match the value and flags in the dictionary [[#ngx.shared.DICT|ngx.shared.DICT]].

The <code>old_value</code> argument can be <code>nil</code> only when <code>old_flags</code> argument is specified, in which case only <code>flags</code> will be checked.

If <code>old_flags</code> argument is not specified, only <code>value</code> will be checked.

The optional <code>old_flags</code> argument can be <code>nil</code>, and it means <code>0</code>.

If they do ''not'' match, the <code>success</code> return value will be <code>false</code> and the <code>err</code> return value will be <code>"not matched"</code>. The <code>current_value</code> return value and <code>current_flags</code> return value will be the current <code>value</code> and current <code>flags</code> in the dictionary [[#ngx.shared.DICT|ngx.shared.DICT]], just like [[#ngx.shared.DICT.get|get]] does.

This function is often used to avoid race condition between [[#ngx.shared.DICT.get|get]] and [[#ngx.shared.DICT.set|set]] across multipe nginx worker processes, and below is an example:

<geshi lang="lua">
local cats = ngx.shared.cats
cats:set("foo", 1, 1)

local old_value, old_flags = cats:get("foo")

while true do
local newvalue = calculate(old_value) -- some logic
local newflags = (old_flags or 0) + 1

local success, err, forcibly, current_value, current_flags
= cats:cas("foo", newvalue, 0, newflags, old_value, old_flags)

if success then
break

elseif err == "not matched" then
old_value = current_value
old_flags = current_flags

elseif err == "not found" then
-- add or some other handle
cats:add("foo", newvalue, 0, newflags)
break

else
-- "no memory" or some other error
-- just log or some other handle
break
end
end
</geshi>

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

== ngx.shared.DICT.delete ==
'''syntax:''' ''ngx.shared.DICT:delete(key)''

Expand All @@ -5275,7 +5328,7 @@ This feature was first introduced in the <code>v0.3.1rc22</code> release.
See also [[#ngx.shared.DICT|ngx.shared.DICT]].

== ngx.shared.DICT.incr ==
'''syntax:''' ''newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?)''
'''syntax:''' ''newval, err, forcible? = ngx.shared.DICT:incr(key, value, init?, 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*''

Expand Down
6 changes: 4 additions & 2 deletions src/ngx_http_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ ngx_http_lua_ngx_redirect(lua_State *L)

if (rc != NGX_HTTP_MOVED_TEMPORARILY
&& rc != NGX_HTTP_MOVED_PERMANENTLY
&& rc != NGX_HTTP_TEMPORARY_REDIRECT)
&& rc != NGX_HTTP_TEMPORARY_REDIRECT
&& rc != NGX_HTTP_SEE_OTHER)
{
return luaL_error(L, "only ngx.HTTP_MOVED_TEMPORARILY, "
"ngx.HTTP_MOVED_PERMANENTLY, and "
"ngx.HTTP_TEMPORARY_REDIRECT are allowed");
"ngx.HTTP_TEMPORARY_REDIRECT, and"
"ngx.HTTP_SEE_OTHER are allowed");
}

} else {
Expand Down
Loading