From 2a1f396df5abd47e7fc4a58c3bc39675e1e84e4f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 12 Apr 2020 01:06:50 -0700 Subject: [PATCH] avoid duplicated setting chunks into cache --- weed/util/chunk_cache/chunk_cache.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/weed/util/chunk_cache/chunk_cache.go b/weed/util/chunk_cache/chunk_cache.go index ead7a8d0b..48e4bfb0d 100644 --- a/weed/util/chunk_cache/chunk_cache.go +++ b/weed/util/chunk_cache/chunk_cache.go @@ -54,6 +54,10 @@ func (c *ChunkCache) GetChunk(fileId string) (data []byte) { c.RLock() defer c.RUnlock() + return c.doGetChunk(fileId) +} + +func (c *ChunkCache) doGetChunk(fileId string) (data []byte) { if data = c.memCache.GetChunk(fileId); data != nil { return data } @@ -86,6 +90,13 @@ func (c *ChunkCache) SetChunk(fileId string, data []byte) { c.Lock() defer c.Unlock() + if existingData := c.doGetChunk(fileId); len(existingData)==0{ + c.doSetChunk(fileId, data) + } +} + +func (c *ChunkCache) doSetChunk(fileId string, data []byte) { + c.memCache.SetChunk(fileId, data) if len(c.diskCaches) == 0 {