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)
|
atomic.AddInt64(&lru.nowSize, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLRUCache lru初始化
|
func cacheInit() (string, string, event.DriverInterface) {
|
||||||
func NewLRUCache() *singleCache {
|
|
||||||
maxSize := viper.GetString("lruCache.maxSize")
|
maxSize := viper.GetString("lruCache.maxSize")
|
||||||
clearSize := viper.GetString("lruCache.clearSize")
|
clearSize := viper.GetString("lruCache.clearSize")
|
||||||
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
maxDriver := viper.GetInt("lruCache.eventDriverSize")
|
||||||
lruDriver := event.NewDriver(maxDriver)
|
lruDriver := event.NewDriver(maxDriver)
|
||||||
|
return maxSize, clearSize, lruDriver
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLRUCache lru初始化
|
||||||
|
func NewLRUCache() *singleCache {
|
||||||
|
maxSize, clearSize, lruDriver := cacheInit()
|
||||||
return &singleCache{
|
return &singleCache{
|
||||||
maxsize: util.ParseSizeToBit(maxSize),
|
maxsize: util.ParseSizeToBit(maxSize),
|
||||||
clearSize: util.ParseSizeToBit(clearSize),
|
clearSize: util.ParseSizeToBit(clearSize),
|
||||||
|
|
|
@ -7,23 +7,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParseSizeToBit
|
// ParseSizeToBit
|
||||||
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"
|
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"等等
|
||||||
func ParseSizeToBit(size string) int64 {
|
func ParseSizeToBit(size string) int64 {
|
||||||
|
|
||||||
sizes := regexp.MustCompile("^\\d+")
|
sizes := regexp.MustCompile("^\\d+")
|
||||||
sizeRes := sizes.FindAllString(size, 1)
|
sizeRes := sizes.FindAllString(size, 1)
|
||||||
unit := strings.Split(size, sizeRes[0])
|
unit := strings.Split(size, sizeRes[0])
|
||||||
if unit[1] == "KB"|| unit[1] == "kb"{
|
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
||||||
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
if strings.ToUpper(unit[1]) == "BIT" || strings.ToUpper(unit[1]) == "B"{
|
||||||
return Res * 1024
|
return Res * 8
|
||||||
} else if unit[1] == "MB"|| unit[1] == "mb"{
|
}else if strings.ToUpper(unit[1]) == "KB"{
|
||||||
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
return Res * 1024 * 8
|
||||||
return Res * 1024 * 1024
|
} else if strings.ToUpper(unit[1])=="MB"{
|
||||||
} else if unit[1] == "GB"|| unit[1] == "fb"{
|
return Res * 1024 * 1024 * 8
|
||||||
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
} else if strings.ToUpper(unit[1]) == "GB"{
|
||||||
return Res * 1024 *1024 * 1024
|
return Res * 1024 *1024 * 1024 * 8
|
||||||
}
|
}
|
||||||
return 0
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,18 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseSizeToBit(t *testing.T) {
|
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