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.
Use go get
to install or upgrade (-u
) the redis
package.
go get -u menteslibres.net/gosexy/redis
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.
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
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
See the online docs for gosexy/redis at godoc.org.
Don't forget to check the complete list of redis commands too!
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.