文件缓存策略支持二级缓存(内存 | 文件)

This commit is contained in:
GoEdgeLab
2021-03-02 19:43:05 +08:00
parent 5f5399cc61
commit 5f6b270af3
9 changed files with 145 additions and 22 deletions

View File

@@ -14,10 +14,13 @@ type MemoryWriter struct {
headerSize int64
bodySize int64
status int
hash uint64
item *MemoryItem
}
func NewMemoryWriter(m map[uint64]*MemoryItem, key string, expiredAt int64, status int, locker *sync.RWMutex) *MemoryWriter {
return &MemoryWriter{
w := &MemoryWriter{
m: m,
key: key,
expiredAt: expiredAt,
@@ -25,16 +28,20 @@ func NewMemoryWriter(m map[uint64]*MemoryItem, key string, expiredAt int64, stat
isFirstWriting: true,
status: status,
}
w.hash = w.calculateHash(key)
return w
}
// 写入数据
func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error) {
this.headerSize += int64(len(data))
hash := this.hash(this.key)
this.locker.Lock()
item, ok := this.m[hash]
item, ok := this.m[this.hash]
if ok {
item.IsDone = false
// 第一次写先清空
if this.isFirstWriting {
item.HeaderValue = nil
@@ -43,13 +50,13 @@ func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error) {
}
item.HeaderValue = append(item.HeaderValue, data...)
} else {
item := &MemoryItem{}
item = &MemoryItem{}
item.HeaderValue = append([]byte{}, data...)
item.ExpiredAt = this.expiredAt
item.Status = this.status
this.m[hash] = item
this.isFirstWriting = false
}
this.item = item
this.locker.Unlock()
return len(data), nil
}
@@ -58,10 +65,11 @@ func (this *MemoryWriter) WriteHeader(data []byte) (n int, err error) {
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]
item, ok := this.m[this.hash]
if ok {
item.IsDone = false
// 第一次写先清空
if this.isFirstWriting {
item.HeaderValue = nil
@@ -70,13 +78,13 @@ func (this *MemoryWriter) Write(data []byte) (n int, err error) {
}
item.BodyValue = append(item.BodyValue, data...)
} else {
item := &MemoryItem{}
item = &MemoryItem{}
item.BodyValue = append([]byte{}, data...)
item.ExpiredAt = this.expiredAt
item.Status = this.status
this.m[hash] = item
this.isFirstWriting = false
}
this.item = item
this.locker.Unlock()
return len(data), nil
}
@@ -92,14 +100,22 @@ func (this *MemoryWriter) BodySize() int64 {
// 关闭
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()
return nil
}
// 丢弃
func (this *MemoryWriter) Discard() error {
hash := this.hash(this.key)
this.locker.Lock()
delete(this.m, hash)
delete(this.m, this.hash)
this.locker.Unlock()
return nil
}
@@ -114,7 +130,12 @@ func (this *MemoryWriter) ExpiredAt() int64 {
return this.expiredAt
}
// 内容类型
func (this *MemoryWriter) ItemType() ItemType {
return ItemTypeMemory
}
// 计算Key Hash
func (this *MemoryWriter) hash(key string) uint64 {
func (this *MemoryWriter) calculateHash(key string) uint64 {
return xxhash.Sum64String(key)
}