ttl

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type TTL

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

TTL is an opaque structure that stores key/values in an internal map. These values have a time-to-live callback functions associated with them. Depending on configuration either these values are automatically deleted from the map on expiration.

func New

func New(capacity int, autoDelete bool) (*TTL, error)

New creates a new TTL map with the provided capacity. If autoDelete is set then the key will be deleted from the map on either the cancel or timeout event.

func (*TTL) Cancel

func (tm *TTL) Cancel(key any) error

Cancel aborts the waiting function and calls the remove callback.

func (*TTL) Delete

func (tm *TTL) Delete(key any) (bool, error)

Delete removes the key from the TTL map and aborts the waiting function. It also prevents callbacks from being called. It returns true if the key did expire.

func (*TTL) DeleteByValue

func (tm *TTL) DeleteByValue(find func(any) bool) int

DeleteByValue walks the TTL map and calls delete if the find callback returns true. It returns the number of deletes called.

func (*TTL) Get

func (tm *TTL) Get(key any) (any, bool, error)

Get returns the corresponding value for the provided key. It also returns true if the value was expired.

func (*TTL) Len

func (tm *TTL) Len() int

Len return the length of the TTL map.

func (*TTL) Put

func (tm *TTL) Put(pctx context.Context, ttl time.Duration, key any, val any, expired func(context.Context, any, any), remove func(context.Context, any, any))

Put inserts the provided key and value into the TTL map. The ttl values designates the duration of the validity of this key value pair. The expired function is called when the duration expires and the remove callback is called when the key is Canceled.

Jump to

Keyboard shortcuts

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