aboutsummaryrefslogtreecommitdiff
path: root/weed/util/chunk_cache
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2020-04-11 12:45:24 -0700
committerChris Lu <chris.lu@gmail.com>2020-04-11 12:45:24 -0700
commitd7f3acb2c056534f29950f3586d804ec274349b2 (patch)
tree0fa0f3326a71f97514374888ebc019afdfde3dcc /weed/util/chunk_cache
parented5468259867502facd8df0e3e90574421abfc94 (diff)
downloadseaweedfs-d7f3acb2c056534f29950f3586d804ec274349b2.tar.xz
seaweedfs-d7f3acb2c056534f29950f3586d804ec274349b2.zip
refactor
Diffstat (limited to 'weed/util/chunk_cache')
-rw-r--r--weed/util/chunk_cache/chunk_cache.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go
new file mode 100644
index 000000000..e2676d9cc
--- /dev/null
+++ b/weed/util/chunk_cache/chunk_cache.go
@@ -0,0 +1,36 @@
+package chunk_cache
+
+import (
+ "time"
+
+ "github.com/karlseguin/ccache"
+)
+
+// a global cache for recently accessed file chunks
+type ChunkCache struct {
+ cache *ccache.Cache
+}
+
+func NewChunkCache(maxEntries int64) *ChunkCache {
+ pruneCount := maxEntries >> 3
+ if pruneCount <= 0 {
+ pruneCount = 500
+ }
+ return &ChunkCache{
+ cache: ccache.New(ccache.Configure().MaxSize(maxEntries).ItemsToPrune(uint32(pruneCount))),
+ }
+}
+
+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)
+}