1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-23 13:42:25 +08:00

feat: improve package redis v9

This commit is contained in:
houseme
2023-08-22 10:51:29 +08:00
parent 26b0aacb4c
commit 6b764e9126
5 changed files with 77 additions and 146 deletions

22
cache/redis.go vendored
View File

@@ -4,7 +4,7 @@ import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/redis/go-redis/v9"
)
// Redis .redis cache
@@ -26,21 +26,21 @@ type RedisOpts struct {
// NewRedis 实例化
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
conn := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{opts.Host},
DB: opts.Database,
Password: opts.Password,
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
MinIdleConns: opts.MaxIdle,
Addrs: []string{opts.Host},
DB: opts.Database,
Password: opts.Password,
ConnMaxIdleTime: time.Second * time.Duration(opts.IdleTimeout),
MinIdleConns: opts.MaxIdle,
})
return &Redis{ctx: ctx, conn: conn}
}
// SetConn 设置conn
// SetConn 设置 conn
func (r *Redis) SetConn(conn redis.UniversalClient) {
r.conn = conn
}
// SetRedisCtx 设置redis ctx 参数
// SetRedisCtx 设置 redis ctx 参数
func (r *Redis) SetRedisCtx(ctx context.Context) {
r.ctx = ctx
}
@@ -66,15 +66,15 @@ func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
// SetContext 设置一个值
func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
return r.conn.SetEX(ctx, key, val, timeout).Err()
return r.conn.SetEx(ctx, key, val, timeout).Err()
}
// IsExist 判断key是否存在
// IsExist 判断 key 是否存在
func (r *Redis) IsExist(key string) bool {
return r.IsExistContext(r.ctx, key)
}
// IsExistContext 判断key是否存在
// IsExistContext 判断 key 是否存在
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
result, _ := r.conn.Exists(ctx, key).Result()