Go-Redis(一)redigo基本操作
·
Go-Redis(一)redigo基本操作
redis是目前流行的高性能key/value缓存,基本上在各种项目都经常出现,在go中使用的是go-redis/redis包操作redis。
基本使用
安装
go get github.com/garyburd/redigo/redis
连接redis
指定下redis的连接地址和密码即可
// 连接redis
func ConnectRedis()redis.Conn{
client,err := redis.Dial("tcp","localhost:6379",redis.DialPassword("123456"))
if err != nil{
panic(err)
}
return client
}
数据结构操作
redigo对于redis的操作类似于指定cli指令,比较直观。
command arg...
string
复习下string结构的基本操作:
set k v // set一对k-v
get k // 通过k获取对应的v
set k v expire time // 设置带过期时间的k-v
exists k // 是否存在指定key
实例:
// string结构操作
func StringOption(client redis.Conn) {
// set k11 v11
_,err := client.Do("set","k11","v11")
if err != nil{
log.Fatal("set字符串失败,",err)
}
// get k11
val,err := client.Do("get","k11")
if err != nil{
log.Fatal("get字符串失败",err)
}
// 支持类型转换
strVal,_ := redis.String(val,nil)
fmt.Println("get k11 = ",strVal)
client.Do("set","k12","12")
intVal,_:= redis.Int(client.Do("get","k12"))
fmt.Println("转换后:",intVal)
// 带国旗时间的k-v,Ex单位是s set k v expire time
_,err1 := client.Do("set","keyWithEx","valueWithEx","EX","5")
if err1 != nil{
fmt.Println("带过期时间的key set失败,",err)
}
valWithEx,_ := redis.String(client.Do("get","keyWithEx"))
fmt.Println("获取带过期时间的key,",valWithEx)
time.Sleep(10 * time.Second) // 模拟过期时间
valWithEx1,_ := redis.String(client.Do("get","keyWithEx"))
fmt.Println("获取带过期时间的key,",valWithEx1)
// 是否存在指定key
exists,_ := client.Do("exists","k12")
flag,_:= redis.Int(exists,nil);
fmt.Println("是否存在指定key,",flag == 1)
}
结果:
list
列表操作:
lpush list val
lpop list
rpush list val
rpop list
lrange list start end
实例:
// 列表操作
func ListOption(client redis.Conn) {
// del key
client.Do("del","list1")
// lpush list val
client.Do("lpush","list1","l1")
// rpush list val
client.Do("rpush","list1","l3")
// lrange list from end
list,_ := client.Do("lrange","list1","0","100")
l,_:= redis.Values(list,nil)
for _,val := range l{
fmt.Println(string(val.([]byte)))
}
}
其他数据结构的操作也是同样道理,这里就不一一列举
了,主要在于redis的指令
完整代码
package go_redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
"log"
"time"
)
func TestRedis() {
ListOption(ConnectRedis())
}
// 连接redis
func ConnectRedis()redis.Conn{
client,err := redis.Dial("tcp","localhost:6379",redis.DialPassword("123456"))
if err != nil{
panic(err)
}
return client
}
// string结构操作
func StringOption(client redis.Conn) {
// set k11 v11
_,err := client.Do("set","k11","v11")
if err != nil{
log.Fatal("set字符串失败,",err)
}
// get k11
val,err := client.Do("get","k11")
if err != nil{
log.Fatal("get字符串失败",err)
}
// 支持类型转换
strVal,_ := redis.String(val,nil)
fmt.Println("get k11 = ",strVal)
client.Do("set","k12","12")
intVal,_:= redis.Int(client.Do("get","k12"))
fmt.Println("转换后:",intVal)
// 带国旗时间的k-v,Ex单位是s set k v expire time
_,err1 := client.Do("set","keyWithEx","valueWithEx","EX","5")
if err1 != nil{
fmt.Println("带过期时间的key set失败,",err)
}
valWithEx,_ := redis.String(client.Do("get","keyWithEx"))
fmt.Println("获取带过期时间的key,",valWithEx)
time.Sleep(10 * time.Second) // 模拟过期时间
valWithEx1,_ := redis.String(client.Do("get","keyWithEx"))
fmt.Println("获取带过期时间的key,",valWithEx1)
// 是否存在指定key
exists,_ := client.Do("exists","k12")
flag,_:= redis.Int(exists,nil);
fmt.Println("是否存在指定key,",flag == 1)
}
// 列表操作
func ListOption(client redis.Conn) {
// del key
client.Do("del","list1")
// lpush list val
client.Do("lpush","list1","l1")
// rpush list val
client.Do("rpush","list1","l3")
// lrange list from end
list,_ := client.Do("lrange","list1","0","100")
l,_:= redis.Values(list,nil)
for _,val := range l{
fmt.Println(string(val.([]byte)))
}
}
总结
更多推荐
已为社区贡献2条内容
所有评论(0)