mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 15:00:26 +08:00
优化缓存管理
This commit is contained in:
@@ -11,22 +11,25 @@ type MemoryWriter struct {
|
||||
m map[uint64]*MemoryItem
|
||||
locker *sync.RWMutex
|
||||
isFirstWriting bool
|
||||
size int64
|
||||
headerSize int64
|
||||
bodySize int64
|
||||
status int
|
||||
}
|
||||
|
||||
func NewMemoryWriter(m map[uint64]*MemoryItem, key string, expiredAt int64, locker *sync.RWMutex) *MemoryWriter {
|
||||
func NewMemoryWriter(m map[uint64]*MemoryItem, key string, expiredAt int64, status int, locker *sync.RWMutex) *MemoryWriter {
|
||||
return &MemoryWriter{
|
||||
m: m,
|
||||
key: key,
|
||||
expiredAt: expiredAt,
|
||||
locker: locker,
|
||||
isFirstWriting: true,
|
||||
status: status,
|
||||
}
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
func (this *MemoryWriter) Write(data []byte) (n int, err error) {
|
||||
this.size += int64(len(data))
|
||||
func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error) {
|
||||
this.headerSize += int64(len(data))
|
||||
|
||||
hash := this.hash(this.key)
|
||||
this.locker.Lock()
|
||||
@@ -34,14 +37,43 @@ func (this *MemoryWriter) Write(data []byte) (n int, err error) {
|
||||
if ok {
|
||||
// 第一次写先清空
|
||||
if this.isFirstWriting {
|
||||
item.Value = nil
|
||||
item.HeaderValue = nil
|
||||
item.BodyValue = nil
|
||||
this.isFirstWriting = false
|
||||
}
|
||||
item.Value = append(item.Value, data...)
|
||||
item.HeaderValue = append(item.HeaderValue, data...)
|
||||
} else {
|
||||
item := &MemoryItem{}
|
||||
item.Value = append([]byte{}, data...)
|
||||
item.HeaderValue = append([]byte{}, data...)
|
||||
item.ExpiredAt = this.expiredAt
|
||||
item.Status = this.status
|
||||
this.m[hash] = item
|
||||
this.isFirstWriting = false
|
||||
}
|
||||
this.locker.Unlock()
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
func (this *MemoryWriter) Write(data []byte) (n int, err error) {
|
||||
this.bodySize += int64(len(data))
|
||||
|
||||
hash := this.hash(this.key)
|
||||
this.locker.Lock()
|
||||
item, ok := this.m[hash]
|
||||
if ok {
|
||||
// 第一次写先清空
|
||||
if this.isFirstWriting {
|
||||
item.HeaderValue = nil
|
||||
item.BodyValue = nil
|
||||
this.isFirstWriting = false
|
||||
}
|
||||
item.BodyValue = append(item.BodyValue, data...)
|
||||
} else {
|
||||
item := &MemoryItem{}
|
||||
item.BodyValue = append([]byte{}, data...)
|
||||
item.ExpiredAt = this.expiredAt
|
||||
item.Status = this.status
|
||||
this.m[hash] = item
|
||||
this.isFirstWriting = false
|
||||
}
|
||||
@@ -50,8 +82,12 @@ func (this *MemoryWriter) Write(data []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
// 数据尺寸
|
||||
func (this *MemoryWriter) Size() int64 {
|
||||
return this.size
|
||||
func (this *MemoryWriter) HeaderSize() int64 {
|
||||
return this.headerSize
|
||||
}
|
||||
|
||||
func (this *MemoryWriter) BodySize() int64 {
|
||||
return this.bodySize
|
||||
}
|
||||
|
||||
// 关闭
|
||||
|
||||
Reference in New Issue
Block a user