2020-10-20 22:34:42 +00:00
|
|
|
package ttlcache
|
2020-10-20 17:19:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Entry in the ExpiryHeap, tracks the index and expiry time of an item in a
|
|
|
|
// ttl cache.
|
|
|
|
type Entry struct {
|
|
|
|
// TODO: can Key be unexported?
|
|
|
|
Key string
|
|
|
|
expiry time.Time
|
|
|
|
heapIndex int
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:00:24 +00:00
|
|
|
// NotIndexed indicates that the entry does not exist in the heap. Either because
|
|
|
|
// it is nil, or because it was removed.
|
|
|
|
const NotIndexed = -1
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
func (c *Entry) Index() int {
|
|
|
|
if c == nil {
|
2020-10-20 23:00:24 +00:00
|
|
|
return NotIndexed
|
2020-10-20 22:34:42 +00:00
|
|
|
}
|
|
|
|
return c.heapIndex
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
// ExpiryHeap is a container/heap.Interface implementation that expires entries
|
2020-10-20 17:19:21 +00:00
|
|
|
// in the cache when their expiration time is reached.
|
|
|
|
//
|
|
|
|
// All operations on the heap and read/write of the heap contents require
|
|
|
|
// the proper entriesLock to be held on Cache.
|
2020-10-20 21:38:12 +00:00
|
|
|
type ExpiryHeap struct {
|
2020-10-20 22:34:42 +00:00
|
|
|
entries []*Entry
|
2020-10-20 17:19:21 +00:00
|
|
|
|
|
|
|
// NotifyCh is sent a value whenever the 0 index value of the heap
|
|
|
|
// changes. This can be used to detect when the earliest value
|
|
|
|
// changes.
|
|
|
|
NotifyCh chan struct{}
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// NewExpiryHeap creates and returns a new ExpiryHeap.
|
2020-10-20 21:38:12 +00:00
|
|
|
func NewExpiryHeap() *ExpiryHeap {
|
|
|
|
h := &ExpiryHeap{NotifyCh: make(chan struct{}, 1)}
|
|
|
|
heap.Init((*entryHeap)(h))
|
2020-10-20 17:59:53 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:57:22 +00:00
|
|
|
// Add an entry to the heap.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 22:34:42 +00:00
|
|
|
func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry {
|
|
|
|
entry := &Entry{
|
|
|
|
Key: key,
|
|
|
|
expiry: time.Now().Add(expiry),
|
2020-10-20 18:57:22 +00:00
|
|
|
// Set the initial heap index to the last index. If the entry is swapped it
|
|
|
|
// will have the correct index set, and if it remains at the end the last
|
|
|
|
// index will be correct.
|
2020-10-20 22:34:42 +00:00
|
|
|
heapIndex: len(h.entries),
|
2020-10-20 18:57:22 +00:00
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
heap.Push((*entryHeap)(h), entry)
|
2020-10-20 22:34:42 +00:00
|
|
|
if entry.heapIndex == 0 {
|
2020-10-20 18:57:22 +00:00
|
|
|
h.notify()
|
|
|
|
}
|
2020-10-20 17:19:21 +00:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the entry that is currently at idx with the new expiry time. The heap
|
|
|
|
// will be rebalanced after the entry is updated.
|
|
|
|
//
|
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Update(idx int, expiry time.Duration) {
|
2020-10-20 23:00:24 +00:00
|
|
|
if idx == NotIndexed {
|
2020-10-20 22:58:30 +00:00
|
|
|
// the previous entry did not have a valid index, its not in the heap
|
|
|
|
return
|
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
entry := h.entries[idx]
|
2020-10-20 22:34:42 +00:00
|
|
|
entry.expiry = time.Now().Add(expiry)
|
2020-10-20 21:38:12 +00:00
|
|
|
heap.Fix((*entryHeap)(h), idx)
|
2020-10-20 17:19:21 +00:00
|
|
|
|
|
|
|
// If the previous index and current index are both zero then Fix did not
|
|
|
|
// swap the entry, and notify must be called here.
|
2020-10-20 22:34:42 +00:00
|
|
|
if idx == 0 || entry.heapIndex == 0 {
|
2020-10-20 17:19:21 +00:00
|
|
|
h.notify()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Remove the entry at idx from the heap.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Remove(idx int) {
|
|
|
|
entry := h.entries[idx]
|
|
|
|
heap.Remove((*entryHeap)(h), idx)
|
2020-10-20 17:59:53 +00:00
|
|
|
|
|
|
|
// A goroutine which is fetching a new value will have a reference to this
|
|
|
|
// entry. When it re-acquires the lock it needs to be informed that
|
2020-10-20 22:34:42 +00:00
|
|
|
// the entry was expired while it was fetching. Setting heapIndex to -1
|
2020-10-20 17:59:53 +00:00
|
|
|
// indicates that the entry is no longer in the heap, and must be re-added.
|
2020-10-20 23:00:24 +00:00
|
|
|
entry.heapIndex = NotIndexed
|
2020-10-20 18:57:22 +00:00
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
h.notify()
|
|
|
|
}
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
type entryHeap ExpiryHeap
|
2020-10-20 17:19:21 +00:00
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Len() int { return len(h.entries) }
|
|
|
|
|
|
|
|
func (h *entryHeap) Swap(i, j int) {
|
|
|
|
h.entries[i], h.entries[j] = h.entries[j], h.entries[i]
|
2020-10-20 22:34:42 +00:00
|
|
|
h.entries[i].heapIndex = i
|
|
|
|
h.entries[j].heapIndex = j
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Less(i, j int) bool {
|
2020-10-20 17:19:21 +00:00
|
|
|
// The usage of Before here is important (despite being obvious):
|
|
|
|
// this function uses the monotonic time that should be available
|
|
|
|
// on the time.Time value so the heap is immune to wall clock changes.
|
2020-10-20 22:34:42 +00:00
|
|
|
return h.entries[i].expiry.Before(h.entries[j].expiry)
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// heap.Interface, this isn't expected to be called directly.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Push(x interface{}) {
|
2020-10-20 22:34:42 +00:00
|
|
|
h.entries = append(h.entries, x.(*Entry))
|
2020-10-20 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// heap.Interface, this isn't expected to be called directly.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *entryHeap) Pop() interface{} {
|
|
|
|
n := len(h.entries)
|
|
|
|
entries := h.entries
|
2020-10-20 17:19:21 +00:00
|
|
|
last := entries[n-1]
|
2020-10-20 21:38:12 +00:00
|
|
|
h.entries = entries[0 : n-1]
|
2020-10-20 17:19:21 +00:00
|
|
|
return last
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:57:22 +00:00
|
|
|
// notify the timer that the head value has changed, so the expiry time has
|
|
|
|
// also likely changed.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) notify() {
|
2020-10-20 18:57:22 +00:00
|
|
|
// Send to channel without blocking. Skips sending if there is already
|
|
|
|
// an item in the buffered channel.
|
2020-10-20 17:19:21 +00:00
|
|
|
select {
|
|
|
|
case h.NotifyCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 17:59:53 +00:00
|
|
|
|
2020-10-20 22:34:42 +00:00
|
|
|
// Next returns a Timer that waits until the first entry in the heap expires.
|
|
|
|
//
|
2020-10-20 17:59:53 +00:00
|
|
|
// Must be synchronized by the caller.
|
2020-10-20 21:38:12 +00:00
|
|
|
func (h *ExpiryHeap) Next() Timer {
|
|
|
|
if len(h.entries) == 0 {
|
|
|
|
return Timer{}
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
2020-10-20 21:38:12 +00:00
|
|
|
entry := h.entries[0]
|
|
|
|
return Timer{
|
2020-10-20 22:34:42 +00:00
|
|
|
timer: time.NewTimer(time.Until(entry.expiry)),
|
2020-10-20 17:59:53 +00:00
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
type Timer struct {
|
2020-10-20 17:59:53 +00:00
|
|
|
timer *time.Timer
|
2020-10-20 22:34:42 +00:00
|
|
|
Entry *Entry
|
2020-10-20 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (t *Timer) Wait() <-chan time.Time {
|
2020-10-20 17:59:53 +00:00
|
|
|
if t.timer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.timer.C
|
|
|
|
}
|
|
|
|
|
2020-10-20 21:38:12 +00:00
|
|
|
func (t *Timer) Stop() {
|
2020-10-20 17:59:53 +00:00
|
|
|
if t.timer != nil {
|
|
|
|
t.timer.Stop()
|
|
|
|
}
|
|
|
|
}
|