文章目录
Mac 下载 Redis
Mac安装Redis,原来就是这么简单
Mac Redix 数据库打开
- brew services start redis
服务端
客户端
导包
go
复制代码
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
连接数据库
go
复制代码
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed,err:%v", err)
return
}
defer c.Close()
}
操作字符串
go
复制代码
// 获取字符串
func getString(c redis.Conn) {
res, err := redis.String(c.Do("Get", "username"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
}
// 设置字符串
func setString(c redis.Conn) {
_, err := c.Do("Set", "username", "jack")
if err != nil {
fmt.Println(err)
return
}
}
// 批量设置字符串
func setStrings(c redis.Conn) {
_, err := c.Do("MSet", "username", "jack", "phone", "188888888")
if err != nil {
fmt.Println("MSet error:", err)
return
}
}
// 批量获取字符串
func getStrings(c redis.Conn) {
res2, err := redis.Strings(c.Do("MGet", "username", "phone"))
if err != nil {
fmt.Println("MGet error:", err)
return
}
fmt.Println(res2)
}
操作 Hash
go
复制代码
// redis Hash 操作 设置
func setHash(c redis.Conn) {
_, err := c.Do("HSet", "names", "jim", "barry")
if err != nil {
fmt.Println("hset error: ", err)
return
}
}
// redis Hash 操作 获取
func getHash(c redis.Conn) {
res3, err := redis.String(c.Do("HGet", "names", "jim"))
if err != nil {
fmt.Println("hget error: ", err)
return
}
fmt.Println(res3)
}
设置过期时间
go
复制代码
// redis 设置过期时间
func setExpire(c redis.Conn) {
_, err := c.Do("expire", "names", "10")
if err != nil {
fmt.Println("expire err: ", err)
}
}
队列操作
go
复制代码
// redis 队列
func queueOpr(c redis.Conn) {
//入队列
_, err := c.Do("lpush", "Queue", "jim", "barry", 9)
if err != nil {
fmt.Println("lpush error:", err)
return
}
//出队列
for true {
r, err := redis.String(c.Do("lpop", "Queue"))
if err != nil {
fmt.Println("lpop error: ", err)
return
}
fmt.Println(r)
}
//获取队列长度
res4, err := redis.Int(c.Do("llen", "Queue"))
if err != nil {
fmt.Println("llen error: ", err)
return
}
fmt.Println(res4)
}
连接池
go
复制代码
// 使用 Pool 初始化连接池
var pool *redis.Pool
func init() {
pool = &redis.Pool{
MaxActive: 1024,
MaxIdle: 16,
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)
}
并发操作
go
复制代码
//redis并发实例
func main() {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("connect redis error:", err)
return
}
defer conn.Close()
conn.Send("HSET", "students", "name", "jim", "age", "19")
conn.Send("HSET", "students", "score", "100")
conn.Send("HSET", "students", "age")
conn.Flush()
res1, err := conn.Receive()
fmt.Printf("Receive res1:%v \n", res1)
res2, err := conn.Receive()
fmt.Printf("Receive res2:%v \n", res2)
res3, err := conn.Receive()
fmt.Printf("Receive res3:%v \n", res3)
}
管道化操作
go
复制代码
//redis管道化操作
func main() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("conn redis failed,err:", err)
return
}
defer c.Close()
c.Send("SET", "username1", "jim")
c.Send("SET", "username2", "jack")
c.Flush()
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)
}
事务操作
go
复制代码
// redis事务操作
func main() {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
fmt.Println("connect redis error:", err)
return
}
defer conn.Close()
conn.Send("MULTI")
conn.Send("INCR", "Foo")
conn.Send("INCR", "bar")
r, err := conn.Do("EXEC")
fmt.Println(r)
}