ttlcache

package
v1.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2022 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package ttlcache provides an ExpiryHeap that can be used by a cache to track the expiration time of its entries. When an expiry is reached the Timer will fire and the entry can be removed.

Index

Constants

View Source
const NotIndexed = -1

NotIndexed indicates that the entry does not exist in the heap. Either because it is nil, or because it was removed.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry in the ExpiryHeap, tracks the index and expiry time of an item in a ttl cache.

func (*Entry) Index

func (e *Entry) Index() int

Index returns the index of this entry within the heap.

func (*Entry) Key

func (e *Entry) Key() string

Key returns the key for the entry in the heap.

type ExpiryHeap

type ExpiryHeap struct {

	// 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{}
	// contains filtered or unexported fields
}

ExpiryHeap is a heap that is ordered by the expiry time of entries. It may be used by a cache or storage to expiry items after a TTL.

ExpiryHeap expects the caller to synchronize calls to most of its methods. This is necessary because the cache needs to ensure that updates to both its storage and the ExpiryHeap are synchronized.

func NewExpiryHeap

func NewExpiryHeap() *ExpiryHeap

NewExpiryHeap creates and returns a new ExpiryHeap.

func (*ExpiryHeap) Add

func (h *ExpiryHeap) Add(key string, expiry time.Duration) *Entry

Add an entry to the heap.

Must be synchronized by the caller.

func (*ExpiryHeap) Next

func (h *ExpiryHeap) Next() Timer

Next returns a Timer that waits until the first entry in the heap expires.

Must be synchronized by the caller.

func (*ExpiryHeap) Remove

func (h *ExpiryHeap) Remove(idx int)

Remove the entry at idx from the heap.

Must be synchronized by the caller.

func (*ExpiryHeap) Update

func (h *ExpiryHeap) Update(idx int, expiry time.Duration)

Update the entry that is currently at idx with the new expiry time, if the new expiry time is further into the future. The heap will be rebalanced after the entry is updated. If the new expiry time is earlier than the existing expiry time than the expiry is not modified.

Must be synchronized by the caller.

type Timer

type Timer struct {
	Entry *Entry
	// contains filtered or unexported fields
}

Timer provides a channel to block on. When the Wait channel receives an item the Timer.Entry has expired. The caller is expected to call ExpiryHeap.Remove with the Entry.Index().

The caller is responsible for calling Stop to stop the timer if the timer has not fired.

func (*Timer) Stop

func (t *Timer) Stop()

func (*Timer) Wait

func (t *Timer) Wait() <-chan time.Time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL