forked from p93542168/wheat-cache
feat(lru): fix lru function and memory function
This commit is contained in:
parent
47a49b1fdb
commit
96d5a9acc9
|
@ -33,13 +33,17 @@ func (lru *singleCache) UpdateLruLength(length int64) {
|
|||
atomic.AddInt64(&lru.nowSize, length)
|
||||
}
|
||||
|
||||
// NewLRUCache lru初始化
|
||||
func NewLRUCache() *singleCache {
|
||||
func cacheInit() (string, string, event.DriverInterface) {
|
||||
maxSize := viper.GetString("lruCache.maxSize")
|
||||
clearSize := viper.GetString("lruCache.clearSize")
|
||||
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
||||
lruDriver := event.NewDriver(maxDriver)
|
||||
return maxSize, clearSize, lruDriver
|
||||
}
|
||||
|
||||
// NewLRUCache lru初始化
|
||||
func NewLRUCache() *singleCache {
|
||||
maxSize, clearSize, lruDriver := cacheInit()
|
||||
return &singleCache{
|
||||
maxsize: util.ParseSizeToBit(maxSize),
|
||||
clearSize: util.ParseSizeToBit(clearSize),
|
||||
|
|
|
@ -7,23 +7,23 @@ import (
|
|||
)
|
||||
|
||||
// ParseSizeToBit
|
||||
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"
|
||||
// 支持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
|
||||
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
|
||||
}
|
||||
return 0
|
||||
return -1
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseSizeToBit(t *testing.T) {
|
||||
fmt.Print(ParseSizeToBit("18KB"))
|
||||
require.Equal(t, ParseSizeToBit("18Kb"), int64(18*1024*8))
|
||||
require.Equal(t, ParseSizeToBit("18KB"), int64(18*1024*8))
|
||||
require.Equal(t, ParseSizeToBit("18mB"), int64(18*1024*1024*8))
|
||||
require.Equal(t, ParseSizeToBit("18gb"), int64(18*1024*1024*1024*8))
|
||||
|
||||
require.Equal(t, ParseSizeToBit("18b"), int64(18*8))
|
||||
require.Equal(t, ParseSizeToBit("18B"), int64(18*8))
|
||||
require.Equal(t, ParseSizeToBit("18bit"), int64(18*8))
|
||||
require.Equal(t, ParseSizeToBit("18BIt"), int64(18*8))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue