Giter VIP home page Giter VIP logo

redutil's Introduction

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/mixer/redutil.v2/conn"
    "gopkg.in/mixer/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.

redutil's People

Contributors

codelingoteam avatar connor4312 avatar doridian avatar mattjustmatt avatar msft-mw avatar pricejc-msft avatar ttaylorr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.