49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package global
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-redis/redis/v8"
|
|
"time"
|
|
)
|
|
|
|
// RedisClient Redis连接对象
|
|
type RedisClient struct {
|
|
Client *redis.Client
|
|
}
|
|
|
|
var ctx = context.Background()
|
|
|
|
// GetData 获取数据
|
|
func (r *RedisClient) GetData(key string) (string, error) {
|
|
return r.Client.Get(ctx, key).Result()
|
|
}
|
|
|
|
// GetKeys 获取key列表
|
|
func (r *RedisClient) GetKeys(key string) ([]string, error) {
|
|
return r.Client.Keys(ctx, key).Result()
|
|
}
|
|
|
|
// ExistKey 判断是否存在key
|
|
func (r *RedisClient) ExistKey(key string) bool {
|
|
keys, err := r.GetKeys(key)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return len(keys) > 0
|
|
}
|
|
|
|
// Set 保存数据
|
|
func (r *RedisClient) Set(key string, value string) error {
|
|
return r.Client.Set(ctx, key, value, 0).Err()
|
|
}
|
|
|
|
// SetWithTimeout 保存带过期时间的数据
|
|
func (r *RedisClient) SetWithTimeout(key string, value string, timeout time.Duration) error {
|
|
return r.Client.Set(ctx, key, value, timeout).Err()
|
|
}
|
|
|
|
// Del 根据key删除Redis数据
|
|
func (r *RedisClient) Del(key string) error {
|
|
return r.Client.Del(ctx, key).Err()
|
|
}
|