redisdb/get_by_key.go

29 lines
513 B
Go
Raw Normal View History

2021-08-09 03:36:09 +00:00
package redisdb
2022-01-27 03:10:02 +00:00
import (
"context"
"encoding/json"
)
2021-08-09 03:36:09 +00:00
// GetValueByKey ...
func (s Server) GetValueByKey(ctx context.Context, key string) string {
cmd := s.rdb.Get(ctx, key)
2021-08-09 03:36:09 +00:00
if cmd == nil {
return ""
}
value, _ := cmd.Result()
return value
}
2022-01-27 03:10:02 +00:00
// GetJSON ...
func (s Server) GetJSON(ctx context.Context, key string, result interface{}) (ok bool) {
v := s.GetValueByKey(ctx, key)
2022-01-27 03:10:02 +00:00
if v == "" {
return false
}
if err := json.Unmarshal([]byte(v), result); err != nil {
return false
}
return true
}