avoid duplicated setting chunks into cache

This commit is contained in:
Chris Lu 2020-04-12 01:06:50 -07:00
parent b9b7da905e
commit 2a1f396df5

View file

@ -54,6 +54,10 @@ func (c *ChunkCache) GetChunk(fileId string) (data []byte) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
return c.doGetChunk(fileId)
}
func (c *ChunkCache) doGetChunk(fileId string) (data []byte) {
if data = c.memCache.GetChunk(fileId); data != nil { if data = c.memCache.GetChunk(fileId); data != nil {
return data return data
} }
@ -86,6 +90,13 @@ func (c *ChunkCache) SetChunk(fileId string, data []byte) {
c.Lock() c.Lock()
defer c.Unlock() 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) c.memCache.SetChunk(fileId, data)
if len(c.diskCaches) == 0 { if len(c.diskCaches) == 0 {