Giter Club home page Giter Club logo

redbird's Introduction

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/vapor/redbird.git", majorVersion: 0, minor: 12)

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

redbird's People

Contributors

czechboy0 avatar gurehamu avatar tanner0101 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.