优化缓存管理

This commit is contained in:
GoEdgeLab
2021-01-13 12:02:50 +08:00
parent 0e51e1c7cb
commit ba6fa92dc9
22 changed files with 1517 additions and 548 deletions

View File

@@ -1,14 +1,11 @@
package utils
import (
"time"
)
var BytePool1024 = NewBytePool(20480, 1024)
// pool for get byte slice
type BytePool struct {
c chan []byte
length int
ticker *Ticker
lastSize int
}
@@ -25,38 +22,9 @@ func NewBytePool(maxSize, length int) *BytePool {
c: make(chan []byte, maxSize),
length: length,
}
pool.start()
return pool
}
func (this *BytePool) start() {
// 清除Timer
this.ticker = NewTicker(1 * time.Minute)
go func() {
for this.ticker.Next() {
currentSize := len(this.c)
if currentSize <= 32 || this.lastSize == 0 || this.lastSize != currentSize {
this.lastSize = currentSize
continue
}
i := 0
For:
for {
select {
case _ = <-this.c:
i++
if i >= currentSize/2 {
break For
}
default:
break For
}
}
}
}()
}
// 获取一个新的byte slice
func (this *BytePool) Get() (b []byte) {
select {
@@ -83,8 +51,3 @@ func (this *BytePool) Put(b []byte) {
func (this *BytePool) Size() int {
return len(this.c)
}
// 销毁
func (this *BytePool) Destroy() {
this.ticker.Stop()
}