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.
gocache/redis.go

124 lines
4.0 KiB

2 years ago
package gocache
import (
"context"
4 months ago
"github.com/redis/go-redis/v9"
4 months ago
"go.dtapp.net/gojson"
2 years ago
"time"
)
2 years ago
// RedisConfig 配置
type RedisConfig struct {
4 months ago
DefaultExpiration time.Duration // 默认过期时间
Client *redis.Client // 驱动,可选
Debug bool // 调试,可选
2 years ago
}
2 years ago
// Redis https://github.com/go-redis/redis
type Redis struct {
4 months ago
config *RedisConfig // 配置
Client *redis.Client // 驱动
2 years ago
}
2 years ago
// NewRedis 实例化
2 years ago
func NewRedis(config *RedisConfig) *Redis {
2 years ago
c := &Redis{config: config}
c.Client = config.Client
return c
2 years ago
}
// Set 设置一个key的值
func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) {
r.setLog(key)
4 months ago
return r.Client.Set(context.Background(), key, value, expiration).Result()
2 years ago
}
2 years ago
// SetInterface 设置一个key的值
func (r *Redis) SetInterface(key string, value interface{}, expiration time.Duration) (string, error) {
r.setLog(key)
4 months ago
marshal, _ := gojson.Marshal(value)
4 months ago
return r.Client.Set(context.Background(), key, marshal, expiration).Result()
2 years ago
}
2 years ago
// SetDefaultExpiration 设置一个key的值使用全局默认过期时间
func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) {
r.setLog(key)
4 months ago
return r.Client.Set(context.Background(), key, value, r.config.DefaultExpiration).Result()
2 years ago
}
2 years ago
// SetInterfaceDefaultExpiration 设置一个key的值使用全局默认过期时间
func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) {
r.setLog(key)
4 months ago
marshal, _ := gojson.Marshal(value)
4 months ago
return r.Client.Set(context.Background(), key, marshal, r.config.DefaultExpiration).Result()
2 years ago
}
2 years ago
// Get 查询key的值
func (r *Redis) Get(key string) (string, error) {
r.getLog(key)
4 months ago
return r.Client.Get(context.Background(), key).Result()
2 years ago
}
2 years ago
// GetInterface 查询key的值
func (r *Redis) GetInterface(key string, result interface{}) error {
r.getLog(key)
4 months ago
ret, err := r.Client.Get(context.Background(), key).Result()
2 years ago
if err != nil {
return err
}
4 months ago
err = gojson.Unmarshal([]byte(ret), result)
2 years ago
return nil
}
2 years ago
// GetSet 设置一个key的值并返回这个key的旧值
func (r *Redis) GetSet(key string, value interface{}) (string, error) {
4 months ago
return r.Client.GetSet(context.Background(), key, value).Result()
2 years ago
}
// SetNX 如果key不存在则设置这个key的值
func (r *Redis) SetNX(key string, value interface{}, expiration time.Duration) error {
4 months ago
return r.Client.SetNX(context.Background(), key, value, expiration).Err()
2 years ago
}
// SetNXDefaultExpiration 如果key不存在则设置这个key的值使用全局默认过期时间
func (r *Redis) SetNXDefaultExpiration(key string, value interface{}) error {
4 months ago
return r.Client.SetNX(context.Background(), key, value, r.config.DefaultExpiration).Err()
2 years ago
}
// MGet 批量查询key的值
func (r *Redis) MGet(keys ...string) ([]interface{}, error) {
4 months ago
return r.Client.MGet(context.Background(), keys...).Result()
2 years ago
}
// MSet 批量设置key的值
// MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
func (r *Redis) MSet(values map[string]interface{}) error {
4 months ago
return r.Client.MSet(context.Background(), values).Err()
2 years ago
}
// Incr 针对一个key的数值进行递增操作
func (r *Redis) Incr(key string) (int64, error) {
4 months ago
return r.Client.Incr(context.Background(), key).Result()
2 years ago
}
// IncrBy 针对一个key的数值进行递增操作指定每次递增多少
func (r *Redis) IncrBy(key string, value int64) (int64, error) {
4 months ago
return r.Client.IncrBy(context.Background(), key, value).Result()
2 years ago
}
// Decr 针对一个key的数值进行递减操作
func (r *Redis) Decr(key string) (int64, error) {
4 months ago
return r.Client.Decr(context.Background(), key).Result()
2 years ago
}
// DecrBy 针对一个key的数值进行递减操作指定每次递减多少
func (r *Redis) DecrBy(key string, value int64) (int64, error) {
4 months ago
return r.Client.DecrBy(context.Background(), key, value).Result()
2 years ago
}
// Del 删除key操作支持批量删除
func (r *Redis) Del(keys ...string) error {
r.delLog(keys...)
4 months ago
return r.Client.Del(context.Background(), keys...).Err()
2 years ago
}