lib/ttlcache: add a constant for NotIndexed

This commit is contained in:
Daniel Nephin 2020-10-20 19:00:24 -04:00
parent 6c09ab3dd8
commit c17baadbf8
2 changed files with 8 additions and 4 deletions

View File

@ -683,7 +683,7 @@ func (c *Cache) fetch(key string, r getOptions, allowNew bool, attempt uint, ign
// If this is a new entry (not in the heap yet), then setup the // If this is a new entry (not in the heap yet), then setup the
// initial expiry information and insert. If we're already in // initial expiry information and insert. If we're already in
// the heap we do nothing since we're reusing the same entry. // the heap we do nothing since we're reusing the same entry.
if newEntry.Expiry == nil || newEntry.Expiry.Index() == -1 { if newEntry.Expiry == nil || newEntry.Expiry.Index() == ttlcache.NotIndexed {
newEntry.Expiry = c.entriesExpiryHeap.Add(key, tEntry.Opts.LastGetTTL) newEntry.Expiry = c.entriesExpiryHeap.Add(key, tEntry.Opts.LastGetTTL)
} }

View File

@ -14,9 +14,13 @@ type Entry struct {
heapIndex int heapIndex int
} }
// NotIndexed indicates that the entry does not exist in the heap. Either because
// it is nil, or because it was removed.
const NotIndexed = -1
func (c *Entry) Index() int { func (c *Entry) Index() int {
if c == nil { if c == nil {
return -1 return NotIndexed
} }
return c.heapIndex return c.heapIndex
} }
@ -66,7 +70,7 @@ func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry {
// //
// Must be synchronized by the caller. // Must be synchronized by the caller.
func (h *ExpiryHeap) Update(idx int, expiry time.Duration) { func (h *ExpiryHeap) Update(idx int, expiry time.Duration) {
if idx < 0 { if idx == NotIndexed {
// the previous entry did not have a valid index, its not in the heap // the previous entry did not have a valid index, its not in the heap
return return
} }
@ -92,7 +96,7 @@ func (h *ExpiryHeap) Remove(idx int) {
// entry. When it re-acquires the lock it needs to be informed that // entry. When it re-acquires the lock it needs to be informed that
// the entry was expired while it was fetching. Setting heapIndex to -1 // the entry was expired while it was fetching. Setting heapIndex to -1
// indicates that the entry is no longer in the heap, and must be re-added. // indicates that the entry is no longer in the heap, and must be re-added.
entry.heapIndex = -1 entry.heapIndex = NotIndexed
if idx == 0 { if idx == 0 {
h.notify() h.notify()