GoTest/pkg/cache/redis.go
2025-11-29 03:27:19 +08:00

188 lines
4.4 KiB
Go

package cache
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"yinli-api/pkg/config"
"github.com/go-redis/redis/v8"
)
var RedisClient *redis.Client
var ctx = context.Background()
// InitRedis 初始化Redis连接
func InitRedis(cfg *config.Config) error {
RedisClient = redis.NewClient(&redis.Options{
Addr: cfg.GetRedisAddr(),
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
PoolSize: cfg.Redis.PoolSize,
})
// 测试连接
_, err := RedisClient.Ping(ctx).Result()
if err != nil {
return fmt.Errorf("Redis连接失败: %w", err)
}
log.Println("Redis连接成功")
return nil
}
// CloseRedis 关闭Redis连接
func CloseRedis() error {
if RedisClient != nil {
return RedisClient.Close()
}
return nil
}
// Set 设置键值对
func Set(key string, value interface{}, expiration time.Duration) error {
jsonValue, err := json.Marshal(value)
if err != nil {
return err
}
return RedisClient.Set(ctx, key, jsonValue, expiration).Err()
}
// Get 获取值
func Get(key string, dest interface{}) error {
val, err := RedisClient.Get(ctx, key).Result()
if err != nil {
return err
}
return json.Unmarshal([]byte(val), dest)
}
// GetString 获取字符串值
func GetString(key string) (string, error) {
return RedisClient.Get(ctx, key).Result()
}
// SetString 设置字符串值
func SetString(key, value string, expiration time.Duration) error {
return RedisClient.Set(ctx, key, value, expiration).Err()
}
// Delete 删除键
func Delete(key string) error {
return RedisClient.Del(ctx, key).Err()
}
// Exists 检查键是否存在
func Exists(key string) (bool, error) {
count, err := RedisClient.Exists(ctx, key).Result()
return count > 0, err
}
// Expire 设置过期时间
func Expire(key string, expiration time.Duration) error {
return RedisClient.Expire(ctx, key, expiration).Err()
}
// TTL 获取剩余过期时间
func TTL(key string) (time.Duration, error) {
return RedisClient.TTL(ctx, key).Result()
}
// Incr 递增
func Incr(key string) (int64, error) {
return RedisClient.Incr(ctx, key).Result()
}
// IncrBy 按指定值递增
func IncrBy(key string, value int64) (int64, error) {
return RedisClient.IncrBy(ctx, key, value).Result()
}
// Decr 递减
func Decr(key string) (int64, error) {
return RedisClient.Decr(ctx, key).Result()
}
// DecrBy 按指定值递减
func DecrBy(key string, value int64) (int64, error) {
return RedisClient.DecrBy(ctx, key, value).Result()
}
// HSet 设置哈希字段
func HSet(key, field string, value interface{}) error {
return RedisClient.HSet(ctx, key, field, value).Err()
}
// HGet 获取哈希字段
func HGet(key, field string) (string, error) {
return RedisClient.HGet(ctx, key, field).Result()
}
// HGetAll 获取所有哈希字段
func HGetAll(key string) (map[string]string, error) {
return RedisClient.HGetAll(ctx, key).Result()
}
// HDel 删除哈希字段
func HDel(key string, fields ...string) error {
return RedisClient.HDel(ctx, key, fields...).Err()
}
// SAdd 添加集合成员
func SAdd(key string, members ...interface{}) error {
return RedisClient.SAdd(ctx, key, members...).Err()
}
// SMembers 获取集合所有成员
func SMembers(key string) ([]string, error) {
return RedisClient.SMembers(ctx, key).Result()
}
// SIsMember 检查是否为集合成员
func SIsMember(key string, member interface{}) (bool, error) {
return RedisClient.SIsMember(ctx, key, member).Result()
}
// SRem 移除集合成员
func SRem(key string, members ...interface{}) error {
return RedisClient.SRem(ctx, key, members...).Err()
}
// ZAdd 添加有序集合成员
func ZAdd(key string, score float64, member interface{}) error {
return RedisClient.ZAdd(ctx, key, &redis.Z{Score: score, Member: member}).Err()
}
// ZRange 获取有序集合范围内的成员
func ZRange(key string, start, stop int64) ([]string, error) {
return RedisClient.ZRange(ctx, key, start, stop).Result()
}
// ZRem 移除有序集合成员
func ZRem(key string, members ...interface{}) error {
return RedisClient.ZRem(ctx, key, members...).Err()
}
// Keys 获取匹配模式的所有键
func Keys(pattern string) ([]string, error) {
return RedisClient.Keys(ctx, pattern).Result()
}
// FlushDB 清空当前数据库
func FlushDB() error {
return RedisClient.FlushDB(ctx).Err()
}
// Ping 测试连接
func Ping() error {
return RedisClient.Ping(ctx).Err()
}
// GetClient 获取Redis客户端实例
func GetClient() *redis.Client {
return RedisClient
}