Documentation ¶
Index ¶
- Variables
- type Callback
- type Element
- type TimedMap
- func (tm *TimedMap[TKey, TVal]) Contains(key TKey) bool
- func (tm *TimedMap[TKey, TVal]) Flush()
- func (tm *TimedMap[TKey, TVal]) GetExpires(key TKey) (time.Time, error)
- func (tm *TimedMap[TKey, TVal]) GetValue(key TKey) (val TVal, ok bool)
- func (tm *TimedMap[TKey, TVal]) Refresh(key TKey, d time.Duration) error
- func (tm *TimedMap[TKey, TVal]) Remove(key TKey)
- func (tm *TimedMap[TKey, TVal]) Set(key TKey, value TVal, expiresAfter time.Duration, cb ...Callback[TVal])
- func (tm *TimedMap[TKey, TVal]) SetExpires(key TKey, d time.Duration) error
- func (tm *TimedMap[TKey, TVal]) Size() int
- func (tm *TimedMap[TKey, TVal]) Snapshot() map[TKey]TVal
- func (tm *TimedMap[TKey, TVal]) StartCleanerExternal(initiator <-chan time.Time)
- func (tm *TimedMap[TKey, TVal]) StartCleanerInternal(interval time.Duration)
- func (tm *TimedMap[TKey, TVal]) StopCleaner()
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyNotFound is returned when a key was // requested which is not present in the map. ErrKeyNotFound = errors.New("key not found") // ErrValueNoMap is returned when a value passed // expected was of another type. ErrValueNoMap = errors.New("value is not of type map") )
Functions ¶
This section is empty.
Types ¶
type Callback ¶
type Callback[TVal any] func(value TVal)
Callback is a function which can be called when a key-value-pair has expired.
type Element ¶
type Element[TVal any] struct { // contains filtered or unexported fields }
Element contains the actual value as interface type, the time when the value expires and an array of callbacks, which will be executed when the Element expires.
type TimedMap ¶
type TimedMap[TKey comparable, TVal any] struct { // contains filtered or unexported fields }
TimedMap is a key-value map with lifetimes attached to values. Expired values are removed on access or via a cleanup coroutine, which can be enabled via the StartCleanerInternal method.
func FromMap ¶
func FromMap[TKey comparable, TVal any]( m map[TKey]TVal, expiration time.Duration, cleanupTickTime time.Duration, tickerChan ...<-chan time.Time, ) (*TimedMap[TKey, TVal], error)
FromMap creates a new TimedMap containing all entries from the passed map m. Each entry will get assigned the passed expiration duration.
func New ¶
func New[TKey comparable, TVal any](cleanupTickTime time.Duration, tickerChan ...<-chan time.Time) *TimedMap[TKey, TVal]
New creates and returns a new instance of TimedMap. The passed cleanupTickTime will be passed to the cleanup ticker, which iterates through the map and deletes expired key-value pairs on each iteration.
Optionally, you can also pass a custom <-chan time.Time, which controls the cleanup cycle if you want to use a single synchronized timer or if you want to have more granular control over the cleanup loop.
When passing 0 as cleanupTickTime and no tickerChan, the cleanup loop will not be started. You can call StartCleanerInternal or StartCleanerExternal to manually start the cleanup loop. These both methods can also be used to re-define the specification of the cleanup loop when already running if you want to.
func (*TimedMap[TKey, TVal]) Contains ¶
Contains returns true, if the key exists in the map. false will be returned, if there is no value to the key or if the key-value pair was expired.
func (*TimedMap[TKey, TVal]) Flush ¶
func (tm *TimedMap[TKey, TVal]) Flush()
Flush deletes all key-value pairs of the map.
func (*TimedMap[TKey, TVal]) GetExpires ¶
GetExpires returns the expiry time of a key-value pair. If the key-value pair does not exist in the map or was expired, this will return an error object.
func (*TimedMap[TKey, TVal]) GetValue ¶
GetValue returns an interface of the value of a key in the map. The returned value is nil if there is no value to the passed key or if the value was expired.
func (*TimedMap[TKey, TVal]) Refresh ¶
Refresh extends the expiry time for a key-value pair about the passed duration. If there is no value to the key passed, this will return an error object.
func (*TimedMap[TKey, TVal]) Remove ¶
func (tm *TimedMap[TKey, TVal]) Remove(key TKey)
Remove deletes a key-value pair in the map.
func (*TimedMap[TKey, TVal]) Set ¶
func (tm *TimedMap[TKey, TVal]) Set(key TKey, value TVal, expiresAfter time.Duration, cb ...Callback[TVal])
Set appends a key-value pair to the map or sets the value of a key. expiresAfter sets the expiry time after the key-value pair will automatically be removed from the map.
func (*TimedMap[TKey, TVal]) SetExpires ¶
SetExpires sets the expiry time for a key-value pair to the passed duration. If there is no value to the key passed , this will return an error.
func (*TimedMap[TKey, TVal]) Size ¶
Size returns the current number of key-value pairs existent in the map.
func (*TimedMap[TKey, TVal]) Snapshot ¶
func (tm *TimedMap[TKey, TVal]) Snapshot() map[TKey]TVal
Snapshot returns a new map which represents the current key-value state of the internal container.
func (*TimedMap[TKey, TVal]) StartCleanerExternal ¶
StartCleanerExternal starts the cleanup loop controlled by the given initiator channel. This is useful if you want to have more control over the cleanup loop or if you want to sync up multiple TimedMaps.
If the cleanup loop is already running, it will be stopped and restarted using the new specification.
func (*TimedMap[TKey, TVal]) StartCleanerInternal ¶
StartCleanerInternal starts the cleanup loop controlled by an internal ticker with the given interval.
If the cleanup loop is already running, it will be stopped and restarted using the new specification.
func (*TimedMap[TKey, TVal]) StopCleaner ¶
func (tm *TimedMap[TKey, TVal]) StopCleaner()
StopCleaner stops the cleaner go routine and timer. This should always be called after exiting a scope where TimedMap is used that the data can be cleaned up correctly.