aboutsummaryrefslogtreecommitdiff
path: root/weed/util/chunk_cache/chunk_cache_in_memory.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2020-04-11 21:12:41 -0700
committerChris Lu <chris.lu@gmail.com>2020-04-11 21:12:41 -0700
commitdf97da25f902912dd527d4aed567408c3ca0f9ae (patch)
tree3e5d4d6bcfb69b3ab869c0b519048943f26e69a1 /weed/util/chunk_cache/chunk_cache_in_memory.go
parentc8ca234773e2a0c57c503c1f3464d1ded4edd2df (diff)
downloadseaweedfs-df97da25f902912dd527d4aed567408c3ca0f9ae.tar.xz
seaweedfs-df97da25f902912dd527d4aed567408c3ca0f9ae.zip
mount: add on disk caching
Diffstat (limited to 'weed/util/chunk_cache/chunk_cache_in_memory.go')
-rw-r--r--weed/util/chunk_cache/chunk_cache_in_memory.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/weed/util/chunk_cache/chunk_cache_in_memory.go b/weed/util/chunk_cache/chunk_cache_in_memory.go
new file mode 100644
index 000000000..931e45e9a
--- /dev/null
+++ b/weed/util/chunk_cache/chunk_cache_in_memory.go
@@ -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)
+}