mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-11-10 17:34:45 +08:00
mount: add on disk caching
This commit is contained in:
36
weed/util/chunk_cache/chunk_cache_in_memory.go
Normal file
36
weed/util/chunk_cache/chunk_cache_in_memory.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package chunk_cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/karlseguin/ccache"
|
||||
)
|
||||
|
||||
// a global cache for recently accessed file chunks
|
||||
type ChunkCacheInMemory struct {
|
||||
cache *ccache.Cache
|
||||
}
|
||||
|
||||
func NewChunkCacheInMemory(maxEntries int64) *ChunkCacheInMemory {
|
||||
pruneCount := maxEntries >> 3
|
||||
if pruneCount <= 0 {
|
||||
pruneCount = 500
|
||||
}
|
||||
return &ChunkCacheInMemory{
|
||||
cache: ccache.New(ccache.Configure().MaxSize(maxEntries).ItemsToPrune(uint32(pruneCount))),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChunkCacheInMemory) 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 *ChunkCacheInMemory) SetChunk(fileId string, data []byte) {
|
||||
c.cache.Set(fileId, data, time.Hour)
|
||||
}
|
||||
Reference in New Issue
Block a user