Storage
The storage library implements an interface to persistent memory.
There are two types of storage: private and shared. Private storage stores variables between runs of a single rule, it is not accessible by other rules. Shared storage allows variables to be shared between rules.
storage.write
-- @param key string
-- @param value string|number|table|nil
-- @return string|nil
function storage.write(key, value)
end
Writes the string, number, boolean or table value to the private persistent storage by key. Later the value can be accessed by the rule using storage.read by the same key. Writing nil is the same as calling storage.remove for the given key. On failure, it returns an error message string.
Example
local err = storage.write("string", "1")
if err ~= nil then
enapter.log("Failed to write value to storage: "..err)
end
local err = storage.write("table", { value = 1 })
if err ~= nil then
enapter.log("Failed to write value to storage: "..err)
end
storage.read
-- @param key string
-- @return string|number|table|nil, string|nil
function storage.read(key)
end
Reads the value from private persistent storage by the given key. Returns data as string or nil if the value is not set in the storage. On failure, it returns nil and an error message string.
Example
local value, err = storage.namespaced.read("string")
if err ~= nil then
enapter.log("Failed to read value from storage: "..err)
end
enapter.log("string: "..value) -- string: 1
local value, err = storage.namespaced.read("table")
if err ~= nil then
enapter.log("Failed to read value from storage: "..err)
end
enapter.log("table: "..inspect(value)) -- table: { value = 1 }
storage.remove
-- @param key string
-- @return string|nil
function storage.remove(key)
end
Removes data from the private persistent storage by the given key. On failure, it returns an error message string.
storage.shared.write
-- @param key string
-- @param value string|number|table|nil
-- @return string|nil
function storage.shared.write(key, value)
end
Writes the string, number, boolean or table value to the shared persistent storage by key. Later the value can be accessed by other rules using storage.shared.read by the same key. Writing nil is the same as calling storage.shared.remove for the given key. On failure, it returns an error message string.
storage.shared.read
-- @param key string
-- @return string|number|table|nil, string|nil
function storage.shared.read(key)
end
Reads the value from shared persistent storage by the given key. Returns data as string or nil if the value is not set in the storage. On failure, it returns nil and an error message string.
storage.shared.remove
-- @param key string
-- @return string|nil
function storage.shared.remove(key)
end
Removes data from the shared persistent storage by the given key. On failure, it returns an error message string.