add utils of incr&decr for cache
This commit is contained in:
parent
b6d6571e99
commit
4b10cda1d6
83
client/cache/calc_utils.go
vendored
Normal file
83
client/cache/calc_utils.go
vendored
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func incr(originVal interface{}) (interface{}, error) {
|
||||||
|
switch val := originVal.(type) {
|
||||||
|
case int:
|
||||||
|
tmp := val + 1
|
||||||
|
if val > 0 && tmp < 0 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
|
case int32:
|
||||||
|
if val == math.MaxInt32 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return val + 1, nil
|
||||||
|
case int64:
|
||||||
|
// if val == math.MaxInt64 {
|
||||||
|
// return nil, fmt.Errorf("increment would overflow")
|
||||||
|
// }
|
||||||
|
return val + 1, nil
|
||||||
|
case uint:
|
||||||
|
tmp := val + 1
|
||||||
|
if tmp < val {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
|
case uint32:
|
||||||
|
if val == math.MaxUint32 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return val + 1, nil
|
||||||
|
case uint64:
|
||||||
|
if val == math.MaxUint64 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return val + 1, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("item val is not (u)int (u)int32 (u)int64")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func decr(originVal interface{}) (interface{}, error) {
|
||||||
|
switch val := originVal.(type) {
|
||||||
|
case int:
|
||||||
|
tmp := val - 1
|
||||||
|
if val < 0 && tmp > 0 {
|
||||||
|
return nil, fmt.Errorf("decrement would overflow")
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
|
case int32:
|
||||||
|
if val == math.MinInt32 {
|
||||||
|
return nil, fmt.Errorf("decrement would overflow")
|
||||||
|
}
|
||||||
|
return val - 1, nil
|
||||||
|
case int64:
|
||||||
|
if val == math.MinInt64 {
|
||||||
|
return nil, fmt.Errorf("decrement would overflow")
|
||||||
|
}
|
||||||
|
return val - 1, nil
|
||||||
|
case uint:
|
||||||
|
if val == 0 {
|
||||||
|
return nil, fmt.Errorf("decrement would overflow")
|
||||||
|
}
|
||||||
|
return val - 1, nil
|
||||||
|
case uint32:
|
||||||
|
if val == 0 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return val - 1, nil
|
||||||
|
case uint64:
|
||||||
|
if val == 0 {
|
||||||
|
return nil, fmt.Errorf("increment would overflow")
|
||||||
|
}
|
||||||
|
return val - 1, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("item val is not (u)int (u)int32 (u)int64")
|
||||||
|
}
|
||||||
|
}
|
||||||
54
client/cache/file.go
vendored
54
client/cache/file.go
vendored
@ -199,25 +199,12 @@ func (fc *FileCache) Incr(ctx context.Context, key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var res interface{}
|
val, err := incr(data)
|
||||||
switch val := data.(type) {
|
if err != nil {
|
||||||
case int:
|
return err
|
||||||
res = val + 1
|
|
||||||
case int32:
|
|
||||||
res = val + 1
|
|
||||||
case int64:
|
|
||||||
res = val + 1
|
|
||||||
case uint:
|
|
||||||
res = val + 1
|
|
||||||
case uint32:
|
|
||||||
res = val + 1
|
|
||||||
case uint64:
|
|
||||||
res = val + 1
|
|
||||||
default:
|
|
||||||
return errors.Errorf("data is not (u)int (u)int32 (u)int64")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fc.Put(context.Background(), key, res, time.Duration(fc.EmbedExpiry))
|
return fc.Put(context.Background(), key, val, time.Duration(fc.EmbedExpiry))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decr decreases cached int value.
|
// Decr decreases cached int value.
|
||||||
@ -227,37 +214,12 @@ func (fc *FileCache) Decr(ctx context.Context, key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var res interface{}
|
val, err := decr(data)
|
||||||
switch val := data.(type) {
|
if err != nil {
|
||||||
case int:
|
return err
|
||||||
res = val - 1
|
|
||||||
case int32:
|
|
||||||
res = val - 1
|
|
||||||
case int64:
|
|
||||||
res = val - 1
|
|
||||||
case uint:
|
|
||||||
if val > 0 {
|
|
||||||
res = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("data val is less than 0")
|
|
||||||
}
|
|
||||||
case uint32:
|
|
||||||
if val > 0 {
|
|
||||||
res = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("data val is less than 0")
|
|
||||||
}
|
|
||||||
case uint64:
|
|
||||||
if val > 0 {
|
|
||||||
res = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("data val is less than 0")
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.Errorf("data is not (u)int (u)int32 (u)int64")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fc.Put(context.Background(), key, res, time.Duration(fc.EmbedExpiry))
|
return fc.Put(context.Background(), key, val, time.Duration(fc.EmbedExpiry))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsExist checks if value exists.
|
// IsExist checks if value exists.
|
||||||
|
|||||||
52
client/cache/memory.go
vendored
52
client/cache/memory.go
vendored
@ -130,22 +130,12 @@ func (bc *MemoryCache) Incr(ctx context.Context, key string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
}
|
}
|
||||||
switch val := itm.val.(type) {
|
|
||||||
case int:
|
val, err := incr(itm.val)
|
||||||
itm.val = val + 1
|
if err != nil {
|
||||||
case int32:
|
return err
|
||||||
itm.val = val + 1
|
|
||||||
case int64:
|
|
||||||
itm.val = val + 1
|
|
||||||
case uint:
|
|
||||||
itm.val = val + 1
|
|
||||||
case uint32:
|
|
||||||
itm.val = val + 1
|
|
||||||
case uint64:
|
|
||||||
itm.val = val + 1
|
|
||||||
default:
|
|
||||||
return errors.New("item val is not (u)int (u)int32 (u)int64")
|
|
||||||
}
|
}
|
||||||
|
itm.val = val
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,34 +147,12 @@ func (bc *MemoryCache) Decr(ctx context.Context, key string) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
}
|
}
|
||||||
switch val := itm.val.(type) {
|
|
||||||
case int:
|
val, err := decr(itm.val)
|
||||||
itm.val = val - 1
|
if err != nil {
|
||||||
case int64:
|
return err
|
||||||
itm.val = val - 1
|
|
||||||
case int32:
|
|
||||||
itm.val = val - 1
|
|
||||||
case uint:
|
|
||||||
if val > 0 {
|
|
||||||
itm.val = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("item val is less than 0")
|
|
||||||
}
|
|
||||||
case uint32:
|
|
||||||
if val > 0 {
|
|
||||||
itm.val = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("item val is less than 0")
|
|
||||||
}
|
|
||||||
case uint64:
|
|
||||||
if val > 0 {
|
|
||||||
itm.val = val - 1
|
|
||||||
} else {
|
|
||||||
return errors.New("item val is less than 0")
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return errors.New("item val is not int int64 int32")
|
|
||||||
}
|
}
|
||||||
|
itm.val = val
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user