ttl

package
v0.0.0-...-19afdc0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ttl provides a Time-To-Live (TTL) list for comparable keys with expiration callbacks.

Index

Constants

This section is empty.

Variables

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

ErrNotFound is returned when delete does not find the item to be deleted.

View Source
var ErrNotRunning = errors.New("ttl is not running")

ErrNotRunning is returned when a timeout is being added or deleted to/from a ttl that has been stopped.

Functions

This section is empty.

Types

type TTL

type TTL[K comparable] struct {
	// contains filtered or unexported fields
}

TTL is a Time-To-Live list of comparable keys that exist only for the duration specified. When a key expires, TTL fires the callback specified in New.

TTL worker should be stopped manually after use with TTL.Stop.

It works by maintaining an ascending sorted queue of key timeout times that get on the next tick that is updated each time a timeout, update, put, or delete occurs. It updates a ticker that ticks at the next timeout time in the queue and fires the timeout callback. Precision is "okay"; the error is always a delay.

func New

func New[K comparable](cb func(key K)) *TTL[K]

New returns a new TTL which calls the optional cb each time a key expires.

TTL is returned started and should be stopped after use with TTL.Stop.

Parameters:

  • cb: A callback function that is called when a key expires. It receives the expired key as an argument.

Returns:

  • *TTL[K]: A pointer to the newly created TTL instance.

Example:

ttl := ttl.New[string](func(key string) {
	fmt.Println("Key expired:", key)
})
defer ttl.Stop()

func (*TTL[K]) Delete

func (self *TTL[K]) Delete(key K) error

Delete removes a key from the list. Returns ErrNotRunning if ttl is stopped.

Parameters:

  • key: The key to remove from the TTL.

Returns:

  • err: An error if the TTL is not running or if the key is not found.

Example:

err := ttl.Delete("mykey")
if err != nil {
	fmt.Println("Error deleting key:", err)
}

func (*TTL[K]) Exists

func (self *TTL[K]) Exists(key K) (exists bool)

Exists returns if key exists in TTL.

Parameters:

  • key: The key to check for existence.

Returns:

  • exists: True if the key exists in the TTL, false otherwise.

Example:

exists := ttl.Exists("mykey")
fmt.Println("Key exists:", exists)

func (*TTL[K]) Len

func (self *TTL[K]) Len() (l int)

Len returns the number of events in the list left to fire.

Returns:

  • l: The number of events in the queue, including the currently waiting event.

Example:

length := ttl.Len()
fmt.Println("Number of events in queue:", length)

func (*TTL[K]) Put

func (self *TTL[K]) Put(key K, duration time.Duration) (err error)

Put adds the key to ttl which will last for duration. If key is already present its timeout is reset to duration. Returns ErrNotRunning if ttl is stopped.

Parameters:

  • key: The key to add to the TTL.
  • duration: The duration for which the key should remain in the TTL.

Returns:

  • err: An error if the TTL is not running.

Example:

err := ttl.Put("mykey", time.Minute)
if err != nil {
	fmt.Println("Error putting key:", err)
}

func (*TTL[K]) Stop

func (self *TTL[K]) Stop() (err error)

Stop stops the worker. This method should be called on shutdown. Returns ErrNotRunning if ttl is already stopped.

Returns:

  • err: An error if the TTL is not running.

Example:

err := ttl.Stop()
if err != nil {
	fmt.Println("Error stopping TTL:", err)
}

func (*TTL[K]) Wait

func (self *TTL[K]) Wait() chan time.Time

Wait returns a channel that returns the current time when the TLL queue is empty and all events have fired.

Returns:

  • chan time.Time: A channel that will receive the current time when the TTL queue is empty.

Example:

done := ttl.Wait()
<-done
fmt.Println("TTL queue is empty")

Jump to

Keyboard shortcuts

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