You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dorm/redis_curd.go

159 lines
4.5 KiB

2 years ago
package dorm
import (
"context"
4 months ago
"github.com/redis/go-redis/v9"
2 years ago
"time"
)
// Set 设置一个key的值
2 years ago
func (r *RedisClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
4 months ago
return r.db.Set(ctx, key, value, expiration)
2 years ago
}
// Get 查询key的值
2 years ago
func (r *RedisClient) Get(ctx context.Context, key string) *redis.StringCmd {
4 months ago
return r.db.Get(ctx, key)
2 years ago
}
// GetSet 设置一个key的值并返回这个key的旧值
2 years ago
func (r *RedisClient) GetSet(ctx context.Context, key string, value interface{}) *redis.StringCmd {
4 months ago
return r.db.GetSet(ctx, key, value)
2 years ago
}
// SetNX 如果key不存在则设置这个key的值
2 years ago
func (r *RedisClient) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd {
4 months ago
return r.db.SetNX(ctx, key, value, expiration)
2 years ago
}
// MGet 批量查询key的值
2 years ago
func (r *RedisClient) MGet(ctx context.Context, keys ...string) *redis.SliceCmd {
4 months ago
return r.db.MGet(ctx, keys...)
2 years ago
}
// MSet 批量设置key的值
// MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
2 years ago
func (r *RedisClient) MSet(ctx context.Context, values map[string]interface{}) *redis.StatusCmd {
4 months ago
return r.db.MSet(ctx, values)
2 years ago
}
// Incr 针对一个key的数值进行递增操作
2 years ago
func (r *RedisClient) Incr(ctx context.Context, key string) *redis.IntCmd {
4 months ago
return r.db.Incr(ctx, key)
2 years ago
}
// IncrBy 针对一个key的数值进行递增操作指定每次递增多少
2 years ago
func (r *RedisClient) IncrBy(ctx context.Context, key string, value int64) *redis.IntCmd {
4 months ago
return r.db.IncrBy(ctx, key, value)
2 years ago
}
// Decr 针对一个key的数值进行递减操作
2 years ago
func (r *RedisClient) Decr(ctx context.Context, key string) *redis.IntCmd {
4 months ago
return r.db.Decr(ctx, key)
2 years ago
}
// DecrBy 针对一个key的数值进行递减操作指定每次递减多少
2 years ago
func (r *RedisClient) DecrBy(ctx context.Context, key string, value int64) *redis.IntCmd {
4 months ago
return r.db.DecrBy(ctx, key, value)
2 years ago
}
// Del 删除key操作支持批量删除
2 years ago
func (r *RedisClient) Del(ctx context.Context, keys ...string) *redis.IntCmd {
4 months ago
return r.db.Del(ctx, keys...)
2 years ago
}
// Keys 按前缀获取所有key名
func (r *RedisClient) Keys(ctx context.Context, prefix string) []string {
4 months ago
values, _ := r.db.Keys(ctx, prefix).Result()
keys := make([]string, 0, len(values))
if len(values) <= 0 {
return keys
}
for _, value := range values {
keys = append(keys, value)
}
return keys
}
// KeysValue 按前缀获取所有key值
func (r *RedisClient) KeysValue(ctx context.Context, prefix string) *redis.SliceCmd {
4 months ago
values, _ := r.db.Keys(ctx, prefix).Result()
2 years ago
if len(values) <= 0 {
return &redis.SliceCmd{}
}
2 years ago
keys := make([]string, 0, len(values))
for _, value := range values {
keys = append(keys, value)
}
2 years ago
return r.MGet(ctx, keys...)
2 years ago
}
4 months ago
// AddKeyToSet 将值添加到集合
func (r *RedisClient) AddKeyToSet(ctx context.Context, setKey string, value interface{}) error {
return r.db.SAdd(ctx, setKey, value).Err()
}
// GetAllKeysInSet 获取集合的所有元素
func (r *RedisClient) GetAllKeysInSet(ctx context.Context, setKey string) ([]string, error) {
return r.db.SMembers(ctx, setKey).Result()
}
// DoesKeyExistInSet 检查值是否存在于集合中
func (r *RedisClient) DoesKeyExistInSet(ctx context.Context, setKey string, targetKey interface{}) (bool, error) {
return r.db.SIsMember(ctx, setKey, targetKey).Result()
}
// RemoveKeyFromSet 从集合中删除指定的元素
func (r *RedisClient) RemoveKeyFromSet(ctx context.Context, setKey string, targetKey interface{}) error {
return r.db.SRem(ctx, setKey, targetKey).Err()
}
// DeleteKeysWithPrefix 根据前缀删除key
func (r *RedisClient) DeleteKeysWithPrefix(ctx context.Context, prefix string) error {
// 获取所有符合给定模式的键
keys, err := r.db.Keys(ctx, prefix+"*").Result()
if err != nil {
return err
}
// 删除所有匹配的键
if len(keys) > 0 {
_, err := r.db.Del(ctx, keys...).Result()
if err != nil {
return err
}
}
return nil
}
// DeleteScanWithPrefix 根据前缀删除key
func (r *RedisClient) DeleteScanWithPrefix(ctx context.Context, prefix string) error {
var cursor uint64
for {
// 使用 SCAN 迭代获取匹配模式的键
keys, nextCursor, err := r.db.Scan(ctx, cursor, prefix+"*", 10).Result()
if err != nil {
return err
}
// 删除当前迭代返回的键
if len(keys) > 0 {
_, err := r.db.Del(ctx, keys...).Result()
if err != nil {
return err
}
}
// 更新游标,继续迭代
cursor = nextCursor
// 如果迭代结束,退出循环
if cursor == 0 {
break
}
}
return nil
}