ttl

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Value interface{}
	// contains filtered or unexported fields
}

Item is an item in TTL list.

type ItemID

type ItemID uint64

ItemID is an unique item id generated by TTL when adding a new item. It is incremented sequentially, no entropy.

const InvalidItemID ItemID = 0

InvalidItemID is an invalid ItemID.

type OnItemTimeout

type OnItemTimeout func(id ItemID, value interface{})

OnItemTimeout is a prototype of a callback function to receive an item timeout notification where id will be the id of the item that timed out and value being the value of that item.

type TTL

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

TTL is a timeout queue that times out added items with a given Time To Live. TTL uses a timer to trim outdated items. Precision is low, always slightly late, a smidgen after item's real timeout, each time.

func New

func New(cb OnItemTimeout) *TTL

New returns a new TTL instance. Optional callback function cb is called when an item times out.

func (*TTL) Add

func (ttl *TTL) Add(value interface{}, timeout time.Duration) (id ItemID)

Add adds specified value to TTL with the specified timeout. Returns an id under which the item was stored.

func (*TTL) Remove

func (ttl *TTL) Remove(id ItemID) (item *Item, exists bool)

Remove removes an item with the specified id from the TTL if it exists and returns it with the truth if it was found.

func (*TTL) Reset

func (ttl *TTL) Reset(id ItemID, timeout time.Duration) (ok bool)

Reset resets the timeout for an item with the specified id. Returns truth if the item with the specified id was found and was reset.

Jump to

Keyboard shortcuts

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