安装go-redis包:
go get github.com/go-redis/redis
使用:
package cache
import (
"fmt"
"github.com/go-redis/redis"
"nuxt-blog-api/configs"
"strconv"
"time"
)
var RedisClient *redis.Client
func init() {
RedisClient = redis.NewClient(&redis.Options{
Addr: configs.Configs.Cache.Host + ":" +strconv.Itoa(configs.Configs.Cache.Port), // 地址 + 端口
Password: configs.Configs.Cache.Pwd, // 密码
DB: configs.Configs.Cache.Select, // 选中db
})
pong, err := RedisClient.Ping().Result()
if err != nil {
fmt.Println(pong, err)
panic(err)
}
}
type Redis struct {
}
func (r Redis) Set(key string, val string, exp time.Duration) bool {
err := RedisClient.Set(key, val, exp).Err()
if err != nil {
return false
}
return true
}
func (r Redis) Get(key string) string {
val, err := RedisClient.Get(key).Result()
if err != nil {
return ""
}
return val
}
func (r Redis) Del(key string) bool {
err := RedisClient.Del(key).Err()
if err != nil {
return false
}
return true
}
var RedisCache = new(Redis)
// 设置,24小时有效期
bool := cache.RedisCache.Set("xxx", "123", 24 * time.Hour)
// 获取
str := cache.RedisCache.Get("xxx")
// 删除
bool := cache.RedisCache.Del("xxx")