vapor/redis


⚡️Non-blocking, event-driven Redis client.

License: MIT

Language: Swift

Keywords: cache, redis, redis-client, server-side-swift, swift, swift-linux, vapor, vapor-service


Redbird

Build Status Latest Release Platforms Package Managers

Blog Twitter Czechboy0

Redis + Swift. Red Is Swift. Swift is a bird. Redbird.

Attempt at a pure-Swift implementation of a Redis client from the original protocol spec.

Redis communication protocol specification: http://redis.io/topics/protocol

Why?

When I write servers for my apps, I usually use 1) Linux servers, 2) Redis as my database/cache. Now I also want to write everything in Swift. I looked through the existing Swift Redis wrappers and unfortunately all of them just wrapped a C library, which had to be installed externally (yuck). Thus I decided to throw all that away, go back to the Redis protocol specification and build up a Swift client without any dependencies, so that it can be used both on OS X and Linux just by adding a Swift Package Manager entry, without the need to install anything extra.

That means I'm writing it up all the way from bare TCP sockets. Just using Glibc and Darwin headers, together with standard Swift libraries. #0dependencies

Installation

Swift Package Manager

.Package(url: "https://github.com/czechboy0/Redbird.git", majorVersion: 0, minor: 10)

Usage

Create a Redbird instance, which opens a socket to the specified Redis server. Then call desired commands on that instance, which synchronously returns a response. That response can be of any of the supported types: SimpleString, BulkString, Integer, Error, RespArray, NullBulkString, NullArray all inherit from the protocol RespObject, which has a RespType to communicate which type you're getting.

do {
    let config = RedbirdConfig(address: "127.0.0.1", port: 6379, password: "foopass")
    let client = try Redbird(config: config)
    let response = try client.command("SET", params: ["mykey", "hello_redis"]).toString() //"OK"
} catch {
    print("Redis error: \(error)")
}

Redbird automatically:

  • authenticates if you pass it a password during initialization
  • attempts one reconnect if the socket was dropped for whatever reason (handy for servers with idle timeouts)

Easy conversion back

Instead of handling the RespObject types directly, you can also use the following convenience converters which will try to convert your RespObject into the specified type:

  • .toString() -> String
  • .toMaybeString() -> String?
  • .toArray() -> [RespObject]
  • .toMaybeArray() -> [RespObject]?
  • .toInt() -> Int
  • .toBool() -> Bool
  • .toError() -> ErrorType

Pipelining

Command pipelining is supported. Just ask for a Pipeline object, enqueue commands on it and then call execute() to send commands to the server. You receive an array of response objects, which respect the enqueing order of your commands.

let responses = try client.pipeline()
    .enqueue("PING")
    .enqueue("SET", params: ["test", "Me_llamo_test"])
    .enqueue("GET", params: ["test"])
    .enqueue("PING")
    .execute()
// responses: [RespObject]

All of the above converters throw an error if invoked on a non-compatible type (like calling toArray() on an Integer).

Missing features?

At the moment the design philosophy of Redbird is to provide a 0-dependency, minimal Swift Redis client. Features such as easier wrappers for things that can be done with standard commands (like transactions) are not on the roadmap at the moment (with the notable exception of AUTH, which is too common to not make easier to do). I want to make sure Redbird allows you to use all of Redis's features. However the aim is not to make it easy, just simple.

That being said, if Redbird doesn't support a fundamental feature that you'd like to use, please create an issue and I'll do my best to add it. Thanks for helping out! 🎉

💙 Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

💝 Contributing

Please create an issue with a description of your problem or open a pull request with a fix.

✌️ License

MIT

👽 Author

Honza Dvorsky - http://honzadvorsky.com, @czechboy0

Project Statistics

Sourcerank 9
Repository Size 407 KB
Stars 356
Forks 34
Watchers 27
Open issues 2
Dependencies 1
Contributors 4
Tags 37
Created
Last updated
Last pushed

Top Contributors See all

Honza Dvorsky Logan Wright Tanner David Graham

Packages Referencing this Repo

github.com/github.com/vapor/redis
⚡️Non-blocking, event-driven Redis client.
Latest release 3.0.0-rc.2.1 - Published - 356 stars
github.com/czechboy0/Redbird
⚡️Non-blocking, event-driven Redis client.
Latest release 3.0.0-rc.2.1 - Published - 356 stars
Redbird
Pure-Swift Redis client. OS X and Linux ready.
Latest release 0.3.0 - Updated - 356 stars
github.com/github.com/czechboy0/Redbird
⚡️Non-blocking, event-driven Redis client.
Latest release 3.0.0-rc.2.1 - Published - 356 stars
github.com/github.com/vapor/redbird
⚡️Non-blocking, event-driven Redis client.
Latest release 3.0.0-rc.2.1 - Published - 356 stars

Recent Tags See all

3.0.0-rc.2.1 March 29, 2018
3.0.0-rc.2 March 21, 2018
3.0.0-rc.1 February 24, 2018
2.2.0 December 06, 2017
2.1.0 October 03, 2017
2.0.0 May 18, 2017
2.0.0-beta.5 May 05, 2017
2.0.0-beta.4 May 03, 2017
2.0.0-beta.3 May 01, 2017
2.0.0-beta.2 April 06, 2017
2.0.0-beta.1 March 29, 2017
2.0.0-alpha.2 March 19, 2017
2.0.0-alpha.1 March 18, 2017
1.0.0 October 20, 2016
0.12.0 October 20, 2016

Something wrong with this page? Make a suggestion

Last synced: 2016-11-24 17:08:10 UTC

Login to resync this repository