wheat-cache/pkg/util/memory.go

30 lines
660 B
Go
Raw Normal View History

2021-09-27 11:29:47 +08:00
package util
import (
"regexp"
"strconv"
"strings"
)
// ParseSizeToBit
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"
func ParseSizeToBit(size string) int64 {
sizes := regexp.MustCompile("^\\d+")
sizeRes := sizes.FindAllString(size, 1)
unit := strings.Split(size, sizeRes[0])
if unit[1] == "KB"|| unit[1] == "kb"{
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
return Res * 1024
} else if unit[1] == "MB"|| unit[1] == "mb"{
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
return Res * 1024 * 1024
} else if unit[1] == "GB"|| unit[1] == "fb"{
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
return Res * 1024 *1024 * 1024
}
return 0
}