Redis
There is basic Redis support in Nauthilus. Most of the time it should be enough to use simple Redis keys and string values as arguments. Type conversion can be done within Lua itself.
local nauthilus_redis = require("nauthilus_redis")
Redis custom pools
In the init script of the Nauthilus server, you can define custom pools. For each Redis command, the first parameter is either a string called "default" or a connection handle. The "default" will use Redis servers from the Nauthilus server itself.
How to register a new pool?
Register a custom pool
Syntax
local result, error = nauthilus_redis.register_redis_pool(name, mode, config)
Parameters
name(string) - Name of the new redis poolmode(string) - Defines the type of redis configuration, Possible values are:-
- standalone - Configure a simple standalone Redis instance
- cluster - Configure a Redis cluster
- sentinel - Configure a Redis sentinel
- sentinel_replica - Configure a failover client primaryly for read requests
config(table) - The configuration parameters for the new pool- address (string) - Address for the standalone system (master or replica)
- addresses (table) - List of addresses for clusters and sentinels
- master_name (string) - Master name is for sentinels
- sentinel_username (string) - Optional username for sentinels
- sentinel_password (string) - Optional password for sentinels
- username (string) - Optional username (used for standalone or sentinel setups)
- password (string) - Optional password (used for standalone or sentinel setups)
- db (number) - The database number in Redis
- pool_size (number) - Maximum number of connections in the pool
- min_idle_conns (number) - Minimum number of idle connections in the pool
- tls_enabled (boolean) - Activates TLS support
- tls_cert_file (string) - Optional path to a certificate file in PEM format
- tls_key_file (string) - Optional path to a key file in PEM format
Returns
result(string) - "OK" is returned, if the pool was configured successfully.error(string) - An error message, if an error occurs.
Example
local _, err_redis_reg = nauthilus_redis.register_redis_pool("my_custom_name", "sentinel", {
addresses = { "redis-sentinel-sentinel.ot-operators:26379" },
master_name = "myMaster",
password = "",
db = 3,
pool_size = 10,
min_idle_conns = 1,
tls_enabled = false
})
To get the handle of this pool, do the following in you Lua scripts:
local custom_pool, err_redis_client = nauthilus_redis.get_redis_connection("my_custom_name")
Now you can use custom_pool as the first argument to each Redis function.
You can define as many pools as you like. Currently supported is "standalone", "sentinel" and "cluster".
Documentation for the parameters is TODO.
In the following, I will use the name "handle" for a pool handler.
Functions
nauthilus_redis.redis_set
Set a key with rich options. Supports Redis SET flags: NX, XX, GET, KEEPTTL and expiration variants EX, PX, EXAT, PXAT.
Backward compatibility: Passing a plain number as the 4th argument still sets EX seconds.
Syntax
-- Preferred form: options table
local result, err = nauthilus_redis.redis_set(handle, key, value, {
nx = boolean,
xx = boolean,
get = boolean,
keepttl = boolean,
ex = number, -- seconds
px = number, -- milliseconds
exat = number, -- absolute unix time in seconds
pxat = number -- absolute unix time in milliseconds
})
-- Legacy form: expiration as seconds (equivalent to { ex = seconds })
local result, err = nauthilus_redis.redis_set(handle, key, value, expiration_seconds)
Parameters
handle(userdata|string): Redis connection handle or "default" for the default connectionkey(string): Key to setvalue(string): Value to storeoptions_or_expiration(table|number|nil): Either an options table (see above) or a number of seconds for EX
Mutual exclusivity and precedence rules follow Redis semantics:
- Only one of
ex,px,exat,pxatmay be used at a time. - Only one of
nxorxxmay be true.
Returns
result(string|nil): "OK" on success, or the previous value ifget = trueand the key existed (depending on Redis version);nilwhennx=trueand the key exists (orxx=trueand the key does not exist)err(string|nil): Error message if the command fails
Examples
Set with EX 60 using the legacy 4th arg:
local R = require("nauthilus_redis")
local ok, err = R.redis_set(handle, "my:key", "v", 60)
assert(not err)
Only set if not exists (NX) and keep existing TTL:
local ok, err = R.redis_set(handle, "my:key", "v", { nx = true, keepttl = true })
Replace only if exists (XX) and return old value (GET):
local old, err = R.redis_set(handle, "my:key", "new", { xx = true, get = true })
if old then
print("previous:", old)
end
Absolute expiration at a unix second timestamp:
local ok, err = R.redis_set(handle, "one-shot", "v", { exat = os.time() + 300 })
nauthilus_redis.redis_incr
Increments a numeric value stored in Redis by one.
Syntax
local number, error = nauthilus_redis.redis_incr(handle, key)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key whose value should be incremented
Returns
number(number): The new value after incrementingerror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local number, error = nauthilus_redis.redis_incr(handle, "key")
nauthilus_redis.redis_get
Retrieves a value from Redis by key.
Syntax
local result, error = nauthilus_redis.redis_get(handle, key)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key to retrieve the value for
Returns
result(string): The value stored at the specified keyerror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local result, error = nauthilus_redis.redis_get(handle, "key")
nauthilus_redis.redis_expire
Sets an expiration time (in seconds) for a Redis key.
Syntax
local result, error = nauthilus_redis.redis_expire(handle, key, seconds)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key to set expiration forseconds(number): The expiration time in seconds
Returns
result(string): "OK" if the operation was successfulerror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local result, error = nauthilus_redis.redis_expire(handle, "key", 3600)
nauthilus_redis.redis_del
Deletes a key from Redis.
Syntax
local result, error = nauthilus_redis.redis_del(handle, key)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key to delete
Returns
result(string): "OK" if the operation was successfulerror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local result, error = nauthilus_redis.redis_del(handle, "key")
nauthilus_redis.redis_rename
Renames a Redis key to a new name.
Syntax
local result, err = nauthilus_redis.redis_rename(handle, oldkey, newkey)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionoldkey(string): The current name of the Redis keynewkey(string): The new name for the Redis key
Returns
result(string): "OK" if the operation was successfulerr(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local oldkey = "abc"
local newkey = "def"
local result, err = nauthilus_redis.redis_rename(handle, oldkey, newkey)
nauthilus_redis.redis_hget
Retrieves a value for a specific field from a Redis hash map.
Syntax
local value, error = nauthilus_redis.redis_hget(handle, key, field)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key of the hash mapfield(string): The field name within the hash map
Returns
value(string): The value of the specified fielderror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local redis_key = "some_key"
local already_sent_mail, err_redis_hget = nauthilus_redis.redis_hget(handle, redis_key, "send_mail")
nauthilus_redis.redis_hset
Sets a field value in a Redis hash map.
Syntax
local result, error = nauthilus_redis.redis_hset(handle, key, field, value)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key of the hash mapfield(string): The field name within the hash mapvalue(string/number): The value to set for the field
Returns
result(number): 1 if field is a new field in the hash and value was set, 0 if field already exists and the value was updatederror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local redis_key = "some_key"
local result, err_redis_hset = nauthilus_redis.redis_hset(handle, redis_key, "send_mail", 1)
nauthilus_redis.redis_hmget (since 1.11.4)
Fetch multiple fields from a hash in a single call. Returns a Lua table mapping field -> value.
Missing fields are present with value nil.
Syntax
local values, err = nauthilus_redis.redis_hmget(handle, key, field1, field2, ...)
Parameters
handle(userdata|string): Redis connection handle or "default"key(string): Hash keyfield1, field2, ...(string): One or more field names
Returns
values(table): Lua table, e.g.{ foo = "1", bar = nil, baz = "x" }err(string|nil): Error message if the operation fails
Example
local R = require("nauthilus_redis")
local vals, err = R.redis_hmget(handle, "user:42", "name", "email", "age")
assert(not err)
if vals.email then
print("email:", vals.email)
end
nauthilus_redis.redis_hdel
Deletes a field from a Redis hash map.
Syntax
local deleted, error = nauthilus_redis.redis_hdel(handle, key, field)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key of the hash mapfield(string): The field name to delete from the hash map
Returns
deleted(number): The number of fields that were removed (1 if successful, 0 if field did not exist)error(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local result = {}
result.dovecot_session = "123"
local redis_key = "some_key"
local deleted, err_redis_hdel = nauthilus_redis.redis_hdel(handle, redis_key, result.dovecot_session)
nauthilus_redis.redis_hlen
Gets the number of fields in a Redis hash map.
Syntax
local length, error = nauthilus_redis.redis_hlen(handle, key)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key of the hash map
Returns
length(number): The number of fields in the hash maperror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local redis_key = "some_key"
local length, err_redis_hlen = nauthilus_redis.redis_hlen(handle, redis_key)
nauthilus_redis.redis_hgetall
Retrieves all fields and values from a Redis hash map.
Syntax
local hash_table, error = nauthilus_redis.redis_hgetall(handle, key)
Parameters
handle(userdata/string): Redis connection handle or "default" for the default connectionkey(string): The Redis key of the hash map
Returns
hash_table(table): A Lua table containing all field-value pairs from the hash maperror(string): An error message if the operation fails
Example
local nauthilus_redis = require("nauthilus_redis")
local redis_key = "some_key"
local all_sessions, err_redis_hgetall = nauthilus_redis.redis_hgetall(handle, redis_key)