Go语言的Redis客户端,官方推荐了两个客户端:redigo和radix.v2
安装之:
安装之:
go get github.com/mediocregopher/radix.v2
go get github.com/garyburd/redigo/redis
test_redigo.go
test_radixv2.go
test_redigo.go
package main
import (
" fmt "
" github.com/garyburd/redigo/redis "
" log "
)
// test common
func testCommonOperate() {
// connect redis server
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
log.Fatal( " connect redis-server error: ", err)
return
}
defer c.Close()
// SET
v, err := c.Do( " SET ", " name ", " red ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
// GET
v, err = redis.String(c.Do( " GET ", " name "))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
// LPUSH
c.Do( " lpush ", " redlist ", " qqq ")
c.Do( " lpush ", " redlist ", " www ")
c.Do( " lpush ", " redlist ", " eee ")
// LRANGE
values, _ := redis.Values(c.Do( " lrange ", " redlist ", " 0 ", " 100 "))
for _, v := range values {
fmt.Println(string(v.([]byte)))
}
// Scan
var v1 string
redis.Scan(values, &v1)
fmt.Println(v1)
// piple line
c.Send( " SET ", " name ", " red ")
c.Send( " GET ", " name ")
c.Flush()
c.Receive()
c.Receive()
}
func subscribe() {
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
psc := redis.PubSubConn{c}
psc.Subscribe( " redChatRoom ")
for {
switch v := psc.Receive().(type) {
case redis.Message:
fmt.Printf( " %s: message: %s\n ", v.Channel, v.Data)
case redis.Subscription:
fmt.Printf( " %s: %s %d\n ", v.Channel, v.Kind, v.Count)
case error:
fmt.Println(v)
return
}
}
}
func testSubscribe() {
go subscribe()
go subscribe()
go subscribe()
go subscribe()
go subscribe()
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
for {
var s string
fmt.Scanln(&s)
_, err := c.Do( " PUBLISH ", " redChatRoom ", s)
if err != nil {
fmt.Println( " pub err: ", err)
return
}
}
}
func main() {
testCommonOperate()
//testSubscribe()
}
import (
" fmt "
" github.com/garyburd/redigo/redis "
" log "
)
// test common
func testCommonOperate() {
// connect redis server
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
log.Fatal( " connect redis-server error: ", err)
return
}
defer c.Close()
// SET
v, err := c.Do( " SET ", " name ", " red ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
// GET
v, err = redis.String(c.Do( " GET ", " name "))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(v)
// LPUSH
c.Do( " lpush ", " redlist ", " qqq ")
c.Do( " lpush ", " redlist ", " www ")
c.Do( " lpush ", " redlist ", " eee ")
// LRANGE
values, _ := redis.Values(c.Do( " lrange ", " redlist ", " 0 ", " 100 "))
for _, v := range values {
fmt.Println(string(v.([]byte)))
}
// Scan
var v1 string
redis.Scan(values, &v1)
fmt.Println(v1)
// piple line
c.Send( " SET ", " name ", " red ")
c.Send( " GET ", " name ")
c.Flush()
c.Receive()
c.Receive()
}
func subscribe() {
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
psc := redis.PubSubConn{c}
psc.Subscribe( " redChatRoom ")
for {
switch v := psc.Receive().(type) {
case redis.Message:
fmt.Printf( " %s: message: %s\n ", v.Channel, v.Data)
case redis.Subscription:
fmt.Printf( " %s: %s %d\n ", v.Channel, v.Kind, v.Count)
case error:
fmt.Println(v)
return
}
}
}
func testSubscribe() {
go subscribe()
go subscribe()
go subscribe()
go subscribe()
go subscribe()
c, err := redis.Dial( " tcp ", " 127.0.0.1:6379 ")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
for {
var s string
fmt.Scanln(&s)
_, err := c.Do( " PUBLISH ", " redChatRoom ", s)
if err != nil {
fmt.Println( " pub err: ", err)
return
}
}
}
func main() {
testCommonOperate()
//testSubscribe()
}
test_radixv2.go
package main
import (
" fmt "
" github.com/mediocregopher/radix.v2/redis "
" log "
" strconv "
)
func test1() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
return
}
defer conn.Close()
resp := conn.Cmd( " HMSET ", " album:1 ", " title ", " Electric Ladyland ", " artist ", " Jimi Hendrix ", " price ", 4.95, " likes ", 8)
if resp.Err != nil {
log.Fatal(resp.Err)
}
fmt.Println( " Electric Ladyland added! ")
}
func test2() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
title, err := conn.Cmd( " HGET ", " album:1 ", " title ").Str()
if err != nil {
log.Fatal(err)
}
artist, err := conn.Cmd( " HGET ", " album:1 ", " artist ").Str()
if err != nil {
log.Fatal(err)
}
price, err := conn.Cmd( " HGET ", " album:1 ", " price ").Float64()
if err != nil {
log.Fatal(err)
}
likes, err := conn.Cmd( " HGET ", " album:1 ", " likes ").Int()
if err != nil {
log.Fatal(err)
}
fmt.Printf( " %s by %s: £%.2f [%d likes]\n ", title, artist, price, likes)
}
type Album struct {
Title string
Artist string
Price float64
Likes int
}
func populateAlbum(reply map[string]string) (*Album, error) {
var err error
ab := new(Album)
ab.Title = reply[ " title "]
ab.Artist = reply[ " artist "]
ab.Price, err = strconv.ParseFloat(reply[ " price "], 64)
if err != nil {
return nil, err
}
ab.Likes, err = strconv.Atoi(reply[ " likes "])
if err != nil {
return nil, err
}
return ab, nil
}
func test3() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
reply, err := conn.Cmd( " HGETALL ", " album:1 ").Map()
if err != nil {
log.Fatal(err)
}
ab, err := populateAlbum(reply)
if err != nil {
log.Fatal(err)
}
fmt.Println(ab)
}
func main() {
test1()
//test2()
//test3()
}
import (
" fmt "
" github.com/mediocregopher/radix.v2/redis "
" log "
" strconv "
)
func test1() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
return
}
defer conn.Close()
resp := conn.Cmd( " HMSET ", " album:1 ", " title ", " Electric Ladyland ", " artist ", " Jimi Hendrix ", " price ", 4.95, " likes ", 8)
if resp.Err != nil {
log.Fatal(resp.Err)
}
fmt.Println( " Electric Ladyland added! ")
}
func test2() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
title, err := conn.Cmd( " HGET ", " album:1 ", " title ").Str()
if err != nil {
log.Fatal(err)
}
artist, err := conn.Cmd( " HGET ", " album:1 ", " artist ").Str()
if err != nil {
log.Fatal(err)
}
price, err := conn.Cmd( " HGET ", " album:1 ", " price ").Float64()
if err != nil {
log.Fatal(err)
}
likes, err := conn.Cmd( " HGET ", " album:1 ", " likes ").Int()
if err != nil {
log.Fatal(err)
}
fmt.Printf( " %s by %s: £%.2f [%d likes]\n ", title, artist, price, likes)
}
type Album struct {
Title string
Artist string
Price float64
Likes int
}
func populateAlbum(reply map[string]string) (*Album, error) {
var err error
ab := new(Album)
ab.Title = reply[ " title "]
ab.Artist = reply[ " artist "]
ab.Price, err = strconv.ParseFloat(reply[ " price "], 64)
if err != nil {
return nil, err
}
ab.Likes, err = strconv.Atoi(reply[ " likes "])
if err != nil {
return nil, err
}
return ab, nil
}
func test3() {
conn, err := redis.Dial( " tcp ", " localhost:6379 ")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
reply, err := conn.Cmd( " HGETALL ", " album:1 ").Map()
if err != nil {
log.Fatal(err)
}
ab, err := populateAlbum(reply)
if err != nil {
log.Fatal(err)
}
fmt.Println(ab)
}
func main() {
test1()
//test2()
//test3()
}