|
|
@ -16,6 +16,7 @@ type RedisConfig struct {
|
|
|
|
DbName int // 数据库,可选
|
|
|
|
DbName int // 数据库,可选
|
|
|
|
DefaultExpiration time.Duration // 默认过期时间
|
|
|
|
DefaultExpiration time.Duration // 默认过期时间
|
|
|
|
Db *redis.Client // 驱动,可选
|
|
|
|
Db *redis.Client // 驱动,可选
|
|
|
|
|
|
|
|
Debug bool // 调试,可选
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Redis https://github.com/go-redis/redis
|
|
|
|
// Redis https://github.com/go-redis/redis
|
|
|
@ -55,33 +56,39 @@ func NewRedis(config *RedisConfig) *Redis {
|
|
|
|
|
|
|
|
|
|
|
|
// Set 设置一个key的值
|
|
|
|
// Set 设置一个key的值
|
|
|
|
func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) {
|
|
|
|
func (r *Redis) Set(key string, value interface{}, expiration time.Duration) (string, error) {
|
|
|
|
|
|
|
|
r.setLog(key)
|
|
|
|
return r.db.Set(r.ctx, key, value, expiration).Result()
|
|
|
|
return r.db.Set(r.ctx, key, value, expiration).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetInterface 设置一个key的值
|
|
|
|
// SetInterface 设置一个key的值
|
|
|
|
func (r *Redis) SetInterface(key string, value interface{}, expiration time.Duration) (string, error) {
|
|
|
|
func (r *Redis) SetInterface(key string, value interface{}, expiration time.Duration) (string, error) {
|
|
|
|
|
|
|
|
r.setLog(key)
|
|
|
|
marshal, _ := json.Marshal(value)
|
|
|
|
marshal, _ := json.Marshal(value)
|
|
|
|
return r.db.Set(r.ctx, key, marshal, expiration).Result()
|
|
|
|
return r.db.Set(r.ctx, key, marshal, expiration).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetDefaultExpiration 设置一个key的值,使用全局默认过期时间
|
|
|
|
// SetDefaultExpiration 设置一个key的值,使用全局默认过期时间
|
|
|
|
func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) {
|
|
|
|
func (r *Redis) SetDefaultExpiration(key string, value interface{}) (string, error) {
|
|
|
|
|
|
|
|
r.setLog(key)
|
|
|
|
return r.db.Set(r.ctx, key, value, r.DefaultExpiration).Result()
|
|
|
|
return r.db.Set(r.ctx, key, value, r.DefaultExpiration).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetInterfaceDefaultExpiration 设置一个key的值,使用全局默认过期时间
|
|
|
|
// SetInterfaceDefaultExpiration 设置一个key的值,使用全局默认过期时间
|
|
|
|
func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) {
|
|
|
|
func (r *Redis) SetInterfaceDefaultExpiration(key string, value interface{}) (string, error) {
|
|
|
|
|
|
|
|
r.setLog(key)
|
|
|
|
marshal, _ := json.Marshal(value)
|
|
|
|
marshal, _ := json.Marshal(value)
|
|
|
|
return r.db.Set(r.ctx, key, marshal, r.DefaultExpiration).Result()
|
|
|
|
return r.db.Set(r.ctx, key, marshal, r.DefaultExpiration).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get 查询key的值
|
|
|
|
// Get 查询key的值
|
|
|
|
func (r *Redis) Get(key string) (string, error) {
|
|
|
|
func (r *Redis) Get(key string) (string, error) {
|
|
|
|
|
|
|
|
r.getLog(key)
|
|
|
|
return r.db.Get(r.ctx, key).Result()
|
|
|
|
return r.db.Get(r.ctx, key).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetInterface 查询key的值
|
|
|
|
// GetInterface 查询key的值
|
|
|
|
func (r *Redis) GetInterface(key string, result interface{}) error {
|
|
|
|
func (r *Redis) GetInterface(key string, result interface{}) error {
|
|
|
|
|
|
|
|
r.getLog(key)
|
|
|
|
ret, err := r.db.Get(r.ctx, key).Result()
|
|
|
|
ret, err := r.db.Get(r.ctx, key).Result()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -138,5 +145,6 @@ func (r *Redis) DecrBy(key string, value int64) (int64, error) {
|
|
|
|
|
|
|
|
|
|
|
|
// Del 删除key操作,支持批量删除
|
|
|
|
// Del 删除key操作,支持批量删除
|
|
|
|
func (r *Redis) Del(keys ...string) error {
|
|
|
|
func (r *Redis) Del(keys ...string) error {
|
|
|
|
|
|
|
|
r.delLog(keys...)
|
|
|
|
return r.db.Del(r.ctx, keys...).Err()
|
|
|
|
return r.db.Del(r.ctx, keys...).Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|