Skip to content

Commit

Permalink
Merge pull request #60 from vapor/updates
Browse files Browse the repository at this point in the history
stream fix + readme + circle cleanup
  • Loading branch information
tanner0101 authored Apr 6, 2017
2 parents 7c53cc1 + 259da7b commit d2057fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 118 deletions.
134 changes: 20 additions & 114 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,114 +1,20 @@
# Redis

[![Swift](http://img.shields.io/badge/swift-3.1-brightgreen.svg)](https://swift.org)
[![CircleCI](https://circleci.com/gh/vapor/redis.svg?style=shield)](https://circleci.com/gh/vapor/redis)
[![Slack Status](http://vapor.team/badge.svg)](http://vapor.team)

Redis communication protocol specification: [http://redis.io/topics/protocol](http://redis.io/topics/protocol)

A Swift wrapper for Redis.

- [x] Pure Swift
- [x] Pub/sub
- [x] Pipelines
- [x] Fast (Byte based)

## 📖 Examples

```swift
import Redis

let client = try TCPClient()

try client.command(.set, ["FOO", "BAR"])

let res = try client.command(.get, ["FOO"])
print(res?.string) // "BAR"
```

### Custom Host / Port

Setting a custom hostname and port for the TCP connection is easy.

```swift
import Redis

let client = try TCPClient(
hostname: "127.0.0.1",
port: 6379
)
```

### Password

Set the password to authorize the connection upon init.

```swift
import Redis

let client = try TCPClient(password: "secret")
```

### Pipeline

Pipelines can be used to send multiple queries at once and receive their responses as an array.

```swift
let client = try TCPClient()
let results = try client
.makePipeline()
.enqueue(.set, ["FOO", "BAR"])
.enqueue(.set, ["Hello", "World"])
.enqueue(.get, ["Hello"])
.enqueue(.get, ["FOO"])
.execute()

print(results) // ["OK", "OK", "World", "Bar"]
```

### Pub/Sub

Publish and subscribe is a mechanism by which two processes or threads can share data.

Note: `subscribe` will block and loop forever. Use on a background thread if you want to continue execution on the main thread.

```swift
background {
let client = try TCPClient()
try client.subscribe(channel: "vapor") { data in
print(data) // "FOO"
}
}

let client = try TCPClient()
try client.publish(channel: "vapor", "FOO")
```

### Ping

For testing the connection.

```swift
let client = try TCPClient()
try client.command(.ping)
print(res?.string) // "PONG"
```

## 📖 Documentation

Visit the Vapor web framework's [documentation](http://docs.vapor.codes) for instructions on how to use this package.

## 💧 Community

Join the welcoming community of fellow Vapor developers in [slack](http://vapor.team).

## 🔧 Compatibility

This package has been tested on macOS and Ubuntu.

## :alien: Original Author

[![Blog](https://img.shields.io/badge/blog-honzadvorsky.com-green.svg)](http://honzadvorsky.com)
[![Twitter Czechboy0](https://img.shields.io/badge/twitter-czechboy0-green.svg)](http://twitter.com/czechboy0)

Honza Dvorsky - http://honzadvorsky.com, [@czechboy0](http://twitter.com/czechboy0)
<p align="center">
<img src="https://cloud.githubusercontent.com/assets/1342803/24753711/a2e633a8-1ad3-11e7-80ee-4b79ebb077cc.png" width="320" alt="Redis">
<br>
<br>
<a href="http://beta.docs.vapor.codes/redis/package/">
<img src="http://img.shields.io/badge/read_the-docs-92A8D1.svg" alt="Documentation">
</a>
<a href="http://vapor.team">
<img src="http://vapor.team/badge.svg" alt="Slack Team">
</a>
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://circleci.com/gh/vapor/redis">
<img src="https://circleci.com/gh/vapor/redis.svg?style=shield" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-3.1-brightgreen.svg" alt="Swift 3.1">
</a>
</center>
4 changes: 4 additions & 0 deletions Sources/Redis/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public final class Client<StreamType: DuplexStream> {
public func makePipeline() -> Pipeline<StreamType> {
return Pipeline(self)
}

deinit {
try? stream.close()
}
}

extension Client {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Redis/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Transport

/// Parses Redis Data from a Stream
public final class Parser<StreamType: DuplexStream> {
public let stream: StreamType
public let stream: StreamBuffer<StreamType>
public init(_ stream: StreamType) {
self.stream = stream
self.stream = StreamBuffer(stream)
}

/// Parse a Redis Data from the stream
Expand Down Expand Up @@ -93,7 +93,7 @@ public final class Parser<StreamType: DuplexStream> {
bytes.reserveCapacity(fullLength)

while bytes.count < fullLength {
bytes += try stream.read(max: fullLength)
bytes += try stream.read(max: fullLength - bytes.count)
}

return Array(bytes[0..<bytes.count - 2])
Expand Down
10 changes: 9 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
machine:
services:
- redis
dependencies:
override:
- eval "$(curl -sL https://apt.vapor.sh)"
- sudo apt-get install vapor cmysql
- sudo chmod -R a+rx /usr/
test:
override:
- eval "$(curl -sL swift.vapor.sh/ci-3.1)"
- swift build
- swift build -c release
- swift test

0 comments on commit d2057fa

Please sign in to comment.