2 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
redis_impl.go 9.10 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2025-05-23 13:41 +08:00 . optimize error module
package redis
import (
"context"
"strings"
"time"
"gitee.com/carlmax_my/console-core-go/pkg/cache"
"gitee.com/carlmax_my/console-core-go/pkg/timeutil"
"gitee.com/carlmax_my/console-core-go/pkg/trace"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
)
// Set set some <key,value> into redis
func (c *cacheRepo) Set(key string, value interface{}, ttl time.Duration, options ...cache.Option) error {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "set"
opt.Redis.Key = key
opt.Redis.Value = value
opt.Redis.TTL = ttl.Minutes()
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
if err := c.client.Set(ctx, key, value, ttl).Err(); err != nil {
return errors.Wrapf(err, "redis set key: %s err", key)
}
return nil
}
func (c *cacheRepo) SetT(key string, value interface{}, ttl time.Duration, trace trace.T) error {
return c.Set(key, value, ttl, cache.WithTrace(trace))
}
func (c *cacheRepo) GetT(key string, trace trace.T) (string, error) {
return c.Get(key, cache.WithTrace(trace))
}
// Get get some key from redis
func (c *cacheRepo) Get(key string, options ...cache.Option) (string, error) {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "get"
opt.Redis.Key = key
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
value, err := c.client.Get(ctx, key).Result()
if err != nil {
return "", errors.Wrapf(err, "redis get key: %s err", key)
}
return value, nil
}
func (c *cacheRepo) CheckGet(key string, options ...cache.Option) (string, error) {
b, err := c.Exists(key)
if err != nil {
return "", err
}
if !b {
return "", errors.New("key not exist")
}
return c.Get(key, options...)
}
func (c *cacheRepo) CheckGetT(key string, trace trace.T) (string, error) {
b, err := c.Exists(key)
if err != nil {
return "", err
}
if !b {
return "", errors.New("key not exist")
}
return c.Get(key, cache.WithTrace(trace))
}
// TTL get some key from redis
func (c *cacheRepo) TTL(key string) (time.Duration, error) {
ctx := context.Background()
ttl, err := c.client.TTL(ctx, key).Result()
if err != nil {
return -1, errors.Wrapf(err, "redis get key: %s err", key)
}
return ttl, nil
}
// Expire expire some key
func (c *cacheRepo) Expire(key string, ttl time.Duration) (bool, error) {
ctx := context.Background()
return c.client.Expire(ctx, key, ttl).Result()
}
// ExpireAt expire some key at some time
func (c *cacheRepo) ExpireAt(key string, ttl time.Time) (bool, error) {
ctx := context.Background()
return c.client.ExpireAt(ctx, key, ttl).Result()
}
func (c *cacheRepo) Exists(keys ...string) (bool, error) {
if len(keys) == 0 {
return false, errors.New("missed key param")
}
ctx := context.Background()
value, err := c.client.Exists(ctx, keys...).Result()
if err != nil {
return false, err
}
return value > 0, nil
}
func (c *cacheRepo) Del(key string, options ...cache.Option) (int64, error) {
if key == "" {
return 0, errors.New("missed key param")
}
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "del"
opt.Redis.Key = key
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
value, err := c.client.Del(ctx, key).Result()
if err != nil {
return 0, err
}
return value, nil
}
func (c *cacheRepo) DelT(key string, trace trace.T) (int64, error) {
return c.Del(key, cache.WithTrace(trace))
}
func (c *cacheRepo) Incr(key string, options ...cache.Option) (int64, error) {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "incr"
opt.Redis.Key = key
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
return c.client.Incr(ctx, key).Result()
}
// Close close redis client
func (c *cacheRepo) Close() error {
return c.client.Close()
}
// Version redis server version
func (c *cacheRepo) Version() string {
ctx := context.Background()
server := c.client.Info(ctx, "server").Val()
spl1 := strings.Split(server, "# Server")
spl2 := strings.Split(spl1[1], "redis_version:")
spl3 := strings.Split(spl2[1], "redis_git_sha1:")
return spl3[0]
}
func (c *cacheRepo) HSetValues(key string, values ...interface{}) error {
return c.HSet(key, values)
}
func (c *cacheRepo) HSet(key string, values []any, options ...cache.Option) error {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "hset"
opt.Redis.Key = key
opt.Redis.Value = values
// opt.Redis.TTL = ttl.Minutes()
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
if err := c.client.HSet(ctx, key, values...).Err(); err != nil {
return errors.Wrapf(err, "redis hset key: %s err", key)
}
return nil
}
func (c *cacheRepo) HGet(key, field string, options ...cache.Option) (string, error) {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "hget"
opt.Redis.Key = key
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
for _, f := range options {
f(opt)
}
ctx := context.Background()
value, err := c.client.HGet(ctx, key, field).Result()
if err != nil {
return "", errors.Wrapf(err, "redis hget key: %s err", key)
}
return value, nil
}
func (c *cacheRepo) HMSet(key string, values ...interface{}) (bool, error) {
ts := time.Now()
opt := cache.NewOption()
defer func() {
if opt.Trace != nil {
opt.Redis.Timestamp = timeutil.CSTLayoutString()
opt.Redis.Handle = "hmset"
opt.Redis.Key = key
opt.Redis.CostSeconds = time.Since(ts).Seconds()
opt.Trace.AppendRedis(opt.Redis)
}
}()
ctx := context.Background()
value, err := c.client.HMSet(ctx, key, values...).Result()
if err != nil {
return false, errors.Wrapf(err, "redis hmset key: %s err", key)
}
return value, nil
}
func (c *cacheRepo) HMGet(key string, field ...string) ([]interface{}, error) {
ctx := context.Background()
return c.client.HMGet(ctx, key, field...).Result()
}
func (c *cacheRepo) HDel(key string, field ...string) (int64, error) {
ctx := context.Background()
return c.client.HDel(ctx, key, field...).Result()
}
func (c *cacheRepo) HGetAll(key string) (map[string]string, error) {
ctx := context.Background()
return c.client.HGetAll(ctx, key).Result()
}
// set
func (c *cacheRepo) SIsMember(key, member string) bool {
ctx := context.Background()
return c.client.SIsMember(ctx, key, member).Val()
}
func (c *cacheRepo) SMembers(key string) ([]string, error) {
ctx := context.Background()
return c.client.SMembers(ctx, key).Result()
}
func (c *cacheRepo) SAdd(key, member string) error {
ctx := context.Background()
return c.client.SAdd(ctx, key, member).Err()
}
func (c *cacheRepo) SRem(key, member string) error {
ctx := context.Background()
return c.client.SRem(ctx, key, member).Err()
}
func (c *cacheRepo) RPop(key string) (r string, err error) {
ctx := context.Background()
r, err = c.client.RPop(ctx, key).Result()
return
}
func (c *cacheRepo) BRPop(key string) (r []string, err error) {
ctx := context.Background()
r, err = c.client.BRPop(ctx, 0, key).Result()
return
}
func (c *cacheRepo) LPush(key string, value interface{}) error {
ctx := context.Background()
_, err := c.client.LPush(ctx, key, value).Result()
return err
}
func (c *cacheRepo) Keys(pattern string) (r []string, err error) {
ctx := context.Background()
r, err = c.client.Keys(ctx, pattern).Result()
return
}
// lua script
func (c *cacheRepo) RunScript(script string, keys []string, args ...interface{}) error {
ctx := context.Background()
luaScript := redis.NewScript(script)
return luaScript.Run(ctx, c.client, keys, args...).Err()
}
func (c *cacheRepo) Publish(channel string, message interface{}) (int64, error) {
ctx := context.Background()
return c.client.Publish(ctx, channel, message).Result()
}
func (c *cacheRepo) Subscribe(channels ...string) <-chan *redis.Message {
pubsub := c.client.Subscribe(context.Background(), channels...)
return pubsub.Channel()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.1.118

搜索帮助