Module eco.shared
Shared-memory dictionary.
This module uses mmap(MAP_SHARED) on files under /dev/shm to provide
cross-process shared memory semantics.
Functions
| new (name, size) | Create a new shared-memory dictionary. |
| get (name) | Open an existing shared-memory dictionary. |
Class dict
| dict:del (key) | Delete a key. |
| dict:set (key, value[, exptime]) | Set key to value. |
| dict:get (key) | Get value by key. |
| dict:incr (key, value[, exptime]) | Increment numeric value. |
| dict:ttl (key) | Get remaining TTL in seconds. |
| dict:expire (key, exptime) | Update key expiration. |
| dict:flush_all () | Flushes out all the items in the dictionary. |
| dict:get_keys () | Get all keys in the dictionary. |
| dict:close () | Close the dictionary and release associated resources. |
Functions
- new (name, size)
-
Create a new shared-memory dictionary.
The caller becomes the owner of the shared-memory file. When the owner closes this dict (or it is garbage-collected), the file is removed.
Parameters:
- name string Dictionary name.
- size integer Size of the dictionary.
Returns:
Or
- nil
- string err
- get (name)
-
Open an existing shared-memory dictionary.
The returned dict is a non-owner handle and close will not remove the shared-memory file.
Parameters:
- name string Dictionary name.
Returns:
Or
- nil
- string err
Class dict
Keys are strings. Values can be booleans, numbers, or strings. Expiration time is stored per key and measured in seconds in the Lua API.
- dict:del (key)
-
Delete a key.
Parameters:
- key string
Returns:
-
boolean
removed
trueif the key existed and was deleted. - dict:set (key, value[, exptime])
-
Set key to value.
When
exptimeis positive, the key expires afterexptimeseconds. Ifexptimeis0or negative, the key is stored without expiration.Parameters:
Returns:
-
boolean
ok
Or
- nil
- string err
- dict:get (key)
-
Get value by key.
Returns value if present; otherwise returns
nil.Parameters:
- key string
Returns:
-
any
value
- dict:incr (key, value[, exptime])
-
Increment numeric value.
The key must already exist and hold a number. If
exptimeis provided, it replaces the key TTL. Ifexptimeis omitted, the previous TTL is preserved.Parameters:
- key string
- value number Delta.
- exptime number Expiration in seconds. (optional)
Returns:
-
number
new_value
Or
- nil
- string err
- dict:ttl (key)
-
Get remaining TTL in seconds.
Returns
nilif the key does not exist. Returns0when the key exists but has no expiration.Parameters:
- key string
Returns:
-
number or nil
ttl
- dict:expire (key, exptime)
-
Update key expiration.
When
exptimeis positive, the key expires afterexptimeseconds. Whenexptimeis0or negative, expiration is cleared.Parameters:
- key string
- exptime number Expiration in seconds.
Returns:
-
boolean or nil
ok
trueon success,nilif key does not exist. - dict:flush_all ()
-
Flushes out all the items in the dictionary.
Returns:
-
nil
- dict:get_keys ()
-
Get all keys in the dictionary.
Returns:
-
table
keys
- dict:close ()
-
Close the dictionary and release associated resources.
This is idempotent and is also invoked by
__gcand__close.For dictionaries created by new, closing also removes the backing shared-memory file. Existing processes that already opened the dictionary may continue to access it, but future get calls by name fail.
Returns:
-
nil