SwiftNIO Redis is a Swift package that contains a high performance Redis protocol implementation for SwiftNIO. This is a standalone project and has no other dependencies but SwiftNIO.
Apart from the protocol implementation which can encode and decode RESP (REdis Serialization Protocol), we also provide a Redis client module build on top.
What is Redis? Redis is a highly scalable in-memory data structure store, used as a database, cache and message broker. For example it can be used to implement a session store backing a web backend using its "expiring keys" feature, or it can be used as a relay to implement a chat server using its builtin PubSub features.
This Swift package contains a simple Redis client based on the protocol implementation swift-nio-redis. We also provide an actual Redis Server written in Swift, using SwiftNIO and SwiftNIO Redis.
An example Package.swift
importing the necessary modules:
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "RedisTests",
dependencies: [
.package(url: "https://github.com/NozeIO/swift-nio-redis-client.git",
from: "0.10.0")
],
targets: [
.target(name: "MyClientTool",
dependencies: [ "Redis" ])
]
)
The
Redis
client module is modeled after the Node.js
node_redis
module,
but it also supports NIO like Promise/Future based methods in addition
to the Node.js (err,result)
style callbacks. Choose your poison.
import Redis
let client = Redis.createClient()
client.set ("counter", 0, expire: 2)
client.incr("counter", by: 10)
client.get ("counter") { err, value in
print("Reply:", value)
}
client.keys("*") { err, reply in
guard let keys = reply else { return print("got no keys!") }
print("all keys in store:", keys.joined(separator: ","))
}
Using NIO Promises:
client
.set ("counter", 0, expire: 2)
.then {
client.incr("counter", by: 10)
}
.then {
client.get("counter")
}
.map {
print("counter is:", $0)
}
The
protocol implementation
is considered complete. There are a few open ends
in the telnet
variant, yet the regular binary protocol is considered done.
The Redis client module has a few more open ends, but seems to work fine.
Brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.