|
|
@@ -3,29 +3,31 @@ package redis
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
+
|
|
|
"github.com/runningwater/gohub/pkg/logger"
|
|
|
)
|
|
|
|
|
|
-// RedisClient Redis 服务
|
|
|
-type RedisClient struct {
|
|
|
+// Client Redis 服务
|
|
|
+type Client struct {
|
|
|
Client *redis.Client
|
|
|
Context context.Context
|
|
|
}
|
|
|
|
|
|
// Ping 测试 Redis 连接
|
|
|
// 通过 Ping 方法测试 Redis 连接是否正常
|
|
|
-func (rds *RedisClient) Ping() error {
|
|
|
+func (rds *Client) Ping() error {
|
|
|
_, err := rds.Client.Ping(rds.Context).Result()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
// Set 设置 Redis 键值对, 并设置过期时间
|
|
|
-func (rds *RedisClient) Set(key string, value any, expiraction time.Duration) bool {
|
|
|
- if err := rds.Client.Set(rds.Context, key, value, expiraction).Err(); err != nil {
|
|
|
+func (rds *Client) Set(key string, value any, expiration time.Duration) bool {
|
|
|
+ if err := rds.Client.Set(rds.Context, key, value, expiration).Err(); err != nil {
|
|
|
logger.ErrorString("Redis", "Set", err.Error())
|
|
|
return false
|
|
|
}
|
|
|
@@ -33,7 +35,7 @@ func (rds *RedisClient) Set(key string, value any, expiraction time.Duration) bo
|
|
|
}
|
|
|
|
|
|
// Get 获取 Redis 键值对
|
|
|
-func (rds *RedisClient) Get(key string) string {
|
|
|
+func (rds *Client) Get(key string) string {
|
|
|
val, err := rds.Client.Get(rds.Context, key).Result()
|
|
|
if err != nil {
|
|
|
logger.ErrorString("Redis", "Get", err.Error())
|
|
|
@@ -43,10 +45,10 @@ func (rds *RedisClient) Get(key string) string {
|
|
|
}
|
|
|
|
|
|
// Has 检查 Redis 中是否存在某个键
|
|
|
-func (rds *RedisClient) Has(key string) bool {
|
|
|
+func (rds *Client) Has(key string) bool {
|
|
|
_, err := rds.Client.Get(rds.Context, key).Result()
|
|
|
if err != nil {
|
|
|
- if err == redis.Nil {
|
|
|
+ if errors.Is(err, redis.Nil) {
|
|
|
logger.ErrorString("Redis", "Has", err.Error())
|
|
|
}
|
|
|
return false
|
|
|
@@ -55,7 +57,7 @@ func (rds *RedisClient) Has(key string) bool {
|
|
|
}
|
|
|
|
|
|
// Del 删除 Redis 中的键, 支持多个 key 传参
|
|
|
-func (rds *RedisClient) Del(keys ...string) bool {
|
|
|
+func (rds *Client) Del(keys ...string) bool {
|
|
|
if err := rds.Client.Del(rds.Context, keys...).Err(); err != nil {
|
|
|
logger.ErrorString("Redis", "Del", err.Error())
|
|
|
return false
|
|
|
@@ -64,7 +66,7 @@ func (rds *RedisClient) Del(keys ...string) bool {
|
|
|
}
|
|
|
|
|
|
// FlushDb 清空 Redis 数据库
|
|
|
-func (rds *RedisClient) FlushDb() bool {
|
|
|
+func (rds *Client) FlushDb() bool {
|
|
|
if err := rds.Client.FlushDB(rds.Context).Err(); err != nil {
|
|
|
logger.ErrorString("Redis", "FlushDb", err.Error())
|
|
|
return false
|
|
|
@@ -74,7 +76,7 @@ func (rds *RedisClient) FlushDb() bool {
|
|
|
|
|
|
// Increment 当参数只有 1 个时,增加 Redis 中的键值对增加 1
|
|
|
// 当参数有 2 个时,第一个参数为 Key,第二个参数为增加的值 int64 类型
|
|
|
-func (rds *RedisClient) Increment(args ...any) bool {
|
|
|
+func (rds *Client) Increment(args ...any) bool {
|
|
|
switch len(args) {
|
|
|
case 1:
|
|
|
key := args[0].(string)
|
|
|
@@ -98,7 +100,7 @@ func (rds *RedisClient) Increment(args ...any) bool {
|
|
|
|
|
|
// Decrement 当参数只有 1 个时,减少 Redis 中的键值对减少 1
|
|
|
// 当参数有 2 个时,第一个参数为 Key,第二个参数为减少的值 int64 类型
|
|
|
-func (rds *RedisClient) Decrement(args ...any) bool {
|
|
|
+func (rds *Client) Decrement(args ...any) bool {
|
|
|
switch len(args) {
|
|
|
case 1:
|
|
|
key := args[0].(string)
|
|
|
@@ -151,7 +153,7 @@ func (rds *RedisClient) Decrement(args ...any) bool {
|
|
|
var once sync.Once
|
|
|
|
|
|
// Redis 全局 Redis, 使用 db 1
|
|
|
-var Redis *RedisClient
|
|
|
+var Redis *Client
|
|
|
|
|
|
// ConnectRedis 连接 redis 数据库,设置全局 Redis 对象
|
|
|
func ConnectRedis(address, username, password string, db int) {
|
|
|
@@ -160,9 +162,9 @@ func ConnectRedis(address, username, password string, db int) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-func NewClient(address, username, password string, db int) *RedisClient {
|
|
|
+func NewClient(address, username, password string, db int) *Client {
|
|
|
|
|
|
- rds := &RedisClient{}
|
|
|
+ rds := &Client{}
|
|
|
rds.Context = context.Background()
|
|
|
|
|
|
// 使用 redis 库里的 NewClient 初始化连接
|