Created
December 10, 2014 07:52
-
-
Save joshuarubin/6ad134f78357a3ec86e0 to your computer and use it in GitHub Desktop.
redis cluster golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example program | |
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"github.com/fzzy/radix/extra/cluster" | |
) | |
func errHndlr(err error) { | |
if err != nil { | |
fmt.Println("error:", err) | |
os.Exit(1) | |
} | |
} | |
func main() { | |
c, err := cluster.NewClusterTimeout("172.17.8.101:6379", time.Duration(10)*time.Second) | |
errHndlr(err) | |
defer c.Close() | |
r := c.Cmd("ECHO", "Hello world!") | |
errHndlr(r.Err) | |
s, err := r.Str() | |
errHndlr(err) | |
fmt.Println("echo:", s) | |
//* Strings | |
r = c.Cmd("SET", "mykey0", "myval0") | |
errHndlr(r.Err) | |
s, err = c.Cmd("GET", "mykey0").Str() | |
errHndlr(err) | |
fmt.Println("mykey0:", s) | |
myhash := map[string]string{ | |
"mykey1": "myval1", | |
"mykey2": "myval2", | |
"mykey3": "myval3", | |
} | |
//* List handling | |
mylist := []string{"foo", "bar", "qux"} | |
// Alternativaly: | |
// c.Cmd("RPUSH", "mylist", "foo", "bar", "qux") | |
r = c.Cmd("RPUSH", "mylist", mylist) | |
errHndlr(r.Err) | |
mylist, err = c.Cmd("LRANGE", "mylist", 0, -1).List() | |
errHndlr(err) | |
fmt.Println("mylist:", mylist) | |
//* Hash handling | |
// Alternatively: | |
// c.Cmd("HMSET", "myhash", ""mykey1", "myval1", "mykey2", "myval2", "mykey3", "myval3") | |
r = c.Cmd("HMSET", "myhash", myhash) | |
errHndlr(r.Err) | |
myhash, err = c.Cmd("HGETALL", "myhash").Hash() | |
errHndlr(err) | |
fmt.Println("myhash:", myhash) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment