Created
May 28, 2025 14:17
-
-
Save brunoksato/79d26ee2fe6c98e21d060e7ecfe924eb to your computer and use it in GitHub Desktop.
redis_helper.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package model | |
| import ( | |
| "context" | |
| "fmt" | |
| "os" | |
| "strconv" | |
| "time" | |
| "github.com/go-redis/redis/v8" | |
| ) | |
| type RedisCacheMiss error | |
| const DEFAULT_TTL = -1 | |
| func RedisGetTTLValue(ctx *ModelCtx, key string, cnx ...redis.Conn) (int, RedisCacheMiss) { | |
| cmd := ctx.Redis.TTL(context.Background(), key) | |
| if cmd.Err() != nil { | |
| ctx.Logger.Warnf("Redis error on querying value for key %s: %s", key, cmd.Err().Error()) | |
| return 0, RedisCacheMiss(cmd.Err()) | |
| } | |
| return int(cmd.Val()), nil | |
| } | |
| func RedisExists(ctx *ModelCtx, key string, cnx ...redis.Conn) (bool, RedisCacheMiss) { | |
| cmd := ctx.Redis.Exists(context.Background(), key) | |
| if cmd.Err() != nil { | |
| ctx.Logger.Warnf("Redis error on querying value for key %s: %s", key, cmd.Err().Error()) | |
| return false, RedisCacheMiss(cmd.Err()) | |
| } | |
| return cmd.Val() == 1, nil | |
| } | |
| func RedisGetModel(ctx *ModelCtx, key string, item interface{}, cnx ...redis.Conn) (interface{}, RedisCacheMiss) { | |
| var j string | |
| j, rerr := RedisGetStringValue(ctx, key) | |
| if rerr != nil { | |
| return item, rerr | |
| } | |
| err := core.JsonToModel(j, item) | |
| if err != nil { | |
| ctx.Logger.Warnf("Error unmarshalling JSON for %s: %s", "", err.Error()) | |
| // RedisClearKey(ctx, key) | |
| rerr = RedisCacheMiss(err) | |
| } | |
| return item, rerr | |
| } | |
| func RedisGetStringValue(ctx *ModelCtx, key string, cnx ...redis.Conn) (string, RedisCacheMiss) { | |
| val, err := ctx.Redis.Do(context.Background(), "get", key).Text() | |
| if err != nil { | |
| return "", RedisCacheMiss(err) | |
| } | |
| return val, nil | |
| } | |
| func RedisSetModel(ctx *ModelCtx, key string, item interface{}, ttl int, cnx ...redis.Conn) core.DefaultError { | |
| json := core.ModelToJson(item) | |
| return RedisSetStringValue(ctx, key, json, ttl) | |
| } | |
| func RedisSetStringValue(ctx *ModelCtx, key string, val string, ttl int, cnx ...redis.Conn) core.DefaultError { | |
| ttl = givenOrDefaultTtl(ttl) | |
| err := RedisSetValue(ctx, key, val, ttl, cnx...) | |
| if err != nil { | |
| return err | |
| } | |
| return nil | |
| } | |
| func givenOrDefaultTtl(ttl int) int { | |
| if ttl == 0 { | |
| ttl = DEFAULT_TTL | |
| newTtl, err := strconv.Atoi(os.Getenv("REDIS_TTL_DEFAULT")) | |
| if err == nil { | |
| ttl = newTtl | |
| } | |
| } | |
| return ttl | |
| } | |
| func RedisSetValue(ctx *ModelCtx, key string, val interface{}, ttl int, cnx ...redis.Conn) core.DefaultError { | |
| var err error | |
| if ttl == -1 { | |
| _, err = ctx.Redis.Set(context.Background(), key, val, DEFAULT_TTL).Result() | |
| } else { | |
| timeout := time.Duration(ttl) * time.Second | |
| _, err = ctx.Redis.SetEX(context.Background(), key, val, timeout).Result() | |
| } | |
| if err != nil { | |
| return core.NewServerError( | |
| fmt.Sprintf("RedisSetValue: key: %s - %s", key, err.Error()), | |
| map[string]interface{}{ | |
| "key": key, | |
| "value": val, | |
| }, | |
| ) | |
| } | |
| return nil | |
| } | |
| func RedisGetAlert(ctx *ModelCtx, a Alert) (alert Alert, rerr RedisCacheMiss) { | |
| key := fmt.Sprintf("tv:%s:%s:%s", a.SymbolName, a.Timeframe, a.Side) | |
| item, rerr := RedisGetModel(ctx, key, &alert) | |
| if rerr != nil { | |
| return Alert{}, rerr | |
| } | |
| alert = *(item.(*Alert)) | |
| return alert, rerr | |
| } | |
| func RedisClearKey(ctx *ModelCtx, key string, cnx ...redis.Conn) { | |
| ctx.Redis.Del(context.Background(), key) | |
| } | |
| func RedisKeyForPositions() string { | |
| return fmt.Sprintf("positions") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment