mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-02-11 04:45:36 +08:00
实现缓存策略的部分功能
This commit is contained in:
@@ -1,71 +1,19 @@
|
||||
package caches
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
// 缓存内容写入接口
|
||||
type Writer interface {
|
||||
// 写入数据
|
||||
Write(data []byte) (n int, err error)
|
||||
|
||||
type Writer struct {
|
||||
rawWriter *os.File
|
||||
key string
|
||||
size int64
|
||||
expiredAt int64
|
||||
locker *sync.RWMutex
|
||||
isReleased bool
|
||||
}
|
||||
|
||||
func NewWriter(rawWriter *os.File, key string, expiredAt int64, locker *sync.RWMutex) *Writer {
|
||||
return &Writer{
|
||||
key: key,
|
||||
rawWriter: rawWriter,
|
||||
expiredAt: expiredAt,
|
||||
locker: locker,
|
||||
}
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
func (this *Writer) Write(data []byte) error {
|
||||
n, err := this.rawWriter.Write(data)
|
||||
this.size += int64(n)
|
||||
if err != nil {
|
||||
_ = this.rawWriter.Close()
|
||||
_ = os.Remove(this.rawWriter.Name())
|
||||
this.Release()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 关闭
|
||||
func (this *Writer) Close() error {
|
||||
// 写入结束符
|
||||
_, err := this.rawWriter.WriteString("\n$$$")
|
||||
if err != nil {
|
||||
_ = os.Remove(this.rawWriter.Name())
|
||||
}
|
||||
|
||||
this.Release()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *Writer) Size() int64 {
|
||||
return this.size
|
||||
}
|
||||
|
||||
func (this *Writer) ExpiredAt() int64 {
|
||||
return this.expiredAt
|
||||
}
|
||||
|
||||
func (this *Writer) Key() string {
|
||||
return this.key
|
||||
}
|
||||
|
||||
// 释放锁,一定要调用
|
||||
func (this *Writer) Release() {
|
||||
if this.isReleased {
|
||||
return
|
||||
}
|
||||
this.isReleased = true
|
||||
this.locker.Unlock()
|
||||
// 关闭
|
||||
Close() error
|
||||
|
||||
// 丢弃
|
||||
Discard() error
|
||||
|
||||
// Key
|
||||
Key() string
|
||||
|
||||
// 过期时间
|
||||
ExpiredAt() int64
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user