github.com/Zewo/Redis

Redis client for (pure) Swift


License
MIT

Documentation

Redis

Swift 3.0 License MIT Slack Status Build Status

A pure Swift client for Redis.

Features

  • Pub/Sub
  • Pipeline with DISCARD
  • Scripts
  • All commands

Installation

Add Redis to your Package.swift

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/Zewo/Redis", majorVersion: 0, minor: 3)
    ]
)

Using

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 implemented
  • MIGRATE, 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

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"))
}

PubSub

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"])...")
    }
}

Contributing

Pull requests are welcome, there is a lot to do.

We recommend using the Vagrant from redis-py to test everything.

Community

Slack

Join us on Slack.

License

Redis is released under the MIT license. See LICENSE for details.