feat: add cache helpers
This commit is contained in:
parent
db684e73f8
commit
3d43ff4758
8 changed files with 532 additions and 1 deletions
56
cache/blob.go
vendored
Normal file
56
cache/blob.go
vendored
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Blob interface {
|
||||
io.ReaderAt
|
||||
io.Closer
|
||||
Size() int64
|
||||
}
|
||||
|
||||
type byteBlob struct {
|
||||
buf *bytes.Reader
|
||||
}
|
||||
|
||||
func NewByteBlob(b []byte) Blob {
|
||||
return &byteBlob{buf: bytes.NewReader(b)}
|
||||
}
|
||||
|
||||
func (blob *byteBlob) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
return blob.buf.ReadAt(p, off)
|
||||
}
|
||||
|
||||
func (blob *byteBlob) Size() int64 {
|
||||
return blob.buf.Size()
|
||||
}
|
||||
|
||||
func (blob *byteBlob) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type fileBlob struct {
|
||||
buf *os.File
|
||||
}
|
||||
|
||||
func NewFileBlob(f *os.File) Blob {
|
||||
return &fileBlob{buf: f}
|
||||
}
|
||||
|
||||
func (blob *fileBlob) ReadAt(p []byte, off int64) (n int, err error) {
|
||||
return blob.buf.ReadAt(p, off)
|
||||
}
|
||||
|
||||
func (blob *fileBlob) Size() int64 {
|
||||
if i, err := blob.buf.Stat(); err != nil {
|
||||
return i.Size()
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (blob *fileBlob) Close() error {
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue