Classes
The following classes are available globally.
-
Handles incoming byte messages from Redis and decodes them according to the Redis Serialization Protocol (RESP).
See
See moreNIO.ByteToMessageDecoder
,RESPTranslator
and https://redis.io/topics/protocolDeclaration
Swift
public final class RedisByteDecoder : ByteToMessageDecoder
-
An object that operates in a First In, First Out (FIFO) request-response cycle.
See moreRedisCommandHandler
is aNIO.ChannelDuplexHandler
that sendsRedisCommand
instances to Redis, and fulfills the command’sNIO.EventLoopPromise
as soon as aRESPValue
response has been received from Redis.Declaration
Swift
public final class RedisCommandHandler
extension RedisCommandHandler: ChannelInboundHandler
extension RedisCommandHandler: ChannelOutboundHandler
-
Encodes outgoing
RESPValue
data into a rawByteBuffer
according to the Redis Serialization Protocol (RESP).See
See moreNIO.MessageToByteEncoder
,RESPTranslator
, and https://redis.io/topics/protocolDeclaration
Swift
public final class RedisMessageEncoder : MessageToByteEncoder
-
A channel handler that stores a map of closures and channel or pattern names subscribed to in Redis using Pub/Sub.
These
RedisPubSubMessageReceiver
closures are added and removed using methods directly on an instance of this handler.When a receiver is added or removed, the handler will send the appropriate subscribe or unsubscribe message to Redis so that the connection reflects the local Channel state.
ChannelInboundHandler
This handler is designed to be placed before a
RedisCommandHandler
so that it can intercept Pub/Sub messages and dispatch them to the appropriate receiver.If a response is not in the Pub/Sub message format as specified by Redis, then it is treated as a normal Redis command response and sent further into the pipeline so that eventually a
RedisCommandHandler
can process it.ChannelOutboundHandler
This handler is what is defined as a “transparent”
NIO.ChannelOutboundHandler
in that it does absolutely nothing except forward outgoing commands in the pipeline.The reason why this handler needs to conform to this protocol at all, is that subscribe and unsubscribe commands are executed outside of a normal
NIO.Channel.write(_:)
cycle, as message receivers aren’t command arguments and need to be stored.All of this is outside the responsibility of the
RedisCommandHandler
, so theRedisPubSubHandler
uses its ownNIO.ChannelHandlerContext
being before the command handler to short circuit the pipeline.RemovableChannelHandler
As a connection can move in and out of “PubSub mode”, this handler is can be added and removed from a
NIO.ChannelPipeline
as needed.When the handler has received a
See moreremoveHandler(context:removalToken:)
request, it will remove itself immediately.Declaration
Swift
public final class RedisPubSubHandler
extension RedisPubSubHandler: RemovableChannelHandler
extension RedisPubSubHandler: ChannelInboundHandler
extension RedisPubSubHandler: ChannelOutboundHandler
-
A concrete
RedisClient
implementation that represents an individual connection to a Redis instance.For basic setups, you will just need a
NIO.EventLoop
and perhaps apassword
.let eventLoop: EventLoop = ... let connection = RedisConnection.make( configuration: .init(hostname: "my.redis.url", password: "some_password"), boundEventLoop: 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:
See morewait()
is used in the example for simplicity. Never callwait()
on an event loop.Declaration
Swift
public final class RedisConnection : RedisClient, RedisClientWithUserContext
-
A
RedisConnectionPool
is an implementation ofRedisClient
backed by a pool of connections to Redis, rather than a single one.RedisConnectionPool
uses a pool of connections on a singleEventLoop
to manage its activity. This pool may vary in size and strategy, including how many active connections it tries to manage at any one time and how it responds to demand for connections beyond its upper limit.Note that
See moreRedisConnectionPool
is entirely thread-safe, even though all of its connections belong to a singleEventLoop
: if callers call the API from a differentEventLoop
(or from noEventLoop
at all)RedisConnectionPool
will ensure that the call is dispatched to the correct loop.Declaration