wheat-cache/pkg/util/memory.go

31 lines
750 B
Go
Raw Normal View History

2021-09-27 11:29:47 +08:00
package util
import (
"gitee.com/timedb/wheatCache/pkg/errorx"
2021-09-27 11:29:47 +08:00
"regexp"
"strconv"
"strings"
)
// ParseSizeToBit
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"等等
func ParseSizeToBit(size string) interface{} {
2021-09-27 11:29:47 +08:00
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
2021-09-27 11:29:47 +08:00
}
return errorx.New("your size is wrong")
2021-09-27 11:29:47 +08:00
}