add utils of incr&decr for cache

This commit is contained in:
AllenX2018
2020-12-12 17:50:36 +08:00
parent b6d6571e99
commit 4b10cda1d6
3 changed files with 101 additions and 88 deletions

54
client/cache/file.go vendored
View File

@@ -199,25 +199,12 @@ func (fc *FileCache) Incr(ctx context.Context, key string) error {
return err
}
var res interface{}
switch val := data.(type) {
case int:
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")
val, err := incr(data)
if err != nil {
return err
}
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.
@@ -227,37 +214,12 @@ func (fc *FileCache) Decr(ctx context.Context, key string) error {
return err
}
var res interface{}
switch val := data.(type) {
case int:
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")
val, err := decr(data)
if err != nil {
return err
}
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.