Add IncryBy and DecrBy methods to increase or decrease a value
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
This commit is contained in:
parent
627ed0cfd2
commit
47864aedf5
|
@ -27,3 +27,26 @@ type ErrValueNotFoundForKey struct {
|
|||
func (e *ErrValueNotFoundForKey) Error() string {
|
||||
return fmt.Sprintf("could not find value for key %s", e.Key)
|
||||
}
|
||||
|
||||
// IsErrValueNotFoundForKey checks if an error is ErrValueNotFoundForKey
|
||||
func IsErrValueNotFoundForKey(err error) bool {
|
||||
_, is := err.(*ErrValueNotFoundForKey)
|
||||
return is
|
||||
}
|
||||
|
||||
// ErrValueHasWrongType represents an error where a value saved at key has the wrong value
|
||||
type ErrValueHasWrongType struct {
|
||||
Key string
|
||||
ExpectedValue string
|
||||
}
|
||||
|
||||
// Error is the error implementation
|
||||
func (e *ErrValueHasWrongType) Error() string {
|
||||
return fmt.Sprintf("value at key %s has the wrong value, expexted was %s", e.Key, e.ExpectedValue)
|
||||
}
|
||||
|
||||
// IsErrValueHasWrongType checks if an error is ErrValueHasWrongType
|
||||
func IsErrValueHasWrongType(err error) bool {
|
||||
_, is := err.(*ErrValueHasWrongType)
|
||||
return is
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ type Storage interface {
|
|||
Put(key string, value interface{}) (err error)
|
||||
Get(key string) (value interface{}, err error)
|
||||
Del(key string) (err error)
|
||||
IncrBy(key string, update int64) (err error)
|
||||
DecrBy(key string, update int64) (err error)
|
||||
}
|
||||
|
||||
var store Storage
|
||||
|
|
|
@ -64,3 +64,39 @@ func (s *Storage) Del(key string) (err error) {
|
|||
delete(s.store, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrBy increases the value saved at key by the amount provided through update
|
||||
// It assumes the value saved for the key either does not exist or has a type of int64
|
||||
func (s *Storage) IncrBy(key string, update int64) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
v, err := s.Get(key)
|
||||
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
||||
return err
|
||||
}
|
||||
val, is := v.(int64)
|
||||
if !is {
|
||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||
}
|
||||
s.store[key] = val + update
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecrBy decreases the value saved at key by the amount provided through update
|
||||
// It assumes the value saved for the key either does not exist or has a type of int64
|
||||
func (s *Storage) DecrBy(key string, update int64) (err error) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
v, err := s.Get(key)
|
||||
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
||||
return err
|
||||
}
|
||||
val, is := v.(int64)
|
||||
if !is {
|
||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||
}
|
||||
s.store[key] = val - update
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -66,3 +66,13 @@ func (s *Storage) Get(key string) (value interface{}, err error) {
|
|||
func (s *Storage) Del(key string) (err error) {
|
||||
return s.client.Del(key).Err()
|
||||
}
|
||||
|
||||
// IncrBy increases the value saved at key by the amount provided through update
|
||||
func (s *Storage) IncrBy(key string, update int64) (err error) {
|
||||
return s.client.IncrBy(key, update).Err()
|
||||
}
|
||||
|
||||
// DecrBy decreases the value saved at key by the amount provided through update
|
||||
func (s *Storage) DecrBy(key string, update int64) (err error) {
|
||||
return s.client.DecrBy(key, update).Err()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue