126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
|
|
package utils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"ai_xhs/database"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SetCache 设置缓存
|
||
|
|
func SetCache(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
|
||
|
|
data, err := json.Marshal(value)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return database.RDB.Set(ctx, key, data, expiration).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetCache 获取缓存
|
||
|
|
func GetCache(ctx context.Context, key string, dest interface{}) error {
|
||
|
|
data, err := database.RDB.Get(ctx, key).Bytes()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return json.Unmarshal(data, dest)
|
||
|
|
}
|
||
|
|
|
||
|
|
// DelCache 删除缓存
|
||
|
|
func DelCache(ctx context.Context, keys ...string) error {
|
||
|
|
return database.RDB.Del(ctx, keys...).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// ExistsCache 检查缓存是否存在
|
||
|
|
func ExistsCache(ctx context.Context, key string) (bool, error) {
|
||
|
|
count, err := database.RDB.Exists(ctx, key).Result()
|
||
|
|
if err != nil {
|
||
|
|
return false, err
|
||
|
|
}
|
||
|
|
return count > 0, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ExpireCache 设置缓存过期时间
|
||
|
|
func ExpireCache(ctx context.Context, key string, expiration time.Duration) error {
|
||
|
|
return database.RDB.Expire(ctx, key, expiration).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetTTL 获取缓存剩余生存时间
|
||
|
|
func GetTTL(ctx context.Context, key string) (time.Duration, error) {
|
||
|
|
return database.RDB.TTL(ctx, key).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// IncrCache 递增计数器
|
||
|
|
func IncrCache(ctx context.Context, key string) (int64, error) {
|
||
|
|
return database.RDB.Incr(ctx, key).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// DecrCache 递减计数器
|
||
|
|
func DecrCache(ctx context.Context, key string) (int64, error) {
|
||
|
|
return database.RDB.Decr(ctx, key).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetCacheNX 设置缓存(仅当key不存在时)
|
||
|
|
func SetCacheNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error) {
|
||
|
|
data, err := json.Marshal(value)
|
||
|
|
if err != nil {
|
||
|
|
return false, err
|
||
|
|
}
|
||
|
|
return database.RDB.SetNX(ctx, key, data, expiration).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// HSetCache 设置哈希字段
|
||
|
|
func HSetCache(ctx context.Context, key, field string, value interface{}) error {
|
||
|
|
return database.RDB.HSet(ctx, key, field, value).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// HGetCache 获取哈希字段
|
||
|
|
func HGetCache(ctx context.Context, key, field string) (string, error) {
|
||
|
|
return database.RDB.HGet(ctx, key, field).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// HGetAllCache 获取哈希所有字段
|
||
|
|
func HGetAllCache(ctx context.Context, key string) (map[string]string, error) {
|
||
|
|
return database.RDB.HGetAll(ctx, key).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// HDelCache 删除哈希字段
|
||
|
|
func HDelCache(ctx context.Context, key string, fields ...string) error {
|
||
|
|
return database.RDB.HDel(ctx, key, fields...).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// SAddCache 添加集合成员
|
||
|
|
func SAddCache(ctx context.Context, key string, members ...interface{}) error {
|
||
|
|
return database.RDB.SAdd(ctx, key, members...).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// SMembersCache 获取集合所有成员
|
||
|
|
func SMembersCache(ctx context.Context, key string) ([]string, error) {
|
||
|
|
return database.RDB.SMembers(ctx, key).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// SRemCache 删除集合成员
|
||
|
|
func SRemCache(ctx context.Context, key string, members ...interface{}) error {
|
||
|
|
return database.RDB.SRem(ctx, key, members...).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// ZAddCache 添加有序集合成员
|
||
|
|
func ZAddCache(ctx context.Context, key string, score float64, member interface{}) error {
|
||
|
|
z := redis.Z{
|
||
|
|
Score: score,
|
||
|
|
Member: member,
|
||
|
|
}
|
||
|
|
return database.RDB.ZAdd(ctx, key, z).Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
// ZRangeCache 获取有序集合指定范围成员
|
||
|
|
func ZRangeCache(ctx context.Context, key string, start, stop int64) ([]string, error) {
|
||
|
|
return database.RDB.ZRange(ctx, key, start, stop).Result()
|
||
|
|
}
|
||
|
|
|
||
|
|
// ZRemCache 删除有序集合成员
|
||
|
|
func ZRemCache(ctx context.Context, key string, members ...interface{}) error {
|
||
|
|
return database.RDB.ZRem(ctx, key, members...).Err()
|
||
|
|
}
|