go-redis数据库操作
package models
import (
"github.com/redis/go-redis/v9"
"context"
)
var RDB0 = Redis_init()
func Redis_init() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
return client
}
func main(){
err = RDB0.Set(ctx, form.Email, code, time.Minute*10).Err()
if err != nil {
c.JSON(400, gin.H{"msg":err})
panic(err)
}
ttl, err := RDB0.TTL(ctx, form.Email).Result()
if err != nil {
c.JSON(400, gin.H{"msg":err})
}
val, _ := RDB0.Get(ctx,email).Result()
exists, err := redisClient.Exists(ctx, key).Result()
if err != nil {
panic(err)
}
if exists > 0 {
fmt.Println("键存在")
} else {
fmt.Println("键不存在")
}
}