Files
EdgeNode/internal/caches/writer_memory.go

142 lines
2.7 KiB
Go
Raw Normal View History

2020-10-05 19:15:35 +08:00
package caches
import (
2020-11-21 22:29:57 +08:00
"github.com/cespare/xxhash"
2020-10-05 19:15:35 +08:00
"sync"
)
type MemoryWriter struct {
key string
expiredAt int64
m map[uint64]*MemoryItem
locker *sync.RWMutex
isFirstWriting bool
2021-01-13 12:02:50 +08:00
headerSize int64
bodySize int64
status int
hash uint64
item *MemoryItem
2020-10-05 19:15:35 +08:00
}
2021-01-13 12:02:50 +08:00
func NewMemoryWriter(m map[uint64]*MemoryItem, key string, expiredAt int64, status int, locker *sync.RWMutex) *MemoryWriter {
w := &MemoryWriter{
2020-10-05 19:15:35 +08:00
m: m,
key: key,
expiredAt: expiredAt,
locker: locker,
isFirstWriting: true,
2021-01-13 12:02:50 +08:00
status: status,
2020-10-05 19:15:35 +08:00
}
w.hash = w.calculateHash(key)
return w
2020-10-05 19:15:35 +08:00
}
2021-01-13 12:02:50 +08:00
// 写入数据
func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error) {
this.headerSize += int64(len(data))
this.locker.Lock()
item, ok := this.m[this.hash]
2021-01-13 12:02:50 +08:00
if ok {
item.IsDone = false
2021-01-13 12:02:50 +08:00
// 第一次写先清空
if this.isFirstWriting {
item.HeaderValue = nil
item.BodyValue = nil
this.isFirstWriting = false
}
item.HeaderValue = append(item.HeaderValue, data...)
} else {
item = &MemoryItem{}
2021-01-13 12:02:50 +08:00
item.HeaderValue = append([]byte{}, data...)
item.ExpiredAt = this.expiredAt
item.Status = this.status
this.isFirstWriting = false
}
this.item = item
2021-01-13 12:02:50 +08:00
this.locker.Unlock()
return len(data), nil
}
2020-10-05 19:15:35 +08:00
// 写入数据
func (this *MemoryWriter) Write(data []byte) (n int, err error) {
2021-01-13 12:02:50 +08:00
this.bodySize += int64(len(data))
2020-10-05 19:15:35 +08:00
this.locker.Lock()
item, ok := this.m[this.hash]
2020-10-05 19:15:35 +08:00
if ok {
item.IsDone = false
2020-10-05 19:15:35 +08:00
// 第一次写先清空
if this.isFirstWriting {
2021-01-13 12:02:50 +08:00
item.HeaderValue = nil
item.BodyValue = nil
2020-10-05 19:15:35 +08:00
this.isFirstWriting = false
}
2021-01-13 12:02:50 +08:00
item.BodyValue = append(item.BodyValue, data...)
2020-10-05 19:15:35 +08:00
} else {
item = &MemoryItem{}
2021-01-13 12:02:50 +08:00
item.BodyValue = append([]byte{}, data...)
2020-10-05 19:15:35 +08:00
item.ExpiredAt = this.expiredAt
2021-01-13 12:02:50 +08:00
item.Status = this.status
2020-10-05 20:23:18 +08:00
this.isFirstWriting = false
2020-10-05 19:15:35 +08:00
}
this.item = item
2020-10-05 19:15:35 +08:00
this.locker.Unlock()
return len(data), nil
}
// 数据尺寸
2021-01-13 12:02:50 +08:00
func (this *MemoryWriter) HeaderSize() int64 {
return this.headerSize
}
func (this *MemoryWriter) BodySize() int64 {
return this.bodySize
2020-10-05 19:15:35 +08:00
}
// 关闭
func (this *MemoryWriter) Close() error {
if this.item == nil {
return nil
}
this.locker.Lock()
this.item.IsDone = true
this.m[this.hash] = this.item
this.locker.Unlock()
2020-10-05 19:15:35 +08:00
return nil
}
// 丢弃
func (this *MemoryWriter) Discard() error {
this.locker.Lock()
delete(this.m, this.hash)
2020-10-05 19:15:35 +08:00
this.locker.Unlock()
return nil
}
// Key
func (this *MemoryWriter) Key() string {
return this.key
}
// 过期时间
func (this *MemoryWriter) ExpiredAt() int64 {
return this.expiredAt
}
// 内容类型
func (this *MemoryWriter) ItemType() ItemType {
return ItemTypeMemory
}
2020-10-05 19:15:35 +08:00
// 计算Key Hash
func (this *MemoryWriter) calculateHash(key string) uint64 {
2020-11-21 22:29:57 +08:00
return xxhash.Sum64String(key)
2020-10-05 19:15:35 +08:00
}