提升文件缓存读取效率大约5%

This commit is contained in:
GoEdgeLab
2021-08-21 21:44:34 +08:00
parent 7a452c5a6f
commit 04c63ea59f

View File

@@ -34,13 +34,7 @@ func (this *FileReader) Init() error {
} }
}() }()
// 读取状态 var buf = make([]byte, SizeMeta)
_, err := this.fp.Seek(SizeExpiresAt, io.SeekStart)
if err != nil {
_ = this.discard()
return err
}
buf := make([]byte, 3)
ok, err := this.readToBuff(this.fp, buf) ok, err := this.readToBuff(this.fp, buf)
if err != nil { if err != nil {
return err return err
@@ -48,37 +42,18 @@ func (this *FileReader) Init() error {
if !ok { if !ok {
return ErrNotFound return ErrNotFound
} }
status := types.Int(string(buf))
status := types.Int(string(buf[SizeExpiresAt : SizeExpiresAt+SizeStatus]))
if status < 100 || status > 999 { if status < 100 || status > 999 {
return errors.New("invalid status") return errors.New("invalid status")
} }
this.status = status this.status = status
// URL // URL
_, err = this.fp.Seek(SizeExpiresAt+SizeStatus, io.SeekStart) urlLength := binary.BigEndian.Uint32(buf[SizeExpiresAt+SizeStatus : SizeExpiresAt+SizeStatus+SizeURLLength])
if err != nil {
return err
}
bytes4 := make([]byte, 4)
ok, err = this.readToBuff(this.fp, bytes4)
if err != nil {
return err
}
if !ok {
return ErrNotFound
}
urlLength := binary.BigEndian.Uint32(bytes4)
// header // header
ok, err = this.readToBuff(this.fp, bytes4) headerSize := int(binary.BigEndian.Uint32(buf[SizeExpiresAt+SizeStatus+SizeURLLength : SizeExpiresAt+SizeStatus+SizeURLLength+SizeHeaderLength]))
if err != nil {
return err
}
if !ok {
return ErrNotFound
}
headerSize := int(binary.BigEndian.Uint32(bytes4))
if headerSize == 0 { if headerSize == 0 {
return nil return nil
} }
@@ -86,15 +61,7 @@ func (this *FileReader) Init() error {
this.headerOffset = int64(SizeMeta) + int64(urlLength) this.headerOffset = int64(SizeMeta) + int64(urlLength)
// body // body
bytes8 := make([]byte, 8) bodySize := int(binary.BigEndian.Uint64(buf[SizeExpiresAt+SizeStatus+SizeURLLength+SizeHeaderLength : SizeExpiresAt+SizeStatus+SizeURLLength+SizeHeaderLength+SizeBodyLength]))
ok, err = this.readToBuff(this.fp, bytes8)
if err != nil {
return err
}
if !ok {
return ErrNotFound
}
bodySize := int(binary.BigEndian.Uint64(bytes8))
if bodySize == 0 { if bodySize == 0 {
return nil return nil
} }