redutil

module
v2.5.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2016 License: MIT

README

redutil Build Status Coverage Status godoc reference

This package consists of several utilities to make Redis easier and more consistent in Go.

pubsub

Traditional Redis libraries allow you to subscribe to events, and maybe even pool connections. But there's often no mechanism for maintaining subscribed state in the event of a connection failure, and many packages aren't thread-safe. This package, redutil/pubsub, solves these issues.

It is fully thread safe and unit tested. We're currently using it in production, though it has not yet been entirely battle-tested. Feel free to open issues on this repository.

package main

import (
    "time"
    "gopkg.in/WatchBeam/redutil.v2/conn"
    "gopkg.in/WatchBeam/redutil.v2/pubsub"
)

func main() {
    // Create a new pubsub client. This will create and manage connections,
    // even if you disconnect.
    c := pubsub.New(conn.New(conn.ConnectionParam{
        Address: "127.0.0.1:6379",
        // optional password
        Password: "secret",
    }, 1))
    go client.Connect()
    defer c.TearDown()

    go listenChannel(c)
    go listenPattern(c)

    // Wait forever!
    select {}
}


// Simple example function that listens for all events broadcast
// in the channel "chan".
func listenChannel(c *pubsub.Client) {
    listener := c.Listen(pubsub.Channel, "chan")
    defer listener.Unsubscribe()
    for _, message := range listener.Messages {
        doStuff()
    }
}

// Example that listens for events that match the pattern
// "foo:*:bar". Note that we listen to the `PMessages` channel, not `Messages`.
func listenPattern(c *pubsub.Client) {
    listener := c.Listen(pubsub.Pattern, "foo:*:bar")
    defer listener.Unsubscribe()

    for _, message := range listener.PMessages {
        // You got mail!
    }
}

License

Copyright 2015-2016 by Beam LLC. Distributed under the MIT license.

Directories

Path Synopsis
Package pubsub implements helpers to connect to and read events from Redis in a reliable way.
Package pubsub implements helpers to connect to and read events from Redis in a reliable way.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL