Golang学习笔记之使用Redis
前言
Redis包
文档:传送门
Demo
package main
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis"
)
var ctx = context.Background()
func main() {
client := getRedis()
// get
value, err := client.Get(ctx, "golang:test").Result()
if err != nil {
panic(err.Error())
}
fmt.Println(value)
// setNx
err = client.SetNX(ctx, "golang:test", "redis nx", time.Duration(2)*time.Second).Err()
if err != nil {
panic(err.Error())
}
// set
err = client.Set(ctx, "golang:test", "redis", time.Duration(10)*time.Minute).Err()
if err != nil {
panic(err.Error())
}
}
// getRedis redis.newClient
func getRedis() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
})
}
最后
还算简单
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!