Coder Social home page Coder Social logo

redis's Introduction

menteslibres.net/gosexy/redis

Package redis was formerly a wrapper for the official redis C client hiredis. As of October 2014, package redis was splitted into two different packages: a RESP decoder (resp) and the redis client (redis) this page describes.

Build Status

How to install or upgrade

Use go get to install or upgrade (-u) the redis package.

go get -u menteslibres.net/gosexy/redis

Usage

Use import to use redis in your program:

import (
  "menteslibres.net/gosexy/redis"
)

The redis.New() function returns a *redis.Client pointer that you can use to interact with a redis server.

This example shows how to connect and ping a redis server.

var client *redis.Client

client = redis.New()

err = client.Connect(host, port)

if err != nil {
  log.Fatalf("Connect failed: %s\n", err.Error())
  return
}

log.Println("Connected to redis-server.")

log.Printf("Sending PING...\n")
s, err = client.Ping()

if err != nil {
  log.Fatalf("Could not ping: %s\n", err.Error())
  return
}

log.Printf("Received %s!\n", s)

client.Quit()

This is the expected output of the above ping-pong example:

2013/02/19 07:15:52 Connected to redis-server.
2013/02/19 07:15:52 Sending PING...
2013/02/19 07:15:52 Received PONG!

Always use client.Quit() to close the client connection.

Examples

Simple SET and GET

An example on how to use *redis.Client to send SET and GET commands to the client.

var client *redis.Client
var err error

client = redis.New()

err = client.Connect(host, port)

if err != nil {
  log.Fatalf("Connect failed: %s\n", err.Error())
  return
}

log.Println("Connected to redis-server.")

// DEL hello
log.Printf("DEL hello\n")
client.Del("hello")

// SET hello 1
log.Printf("SET hello 1\n")
client.Set("hello", 1)

// INCR hello
log.Printf("INCR hello\n")
client.Incr("hello")

// GET hello
log.Printf("GET hello\n")
s, err = client.Get("hello")

if err != nil {
  log.Fatalf("Could not GET: %s\n", err.Error())
  return
}

log.Printf("> hello = %s\n", s)

client.Quit()

This is the expected output of the above set-get example:

2013/02/19 07:19:37 Connected to redis-server.
2013/02/19 07:19:37 DEL hello
2013/02/19 07:19:37 SET hello 1
2013/02/19 07:19:37 INCR hello
2013/02/19 07:19:37 GET hello
2013/02/19 07:19:37 > hello = 2

Subscriptions

You can use SUBSCRIBE and PSUBSCRIBE with channels and goroutines inside a non-blocking connection. You can create a non-blocking connection using the ConnectNonBlock or ConnectUnixNonBlock functions.

go consumer.Subscribe(rec, "channel")

var ls []string

for {
  ls = <-rec
  log.Printf("Consumer received: %v\n", strings.Join(ls, ", "))
}

The above snippet is part of a subscription example, if you run the full example you'll see something like this:

2013/02/19 07:25:33 Consumer received: message, channel, Hello world!
2013/02/19 07:25:33 Consumer received: message, channel, Do you know how to count?
2013/02/19 07:25:33 Consumer received: message, channel, 0
2013/02/19 07:25:33 Consumer received: message, channel, 1
2013/02/19 07:25:33 Consumer received: message, channel, 2

Documentation

See the online docs for gosexy/redis at godoc.org.

Don't forget to check the complete list of redis commands too!

License

Copyright (c) 2013-2014 José Carlos Nieto, https://menteslibres.net/xiam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

redis's People

Contributors

xiam avatar titanous avatar

Watchers

James Cloos 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.