You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
6126
+
6127
+
The `old_value` argument can be `nil` only when `old_flags` argument is specified, in which case only `flags` will be checked.
6128
+
6129
+
If `old_flags` argument is not specified, only `value` will be checked.
6130
+
6131
+
The optional `old_flags` argument can be `nil`, and it means `0`.
6132
+
6133
+
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.
6134
+
6135
+
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:
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]].
5133
+
5134
+
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.
5135
+
5136
+
If <code>old_flags</code> argument is not specified, only <code>value</code> will be checked.
5137
+
5138
+
The optional <code>old_flags</code> argument can be <code>nil</code>, and it means <code>0</code>.
5139
+
5140
+
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.
5141
+
5142
+
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:
5143
+
5144
+
<geshi lang="lua">
5145
+
local cats = ngx.shared.cats
5146
+
cats:set("foo", 1, 1)
5147
+
5148
+
local old_value, old_flags = cats:get("foo")
5149
+
5150
+
while true do
5151
+
local newvalue = calculate(old_value) -- some logic
5152
+
local newflags = (old_flags or 0) + 1
5153
+
5154
+
local success, err, forcibly, current_value, current_flags
0 commit comments