A pure Swift client for Redis.
- Pub/Sub
- Pipeline with
DISCARD
- Scripts
- All commands
Add Redis
to your Package.swift
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/Zewo/Redis", majorVersion: 0, minor: 3)
]
)
let redis = try Redis("127.0.0.1", 6379)
try redis.command(.SET("foo", "bar"))
All commands and its parameters are defined in CommandTypeEnum
enum, with parameters in the same order as Redis docs. The command
function returns the same response from Redis.
Commands with milliseconds (SETEX/PSETEX
, EXPIRE/PEXPIRE
, etc) have a Bool
parameter to send or return in milliseconds.
It's always the last parameter.
At this time, there is some commands exceptions:
SORT
- To be implementedMIGRATE
,WAIT
,SCAN
- These are commands to manage the server. A discussion could be opened to implement it or don't.Server
commands - Same as above
Pipeline works by issuing commands inside a closure:
try redis.pipeline {
try redis.command(.SET("foobar", "foo bar"))
try redis.command(.SET("foo", "bar"))
}
If you need to WATCH
a key, use the first argument. In case of an error, it'll be returned as nil
.
try redis.pipeline(["foo"]) {
try redis.command(.SET("foobar", "foo bar"))
try redis.command(.SET("foo", "bar"))
}
WARNING: This is a first draft and the syntax is open to discussion. Beware of changes.
PubSub can subscribe to multiple channels at once, but you have to unsubscribe one at time.
let redis = try Redis("127.0.0.1", 6379)
let pubsub = PubSub(conn: redis)
try pubsub.subscribe(["foo-channel", "bar-channel"]) { message in
if message["data"] as? String == "stop" {
print("Stahp \(message["channel"])!")
pubsub.unsubscribe(message["channel"] as! String)
} else {
print("Keep walking \(message["channel"])...")
}
}
Pull requests are welcome, there is a lot to do.
We recommend using the Vagrant from redis-py to test everything.
Join us on Slack.
Redis is released under the MIT license. See LICENSE for details.