优化缓存管理

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

@@ -0,0 +1,160 @@
package caches
import (
"errors"
)
type MemoryReader struct {
item *MemoryItem
}
func NewMemoryReader(item *MemoryItem) *MemoryReader {
return &MemoryReader{item: item}
}
func (this *MemoryReader) Init() error {
return nil
}
func (this *MemoryReader) Status() int {
return this.item.Status
}
func (this *MemoryReader) HeaderSize() int64 {
return int64(len(this.item.HeaderValue))
}
func (this *MemoryReader) BodySize() int64 {
return int64(len(this.item.BodyValue))
}
func (this *MemoryReader) ReadHeader(buf []byte, callback ReaderFunc) error {
l := len(buf)
if l == 0 {
return errors.New("using empty buffer")
}
size := len(this.item.HeaderValue)
offset := 0
for {
left := size - offset
if l <= left {
copy(buf, this.item.HeaderValue[offset:offset+l])
goNext, e := callback(l)
if e != nil {
return e
}
if !goNext {
break
}
} else {
copy(buf, this.item.HeaderValue[offset:])
_, e := callback(left)
if e != nil {
return e
}
break
}
offset += l
if offset >= size {
break
}
}
return nil
}
func (this *MemoryReader) ReadBody(buf []byte, callback ReaderFunc) error {
l := len(buf)
if l == 0 {
return errors.New("using empty buffer")
}
size := len(this.item.BodyValue)
offset := 0
for {
left := size - offset
if l <= left {
copy(buf, this.item.BodyValue[offset:offset+l])
goNext, e := callback(l)
if e != nil {
return e
}
if !goNext {
break
}
} else {
copy(buf, this.item.BodyValue[offset:])
_, e := callback(left)
if e != nil {
return e
}
break
}
offset += l
if offset >= size {
break
}
}
return nil
}
func (this *MemoryReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error {
offset := start
bodySize := int64(len(this.item.BodyValue))
if start < 0 {
offset = bodySize + end
end = bodySize - 1
} else if end < 0 {
offset = start
end = bodySize - 1
}
if end >= bodySize {
end = bodySize - 1
}
if offset < 0 || end < 0 || offset > end {
return ErrInvalidRange
}
newData := this.item.BodyValue[offset : end+1]
l := len(buf)
if l == 0 {
return errors.New("using empty buffer")
}
size := len(newData)
offset2 := 0
for {
left := size - offset2
if l <= left {
copy(buf, newData[offset2:offset2+l])
goNext, e := callback(l)
if e != nil {
return e
}
if !goNext {
break
}
} else {
copy(buf, newData[offset2:])
_, e := callback(left)
if e != nil {
return e
}
break
}
offset2 += l
if offset2 >= size {
break
}
}
return nil
}
func (this *MemoryReader) Close() error {
return nil
}