Nullify list memory to nil as we remove the element

This commit is contained in:
Harshavardhana 2015-05-04 15:58:58 -07:00
parent d63064b8af
commit 63edb1e9a0
1 changed files with 7 additions and 5 deletions

View File

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