Atomic Increment/Decrement (LCC.kvdb.dict.incr)
Signature
value, meta_or_err = LCC.kvdb.dict.incr(name, key, by?, init?, min?, max?, coerce?, timeout?)
Parameters
- name
String. Dictionary name (non-empty, max256bytes). - key
String. Key to modify (must be valid UTF-8 and non-empty). - by
Integer, optional. Step size; defaults to1and can be negative. - init
Integer, optional. Initial value when the key is missing; defaults to0. - min
Integer, optional. Lower bound; results are clamped when below it. - max
Integer, optional. Upper bound; results are clamped when above it. - coerce
Boolean, optional. Whentrue, non-integer existing values are reset toinit; defaults tofalse. - timeout
Number, optional. Request timeout in seconds, default60.
Returns
- value
Number. Result after incrementing;nilon failure. - meta_or_err
Table or string. On success returns metadata:created: whether the key was created this timecoerced: whether coercion happenedclamped: whether the value was clampedclamp_reason:'min'or'max'when clampedoriginal_value: previous numeric value (when one existed)computed_value: result before clampingresult_value: final stored value
On failure contains the error message.
Notes
Provides a thread-safe atomic counter operation.
Usemin/maxto constrain the range andcoerceto reset invalid values back toinit.
Returnsnilwith an error string when the call fails.
Example
local value, meta = LCC.kvdb.dict.incr("counter", "retry", 1, 0, 0, 5, true)
if value then
LCC.log(1, "Retry count", value, meta.clamped and "Reached limit" or "")
end