Skip to content

redis.open { PARAMS }

Opens a connection to a Redis data store and returns a connection handle.

local redis = require 'redis'

-- Open a connection and increment a counter, returning its new value
local conn = redis.open { node = 'redis://127.0.0.1/' }
print(conn:query('incr', 'test-count'))

PARAMS is a lua table with the following keys:

  • node - the redis URL string identifying the server. Can be a table listing multiple servers if you have a redis cluster deployed.

  • pool_size - optional integer. Specifies the maximum number of spare connections to be maintained by the connection pool. The default is 10.

  • read_from_replicas - optional boolean. If true, when connecting to a redis cluster, reads are issued to replicas while writes are issued to the primary data stores.

  • username - optional string. convenience for working with the cluster, so that you only need to specify the username once. This is not used for connecting to a single node.

  • password - optional string. convenience for working with the cluster, so that you only need to specify the password once. This is not used for connecting to a single node.

  • connect_timeout - optional duration string. Specify how long to keep attempting to connect to redis. The default is 30 seconds. (Since: Version 2024.06.10-84e84b89)

  • wait_timeout - optional duration string. Specify how long to wait for an available connection. If the pool is at capacity (pool_size), then an open call will block for up this duration for a connection to be released and made to connect to redis. The default is 30 seconds. (Since: Version 2024.09.02-c5476b89)

  • response_timeout - optional duration string. Specify how long to wait for the response to a query. (Since: Version 2024.09.02-c5476b89)

  • recycle_timeout - optional duration string. Specify how long to wait for the liveness check when testing whether a connection can be resused. (Since: Version 2024.09.02-c5476b89)

  • cluster - optional boolean. Set this to true to explicit use a cluster-enabled client. The default is false if the number of nodes is one, or true if the number of nodes is greater than one. You generally don't need to set this, but can do so to force a cluster connection when you have only a single node address. (Since: Version 2024.09.02-c5476b89)

The returned connection handle has a single "query" method:

conn:query(CMD, [ARGS])

Issue a redis command and return the result.

See Redis Commands for a list of commands.

The redis INCRBY command increments a key by a value; it has the syntax:

INCRBY key increment

To use INCRBY to increment my-key by 2:

conn:query("INCRBY", "my-key", 2)