You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.6 KiB

package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
"time"
)
func main() {
// 设置获取
//setGet()
// 递增、递减、追加
//appendIncrDecr()
// 子串操作
sub()
}
// 子串操作
func sub() {
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
if err != nil {
panic(err)
}
client := redis.NewClient(opt)
ctx := context.Background()
client.Set(ctx, "name", "MaShiBing", 0)
// GetRange
//fmt.Println(client.GetRange(ctx, "name", 0, 4).Result())
//fmt.Println(client.GetRange(ctx, "name", -4, -1).Result())
//fmt.Println(client.GetRange(ctx, "name", -4, 16).Result())
//fmt.Println(client.GetRange(ctx, "name", -16, 16).Result())
//// 全部内容
//fmt.Println(client.GetRange(ctx, "name", 0, -1).Result())
// SetRange
//client.SetRange(ctx, "name", 0, "Go")
//fmt.Println(client.Get(ctx, "name").Result())
//client.SetRange(ctx, "name", 4, "Go")
//fmt.Println(client.Get(ctx, "name").Result())
client.SetRange(ctx, "name", 4, "GoRedisGo")
fmt.Println(client.Get(ctx, "name").Result())
}
// 递增、递减、追加
func appendIncrDecr() {
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
if err != nil {
panic(err)
}
client := redis.NewClient(opt)
ctx := context.Background()
// append
//client.Set(ctx, "name", "Ma", 0)
//// "Ma" + "Shi"
//client.Append(ctx, "name", "Shi")
//status := client.Append(ctx, "name", "Bing")
//fmt.Println(status.Result())
//result := client.Get(ctx, "name")
//fmt.Println(result.Result())
// incr, decr
client.Set(ctx, "counter", "0", 0)
client.Incr(ctx, "counter")
client.Incr(ctx, "counter")
client.Incr(ctx, "counter")
result := client.Get(ctx, "counter")
fmt.Println(result.Result())
client.IncrBy(ctx, "counter", 3)
client.IncrBy(ctx, "counter", 3)
fmt.Println(client.Get(ctx, "counter").Result())
client.Decr(ctx, "counter")
client.Decr(ctx, "counter")
fmt.Println(client.Get(ctx, "counter").Result())
client.DecrBy(ctx, "counter", 3)
client.DecrBy(ctx, "counter", 3)
fmt.Println(client.Get(ctx, "counter").Result())
}
// 设置获取
func setGet() {
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
if err != nil {
panic(err)
}
client := redis.NewClient(opt)
ctx := context.Background()
// 设置
//status := client.Set(ctx, "name", "", 3*time.Second)
// 有效期
// 0 表示永不过期
//status := client.Set(ctx, "name", "MaShiBing", 0)
// KeepTTL -1,表示保持原有的有效期
// 仅仅需要修改值,而不去更新有效期的话
//status := client.Set(ctx, "name", "MaShiBing", redis.KeepTTL)
// 设置条件
// NX, key 不存在时设置
// XX, key 存在时设置
status := client.SetArgs(ctx, "name", "mashibing", redis.SetArgs{
// 不存在才设置
//Mode: "NX",
// 存在时才设置
Mode: "XX",
// Mode "" 表示存在则更新,不存在则添加,是默认的模式
// 有效期,时间周期
TTL: 0,
// 有效期,时间点
ExpireAt: time.Time{},
// 是否返回原有值
Get: false,
// 是否保持原有有效期
KeepTTL: false,
})
fmt.Println(status.Result())
// 获取
// 执行命令
//result := client.Get(ctx, "name")
//// 获取结果,以及命令的执行结果(是否有错误)
//val, err := result.Result()
//
//// 判定 key 是否存在(错误)
//if err == redis.Nil {
// fmt.Println("key not exists")
//} else if err != nil {
// fmt.Println(err)
//} else if val == "" {
// fmt.Println("value is empty")
//} else {
// fmt.Println("get value:", val)
//}
}