Nullify list memory to nil as we remove the element

master
Harshavardhana 10 years ago
parent d63064b8af
commit 63edb1e9a0
  1. 12
      pkg/storage/drivers/memory/lru.go

@ -72,6 +72,7 @@ type entry struct {
key string key string
time time.Time time time.Time
value *bytes.Buffer value *bytes.Buffer
size int64
} }
// NewCache creates a new Cache. // NewCache creates a new Cache.
@ -114,17 +115,17 @@ func (c *Cache) Add(key string, size int64) io.WriteCloser {
c.RemoveOldest() c.RemoveOldest()
} }
} }
value := new(bytes.Buffer) buffer := new(bytes.Buffer)
n, err := io.CopyN(value, r, size) written, err := io.CopyN(buffer, r, size)
if err != nil { if err != nil {
err := iodine.New(err, nil) err := iodine.New(err, nil)
r.CloseWithError(err) r.CloseWithError(err)
blockingWriter.Release(err) blockingWriter.Release(err)
return return
} }
ele := c.ll.PushFront(&entry{key, time.Now(), value}) ele := c.ll.PushFront(&entry{key, time.Now(), buffer, written})
c.cache[key] = ele c.cache[key] = ele
c.totalSize += uint64(n) c.totalSize += uint64(written)
r.Close() r.Close()
blockingWriter.Release(nil) blockingWriter.Release(nil)
}() }()
@ -187,7 +188,8 @@ func (c *Cache) removeElement(e *list.Element) {
kv := e.Value.(*entry) kv := e.Value.(*entry)
delete(c.cache, kv.key) delete(c.cache, kv.key)
c.totalEvicted++ c.totalEvicted++
c.totalSize -= uint64(kv.value.Len()) c.totalSize -= uint64(kv.size)
kv.value.Reset()
if c.OnEvicted != nil { if c.OnEvicted != nil {
c.OnEvicted(kv.key) c.OnEvicted(kv.key)
} }

Loading…
Cancel
Save