package redis import ( "context" "finclip-app-manager/infrastructure/config" "strings" "time" "github.com/SkyAPM/go2sky" "github.com/garyburd/redigo/redis" goRedis "github.com/go-redis/redis/v8" fcredis "gitlab.finogeeks.club/finclip-backend-v2/finclip-redis" "gitlab.finogeeks.club/finclip-backend/apm" ) const ( MODE_SINGLE = "single" MODE_SENTINEL = "sentinel" MODE_SENTINEL_V2 = "sentinel_v2" MODE_CLUSTER = "cluster" MODE_CLUSTER_V2 = "cluster_v2" ) func init() { RedisStart() } func RedisStart() { cfg := config.GetConfig() switch cfg.RedisMode { case "single": opt, err := goRedis.ParseURL(cfg.RedisAddr) if err != nil { panic(err) } opt.Password = cfg.RedisPassword err = fcredis.Init("single", opt) if err != nil { panic(err) } case "sentinel", "sentinel_v2": opt := &goRedis.FailoverOptions{ SentinelAddrs: strings.Split(cfg.RedisSentinelAddr, ","), SentinelPassword: cfg.RedisSentinelPassword, MasterName: cfg.RedisMasterName, DB: cfg.RedisDatabase, } if err := fcredis.Init("sentinel", opt); err != nil { panic(err) } case "cluster", "cluster_v2": opt := &goRedis.ClusterOptions{ Addrs: strings.Split(cfg.RedisAddr, ","), Password: cfg.RedisPassword, } if err := fcredis.Init("cluster", opt); err != nil { panic(err) } } return } func createRedisSpan(ctx context.Context, action string) go2sky.Span { switch config.GetConfig().RedisMode { case MODE_SENTINEL_V2: span := apm.ApmClient().CreateRedisExitSpan(ctx, action, config.GetConfig().RedisSentinelAddr, "") span.Log(time.Now(), "Mode", config.GetConfig().RedisMode) span.Log(time.Now(), "MasterName", config.GetConfig().RedisMasterName) return span case MODE_CLUSTER: span := apm.ApmClient().CreateRedisExitSpan(ctx, action, config.GetConfig().RedisAddr, "") span.Log(time.Now(), "Mode", config.GetConfig().RedisMode) return span case MODE_CLUSTER_V2: span := apm.ApmClient().CreateRedisExitSpan(ctx, action, config.GetConfig().RedisAddr, "") span.Log(time.Now(), "Mode", config.GetConfig().RedisMode) return span default: if config.GetConfig().RedisMode == MODE_SENTINEL { span := apm.ApmClient().CreateRedisExitSpan(ctx, action, config.GetConfig().RedisSentinelAddr, "") span.Log(time.Now(), "Mode", config.GetConfig().RedisMode) span.Log(time.Now(), "MasterName", config.GetConfig().RedisMasterName) return span } else { span := apm.ApmClient().CreateRedisExitSpan(ctx, action, config.GetConfig().RedisAddr, "") span.Log(time.Now(), "Mode", config.GetConfig().RedisMode) return span } } } func RedisSet(ctx context.Context, key string, value string, expireTime int) (err error) { span := createRedisSpan(ctx, "RedisSet") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Set(ctx, "finclip_"+key, value, time.Duration(expireTime)*time.Second).Err() } func RedisGet(ctx context.Context, key string) (s string, err error) { span := createRedisSpan(ctx, "RedisGet") span.Log(time.Now(), "key", key) return fcredis.Client().Get(ctx, "finclip_"+key).Result() } func RedisGetNum(ctx context.Context, key string) (s int, err error) { span := createRedisSpan(ctx, "RedisGet") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Get(ctx, "finclip_"+key).Int() } func RedisDel(ctx context.Context, key string) (err error) { span := createRedisSpan(ctx, "RedisDel") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Del(ctx, "finclip_"+key).Err() } func Setnx(ctx context.Context, key string, value string, expireTime int) (exi bool, err error) { span := createRedisSpan(ctx, "Setnx") span.Log(time.Now(), "key", key) defer span.End() notExi, err := fcredis.Client().SetNX(ctx, "finclip_"+key, value, time.Duration(expireTime)*time.Second).Result() return !notExi, err } func RedisSetByte(ctx context.Context, key string, value []byte, expireTime int) (err error) { span := createRedisSpan(ctx, "RedisSetByte") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Set(ctx, "finclip_"+key, value, time.Duration(expireTime)*time.Second).Err() } func RedisGetByte(ctx context.Context, key string) (bytes []byte, err error) { span := createRedisSpan(ctx, "RedisGetByte") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Get(ctx, "finclip_"+key).Bytes() } func RedisSetExpire(ctx context.Context, key string, sec int) (err error) { span := createRedisSpan(ctx, "RedisSetExpire") span.Log(time.Now(), "key", key) defer span.End() return fcredis.Client().Expire(ctx, "finclip_"+key, time.Duration(sec)*time.Second).Err() } func RedisIncrValue(ctx context.Context, key string) (value int, err error) { span := createRedisSpan(ctx, "RedisIncrValue") span.Log(time.Now(), "key", key) result, err := fcredis.Client().Incr(ctx, "finclip_"+key).Result() return int(result), err } func RedisGetIntValue(ctx context.Context, key string) (val int, err error) { return fcredis.Client().Get(ctx, "finclip_"+key).Int() } func TryLock(ctx context.Context, key string, lockTime time.Duration) (ok bool, err error) { return fcredis.Lock(ctx, "finclip_"+key, lockTime*time.Second) } func Unlock(ctx context.Context, key string) error { return fcredis.Client().Del(ctx, "finclip_"+key).Err() } func RedisNotFound(err error) bool { return err == goRedis.Nil || err == redis.ErrNil }