FUSE: add chunk cache for recently accessed file chunks

This commit is contained in:
Chris Lu
2020-03-28 13:43:31 -07:00
parent 4aa82c95e6
commit 826bc0b7e3
6 changed files with 71 additions and 17 deletions

View File

@@ -0,0 +1,32 @@
package pb_cache
import (
"time"
"github.com/karlseguin/ccache"
)
// a global cache for recently accessed file chunks
type ChunkCache struct {
cache *ccache.Cache
}
func NewChunkCache() *ChunkCache {
return &ChunkCache{
cache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
}
}
func (c *ChunkCache) GetChunk(fileId string) []byte {
item := c.cache.Get(fileId)
if item == nil {
return nil
}
data := item.Value().([]byte)
item.Extend(time.Hour)
return data
}
func (c *ChunkCache) SetChunk(fileId string, data []byte) {
c.cache.Set(fileId, data, time.Hour)
}