代码拉取完成,页面将自动刷新
/*
* @Author: i@douxuefeng.cn
* @Date: 2021-10-31 16:32:16
* @LastEditTime: 2022-05-28 14:23:07
* @LastEditors: i@douxuefeng.cn
* @Description:
* @FilePath: \go-utils\cache\redis.go
*/
package cache
import (
"time"
"github.com/gomodule/redigo/redis"
)
type redisPool struct {
Pool *redis.Pool
}
type RedisOpt struct {
Host string
Password string
Database int
MaxIdle int
MaxActive int
IdleTimeout int //second
}
var client *redisPool
func InitRedis(opt *RedisOpt) {
client = new(redisPool)
client.Pool = &redis.Pool{
MaxIdle: opt.MaxIdle,
MaxActive: opt.MaxActive,
IdleTimeout: time.Duration(opt.IdleTimeout) * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", opt.Host,
redis.DialPassword(opt.Password),
redis.DialDatabase(opt.Database),
redis.DialConnectTimeout(time.Duration(opt.IdleTimeout)*time.Second),
redis.DialReadTimeout(time.Duration(opt.IdleTimeout)*time.Second),
redis.DialWriteTimeout(time.Duration(opt.IdleTimeout)*time.Second))
},
}
}
func NewRedisConn() *redisPool {
return client
}
func (r *redisPool) Get(key string) (string, error) {
conn := r.Pool.Get()
defer conn.Close()
return redis.String(conn.Do("GET", key))
}
//Set 设置一个过期时间值
func (r *redisPool) Set(key string, data string, timeout int64) error {
conn := r.Pool.Get()
defer conn.Close()
_, err := conn.Do("SETEX", key, timeout, data)
return err
}
func (r *redisPool) IsExist(key string) bool {
conn := r.Pool.Get()
defer conn.Close()
a, _ := redis.Bool(conn.Do("EXISTS", key))
return a
}
func (r *redisPool) Del(key string) error {
conn := r.Pool.Get()
defer conn.Close()
if _, err := conn.Do("DEL", key); err != nil {
return err
}
return nil
}
// hash
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。