perf(util): use switch to change size

This commit is contained in:
HuangJiaLuo 2021-10-04 20:33:38 +08:00
parent 1774c9f230
commit b95fb85ad6
2 changed files with 14 additions and 12 deletions

View File

@ -16,7 +16,7 @@ func TestWorker(t *testing.T) {
ctx := context.Background()
lru := NewLRUCache()
produce := event.NewProduce(lru.GetDriver())
workEvent := event.NewEvent(OpEventName)
workEvent := event.NewEvent(OptionEventName)
workEvent.SetValue(WorkFuncEventKey, event.EventWorkFunc(func() (interface{}, error) {
v1 := stringx.NewStringSingle()
key := "v1"

View File

@ -9,22 +9,24 @@ import (
// ParseSizeToBit
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"等等
func ParseSizeToBit(size string) interface{} {
func ParseSizeToBit(size string) (int64, error) {
sizes := regexp.MustCompile("^\\d+")
sizeRes := sizes.FindAllString(size, 1)
unit := strings.Split(size, sizeRes[0])
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
if strings.ToUpper(unit[1]) == "BIT" || strings.ToUpper(unit[1]) == "B"{
return Res * 8
}else if strings.ToUpper(unit[1]) == "KB"{
return Res * 1024 * 8
} else if strings.ToUpper(unit[1])=="MB"{
return Res * 1024 * 1024 * 8
} else if strings.ToUpper(unit[1]) == "GB"{
return Res * 1024 *1024 * 1024 * 8
sizeType := strings.ToUpper(unit[1])
switch {
case sizeType == "BIT" || sizeType == "B":
return Res * 8, nil
case sizeType == "KB":
return Res * 1024 * 8, nil
case sizeType =="MB":
return Res * 1024 * 1024 * 8, nil
case sizeType == "GB":
return Res * 1024 *1024 * 1024 * 8, nil
default:
return 0, errorx.New("your size is wrong")
}
return errorx.New("your size is wrong")
}