redisdb/get_by_key.go

27 lines
530 B
Go
Raw Permalink 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"
2022-12-05 08:16:42 +00:00
"errors"
2022-01-27 03:10:02 +00:00
)
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
2022-12-05 08:17:39 +00:00
// GetValueByKeyWithBindData ...
func (s Server) GetValueByKeyWithBindData(ctx context.Context, key string, result interface{}) error {
v := s.GetValueByKey(ctx, key)
2022-01-27 03:10:02 +00:00
if v == "" {
2022-12-05 08:16:42 +00:00
return errors.New("key not found")
2022-01-27 03:10:02 +00:00
}
2022-12-05 08:17:39 +00:00
return json.Unmarshal([]byte(v), &result)
2022-01-27 03:10:02 +00:00
}