Change keyvalue.Get to return if a value exists or not instead of an error
This commit is contained in:
@ -25,7 +25,7 @@ import (
|
||||
// Storage defines an interface for saving key-value pairs
|
||||
type Storage interface {
|
||||
Put(key string, value interface{}) (err error)
|
||||
Get(key string) (value interface{}, err error)
|
||||
Get(key string) (value interface{}, exists bool, err error)
|
||||
Del(key string) (err error)
|
||||
IncrBy(key string, update int64) (err error)
|
||||
DecrBy(key string, update int64) (err error)
|
||||
@ -51,7 +51,7 @@ func Put(key string, value interface{}) error {
|
||||
}
|
||||
|
||||
// Get returns a value from a storage backend
|
||||
func Get(key string) (value interface{}, err error) {
|
||||
func Get(key string) (value interface{}, exists bool, err error) {
|
||||
return store.Get(key)
|
||||
}
|
||||
|
||||
|
@ -44,16 +44,11 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
|
||||
}
|
||||
|
||||
// Get retrieves a saved value from memory storage
|
||||
func (s *Storage) Get(key string) (value interface{}, err error) {
|
||||
func (s *Storage) Get(key string) (value interface{}, exists bool, err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
var exists bool
|
||||
value, exists = s.store[key]
|
||||
if !exists {
|
||||
return nil, &e.ErrValueNotFoundForKey{Key: key}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -20,9 +20,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
|
||||
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
||||
"code.vikunja.io/api/pkg/red"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
@ -52,13 +49,10 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
|
||||
}
|
||||
|
||||
// Get retrieves a saved value from redis
|
||||
func (s *Storage) Get(key string) (value interface{}, err error) {
|
||||
func (s *Storage) Get(key string) (value interface{}, exists bool, err error) {
|
||||
b, err := s.client.Get(context.Background(), key).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, &e.ErrValueNotFoundForKey{Key: key}
|
||||
}
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, value)
|
||||
|
Reference in New Issue
Block a user