RedisConnection

public final class RedisConnection : RedisClient

A concrete RedisClient implementation that represents an individual connection to a Redis database instance.

For basic setups, you will just need a NIO.SocketAddress and a NIO.EventLoop and perhaps a password.

let eventLoop: EventLoop = ...
let connection = RedisConnection.connect(
    to: try .makeAddressResolvingHost("my.redis.url", port: RedisConnection.defaultPort),
    on: eventLoop
).wait()

let result = try connection.set("my_key", to: "some value")
    .flatMap { return connection.get("my_key") }
    .wait()

print(result) // Optional("some value")

Note: wait() is used in the example for simplicity. Never call wait() on an event loop.

See NIO.SocketAddress, NIO.EventLoop, and RedisClient

  • Declaration

    Swift

    public var eventLoop: EventLoop { get }
  • Is the connection to Redis still open?

    Declaration

    Swift

    public var isConnected: Bool { get }
  • Controls the behavior of when sending commands over this connection. The default is `true.

    When set to false, the commands will be placed into a buffer, and the host machine will determine when to drain the buffer. When set to true, the buffer will be drained as soon as commands are added.

    Important

    Even when set to true, the host machine may still choose to delay sending commands.

    Note

    Setting this to true will immediately drain the buffer.

    Declaration

    Swift

    public var sendCommandsImmediately: Bool { get set }
  • The documented default port that Redis connects through.

    See https://redis.io/topics/quickstart

    Declaration

    Swift

    public static let defaultPort: Int
  • Creates a new connection to a Redis instance.

    If you would like to specialize the NIO.ClientBootstrap that the connection communicates on, override the default by passing it in as tcpClient.

    let eventLoopGroup: EventLoopGroup = ...
    var customTCPClient = ClientBootstrap.makeRedisTCPClient(group: eventLoopGroup)
    customTCPClient.channelInitializer { channel in
        // channel customizations
    }
    let connection = RedisConnection.connect(
        to: ...,
        on: eventLoopGroup.next(),
        password: ...,
        tcpClient: customTCPClient
    ).wait()
    

    It is recommended that you be familiar with ClientBootstrap.makeRedisTCPClient(group:) and NIO.ClientBootstrap in general before doing so.

    Note: Use of wait() in the example is for simplicity. Never call wait() on an event loop.

    Important

    Call close() on the connection before letting the instance deinit to properly cleanup resources.

    Note

    If a password is provided, the connection will send an AUTH command to Redis as soon as it has been opened.

    Declaration

    Swift

    public static func connect(
        to socket: SocketAddress,
        on eventLoop: EventLoop,
        password: String? = nil,
        logger: Logger = Logger(label: "RediStack.RedisConnection"),
        tcpClient: ClientBootstrap? = nil
    ) -> EventLoopFuture<RedisConnection>

    Parameters

    socket

    The NIO.SocketAddress information of the Redis instance to connect to.

    eventLoop

    The NIO.EventLoop that this connection will execute all tasks on.

    password

    The optional password to use for authorizing the connection with Redis.

    logger

    The Logging.Logger instance to use for all logging purposes. If one is not provided, one will be created. A Foundation.UUID will be attached to the metadata to uniquely identify this connection instance’s logs.

    tcpClient

    If you have chosen to configure a NIO.ClientBootstrap yourself, this will.

    Return Value

    A NIO.EventLoopFuture that resolves with the new connection after it has been opened, and if a password is provided, authenticated.

  • Sends a QUIT command to Redis, then closes the NIO.Channel that supports this connection.

    See https://redis.io/commands/quit

    Important

    Regardless if the returned NIO.EventLoopFuture fails or succeeds - after calling this method the connection should no longer be used for sending commands to Redis.

    Declaration

    Swift

    @discardableResult
    public func close() -> EventLoopFuture<Void>

    Return Value

    A NIO.EventLoopFuture that resolves when the connection has been closed.