nginx shared dictionary item API (Operation Method)

nginx shared memory, called shared dictionary entries for all the worker processes are visible, is a global variable.

Remarks about the contents of the [] is remark.

Source Analysis Document: https: //www.codercto.com/a/9488.html

git document: https: //github.com/openresty/lua-nginx-module#ngxshareddictget

Set global dictionary entry, block arranged at http:

lua_shared_dict cats [dictionary item name] 12M [memory size];

 

1、 ngx.shared.DICT.get

语法:value, flags = ngx.shared.DICT:get(key)

Get the value of the key corresponding to the shared memory. If the key does not exist, or the key has expired, it will return nil; if an error occurs, it returns nil and an error message.

local cats = ngx.shared.cats local value, flags = cats.get(cats, "Marry")

Equivalent to

local cats = ngx.shared.cats local value, flags = cats:get("Marry")

Flags in the returned list, the value is set in ngx.shared.DICT.set method, the default value is 0. If the flags are set to 0, then the value of the flags here will not be returned.

 

2、 ngx.shared.DICT.get_stale

Syntax: value, flags, stale = ngx.shared.DICT: similar get_stale (key) and get method, except that the method will also return for the expired key, the third key return parameter indicates whether the value returned has expired, true to expire, false representation has not expired.

 

3、 ngx.shared.DICT.set

语法:success, err, forcible = ngx.shared.DICT:set(key, value, exptime?, flags?)

Unconditional "to insert into the shared memory key-value pairs, where the talk" means the value of the three return condition "refers to whether the same key already exists on the shared memory to be inserted regardless of:

success: success insert true, the failure to insert false

err: Operation error message when you fail, it may be similar to the "no memory"

forcible: true indicates the need to achieve by forcefully removing the insert (LRU algorithm) shared by other dictionary entries on memory, false indicating that no delete dictionary entries on the shared memory to achieve insertion.

The third parameter indicates that key exptime the expiration time, in seconds (s), the default value is 0, indicating that never expire; flags parameter is a user flag value, will also get get when calling the get method.

local cats = ngx.shared.cats
local succ, err, forcible = cats.set(cats, "Marry", "it is a nice cat!")
 
等价于
 
local cats = ngx.shared.cats
local succ, err, forcible = cats:set("Marry", "it is a nice cat!")

4、 ngx.shared.DICT.safe_set

语法:ok, err = ngx.shared.DICT:safe_set(key, value, exptime?, flags?)

Similar set method, except that in the case where the shared memory is not exhausted, by forcefully removing (LRU algorithm) implemented method of insertion. If memory will be returned directly nil and err information "no memory"

note:

set and safe_set in common is this: If the key to be inserted already exists, then the key corresponding to the original value of the new value will be covered!

5、  ngx.shared.DICT.add

语法:success, err, forcible = ngx.shared.DICT:add(key, value, exptime?, flags?)

Similar to the set method, except that the method is not set and insert duplicate key (simply add method is considered a method of sub-set method), if the key to be inserted already exists, and returns nil and err = "exists"

6、  ngx.shared.DICT.safe_add

语法:ok, err = ngx.shared.DICT:safe_add(key, value, exptime?, flags?)

Safe_set similar methods, except that the duplicate key is not inserted (safe_add method is simply considered a sub-method safe_set method), if the key to be inserted already exists, and returns nil and err = "exists"

7、 ngx.shared.DICT.replace

语法:success, err, forcible = ngx.shared.DICT:replace(key, value, exptime?, flags?)

Similar set method, except that the only existing key operation (can be considered to replace simple method is a method of sub-set method), to be inserted if the key does not exist in the dictionary, it will return nil and the error message "not found "

8, ngx.shared.DICT.delete

  Syntax: ngx.shared.DICT: the Delete (Key)

  Unconditional delete the specified key-value pairs, which is equivalent to

  ngx.shared.DICT: set (key, callers)

 

9,   ngx.shared.DICT.incr

  语法:newval, err = ngx.shared.DICT:incr(key, value)

  Of the incremental value corresponding key operation, the increment value is a value, which value may be the value of a positive number, 0, or may be a negative number. Lua value must be a type in the type of number, otherwise it will return nil and "not a number"; key must already exist in a shared memory key, or it will return nil and "not found".

 
10、  ngx.shared.DICT.flush_all

  Syntax: ngx.shared.DICT: flush_all ()

  Clear all fields on the dictionary, but not really freed memory occupied by the fields, and each field will only be marked as expired.

 

11、 ngx.shared.DICT.flush_expired

  语法:flushed = ngx.shared.DICT:flush_expired(max_count?)

  Clear expired on dictionary field, max_count indicate the upper limit, or 0 if not given the need to clear all expired field, the return value is the number of actual deletion flushed out of date fields.

  note:

  Flush_all method differs in that it will release out of date fields occupied by memory.

12、 ngx.shared.DICT.get_keys  

  语法:keys = ngx.shared.DICT:get_keys(max_count?)

  Get a list of the fields from the dictionary, the number of MAX_COUNT, or 0 if it is not given, indicates that the number is not limited. The default value is 1024

  note:

  It is strongly recommended when you call this method, specify a max_count argument, because in a large number of keys, if you do not specify the value of max_count may result in dictionary is locked, blocking attempts to access the dictionary worker process.

13、  ngx.shared.DICT.flush_keys

  Syntax: ngx.shared.DICT: flush_keys (num, time)

  Get key from the dictionary entry number is num, time is time effective. In order to ensure the specified num process data will not be explode memory.

Guess you like

Origin www.cnblogs.com/chenpython123/p/10758442.html