From b95fb85ad6729a29665f599cf63301ba146c456c Mon Sep 17 00:00:00 2001 From: HuangJiaLuo <1820799930@qq.com> Date: Mon, 4 Oct 2021 20:33:38 +0800 Subject: [PATCH] perf(util): use switch to change size --- pkg/lru/woker_test.go | 2 +- pkg/util/memory.go | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pkg/lru/woker_test.go b/pkg/lru/woker_test.go index d412b43..b278f20 100644 --- a/pkg/lru/woker_test.go +++ b/pkg/lru/woker_test.go @@ -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" diff --git a/pkg/util/memory.go b/pkg/util/memory.go index 6b7f64d..c0f5ef3 100644 --- a/pkg/util/memory.go +++ b/pkg/util/memory.go @@ -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") }