go get github.com/gomodule/redigo
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
_, err = c.Do("Set", "username", "jim")
if err != nil {
fmt.Println(err)
return
}
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
res, err := redis.String(c.Do("Get", "username"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
_, err = c.Do("MSet", "username", "james", "phone", "0988888888")
if err != nil {
fmt.Println(err)
return
}
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
res, err := redis.Strings(c.Do("MGet", "username", "phone"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
_, err = c.Do("HSet", "names", "jim", "barry")
if err != nil {
fmt.Println(err)
return
}
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
res, err := redis.String(c.Do("HGet", "names", "jim"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
_, err = c.Do("expire", "names", 5)
if err != nil {
fmt.Println(err)
return
}
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
_, err = c.Do("lpush", "Queue", "jim", "barry", 9)
if err != nil {
fmt.Println("lpush error: ", err)
return
}
for {
r, err := redis.String(c.Do("lpop", "Queue"))
if err != nil {
fmt.Println("lpop error: ", err)
break
}
fmt.Println(r)
}
res, err := redis.Int(c.Do("llen", "Queue"))
if err != nil {
fmt.Println("llen error: ", err)
return
}
fmt.Println(res)
}
可以建立多條連線且不釋放,可避免與redis建立連線所需的時間。
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
var pool *redis.Pool
func init() {
pool = &redis.Pool{
MaxIdle: 16, //最大的閒置連線數
MaxActive: 1024,//最大的啟動連線數
IdleTimeout: 300, //最大的閒置連線等待時間
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "localhost:6379")
},
}
}
func main() {
c := pool.Get()
defer c.Close()
_, err := c.Do("Set", "username", "jack")
if err != nil {
fmt.Println(err)
return
}
r, err := redis.String(c.Do("Get", "username"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(r)
}
用戶端在發送請求到伺服器時,可以在伺服器「還沒回應」的情況下不斷發送請求,最後再一次接收所有資料。
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed, err:", err)
return
}
defer c.Close()
//Send()會先輸入到輸出緩存
c.Send("SET", "username1", "jim")
c.Send("SET", "username2", "jack")
//Flush()會凊空輸出緩存
c.Flush()
//Receive()會依照FIFO讀取回應
v, err := c.Receive()
fmt.Printf("v:%v,err:%v\n", v, err)
v, err = c.Receive()
fmt.Printf("v:%v,err:%v\n", v, err)
v, err = c.Receive()
fmt.Printf("v:%v,err:%v\n", v, err)
}