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:

    dict

Or

  1. nil
  2. 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:

    dict

Or

  1. nil
  2. string err

Class dict

Dictionary object created by shared.new or opened by shared.get.

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:

Returns:

    boolean removed true if the key existed and was deleted.
dict:set (key, value[, exptime])
Set key to value.

When exptime is positive, the key expires after exptime seconds. If exptime is 0 or negative, the key is stored without expiration.

Parameters:

  • key string
  • value string, number or boolean
  • exptime number Expiration in seconds. (optional)

Returns:

    boolean ok

Or

  1. nil
  2. string err
dict:get (key)
Get value by key.

Returns value if present; otherwise returns nil.

Parameters:

Returns:

    any value
dict:incr (key, value[, exptime])
Increment numeric value.

The key must already exist and hold a number. If exptime is provided, it replaces the key TTL. If exptime is omitted, the previous TTL is preserved.

Parameters:

  • key string
  • value number Delta.
  • exptime number Expiration in seconds. (optional)

Returns:

    number new_value

Or

  1. nil
  2. string err
dict:ttl (key)
Get remaining TTL in seconds.

Returns nil if the key does not exist. Returns 0 when the key exists but has no expiration.

Parameters:

Returns:

    number or nil ttl
dict:expire (key, exptime)
Update key expiration.

When exptime is positive, the key expires after exptime seconds. When exptime is 0 or negative, expiration is cleared.

Parameters:

  • key string
  • exptime number Expiration in seconds.

Returns:

    boolean or nil ok true on success, nil if 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 __gc and __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
generated by LDoc 1.5.0 Last updated 2026-04-09 14:48:22