From c58e6fe7612f2e5575f8ad9a04c35a3a84da1406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 11 Aug 2022 21:57:16 +0800 Subject: [PATCH] - update redis --- const.go | 2 +- redis_curd.go | 73 +++++++++++++++++++++++++++++++++++++++++++ redis_curd_channel.go | 31 ++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 redis_curd.go create mode 100644 redis_curd_channel.go diff --git a/const.go b/const.go index 20d0f6a..057c1bf 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package dorm -const Version = "1.0.14" +const Version = "1.0.15" diff --git a/redis_curd.go b/redis_curd.go new file mode 100644 index 0000000..e378bdc --- /dev/null +++ b/redis_curd.go @@ -0,0 +1,73 @@ +package dorm + +import ( + "context" + "github.com/go-redis/redis/v9" + "time" +) + +// Set 设置一个key的值 +func (c *RedisClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd { + return c.Db.Set(ctx, key, value, expiration) +} + +// Get 查询key的值 +func (c *RedisClient) Get(ctx context.Context, key string) *redis.StringCmd { + return c.Db.Get(ctx, key) +} + +// GetSet 设置一个key的值,并返回这个key的旧值 +func (c *RedisClient) GetSet(ctx context.Context, key string, value interface{}) *redis.StringCmd { + return c.Db.GetSet(ctx, key, value) +} + +// SetNX 如果key不存在,则设置这个key的值 +func (c *RedisClient) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd { + return c.Db.SetNX(ctx, key, value, expiration) +} + +// MGet 批量查询key的值 +func (c *RedisClient) MGet(ctx context.Context, keys ...string) *redis.SliceCmd { + return c.Db.MGet(ctx, keys...) +} + +// MSet 批量设置key的值 +// MSet(map[string]interface{}{"key1": "value1", "key2": "value2"}) +func (c *RedisClient) MSet(ctx context.Context, values map[string]interface{}) *redis.StatusCmd { + return c.Db.MSet(ctx, values) +} + +// Incr 针对一个key的数值进行递增操作 +func (c *RedisClient) Incr(ctx context.Context, key string) *redis.IntCmd { + return c.Db.Incr(ctx, key) +} + +// IncrBy 针对一个key的数值进行递增操作,指定每次递增多少 +func (c *RedisClient) IncrBy(ctx context.Context, key string, value int64) *redis.IntCmd { + return c.Db.IncrBy(ctx, key, value) +} + +// Decr 针对一个key的数值进行递减操作 +func (c *RedisClient) Decr(ctx context.Context, key string) *redis.IntCmd { + return c.Db.Decr(ctx, key) +} + +// DecrBy 针对一个key的数值进行递减操作,指定每次递减多少 +func (c *RedisClient) DecrBy(ctx context.Context, key string, value int64) *redis.IntCmd { + return c.Db.DecrBy(ctx, key, value) +} + +// Del 删除key操作,支持批量删除 +func (c *RedisClient) Del(ctx context.Context, keys ...string) *redis.IntCmd { + return c.Db.Del(ctx, keys...) +} + +// Keys 按前缀获取所有 key +func (c *RedisClient) Keys(ctx context.Context, prefix string) *redis.SliceCmd { + values, _ := c.Db.Keys(ctx, prefix).Result() + keys := make([]string, 0, len(values)) + for _, value := range values { + keys = append(keys, value) + } + return c.MGet(ctx, keys...) +} diff --git a/redis_curd_channel.go b/redis_curd_channel.go new file mode 100644 index 0000000..1049159 --- /dev/null +++ b/redis_curd_channel.go @@ -0,0 +1,31 @@ +package dorm + +import ( + "context" + "github.com/go-redis/redis/v9" +) + +// Subscribe 订阅channel +func (c *RedisClient) Subscribe(ctx context.Context, channels ...string) *redis.PubSub { + return c.Db.Subscribe(ctx, channels...) +} + +// PSubscribe 订阅channel支持通配符匹配 +func (c *RedisClient) PSubscribe(ctx context.Context, channels ...string) *redis.PubSub { + return c.Db.PSubscribe(ctx, channels...) +} + +// Publish 将信息发送到指定的channel +func (c *RedisClient) Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd { + return c.Db.Publish(ctx, channel, message) +} + +// PubSubChannels 查询活跃的channel +func (c *RedisClient) PubSubChannels(ctx context.Context, pattern string) *redis.StringSliceCmd { + return c.Db.PubSubChannels(ctx, pattern) +} + +// PubSubNumSub 查询指定的channel有多少个订阅者 +func (c *RedisClient) PubSubNumSub(ctx context.Context, channels ...string) *redis.StringIntMapCmd { + return c.Db.PubSubNumSub(ctx, channels...) +}