diff --git a/get_by_key.go b/get_by_key.go index cb1a943..9e50613 100644 --- a/get_by_key.go +++ b/get_by_key.go @@ -1,6 +1,9 @@ package redisdb -import "context" +import ( + "context" + "encoding/json" +) // GetValueByKey ... func GetValueByKey(ctx context.Context, key string) string { @@ -11,3 +14,15 @@ func GetValueByKey(ctx context.Context, key string) string { value, _ := cmd.Result() return value } + +// GetJSON ... +func GetJSON(ctx context.Context, key string, result interface{}) (ok bool) { + v := GetValueByKey(ctx, key) + if v == "" { + return false + } + if err := json.Unmarshal([]byte(v), result); err != nil { + return false + } + return true +} diff --git a/set_key_value.go b/set_key_value.go index 5713ba0..c13a3b3 100644 --- a/set_key_value.go +++ b/set_key_value.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "time" ) // SetKeyValue ... @@ -15,3 +16,13 @@ func SetKeyValue(ctx context.Context, key string, value interface{}) { } rdb.Set(ctx, key, b, 0) } + +// SetTTL ... +func SetTTL(ctx context.Context, key string, value interface{}, d time.Duration) { + b, err := json.Marshal(value) + if err != nil { + fmt.Println("redisdb - SetTTL error", err.Error()) + return + } + rdb.Set(ctx, key, b, d) +}