Documentation
¶
Overview ¶
Package ttl provides a Time-To-Live (TTL) list for comparable keys with expiration callbacks.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when delete does not find the item to be deleted.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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)
}