imc

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](defaultExpires, cleanupInterval time.Duration) *Cache[K, V]

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than 1, the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than 1, expired items are not deleted from the cache before calling c.Clean().

func NewFrom

func NewFrom[K comparable, V any](defaultExpires, cleanupInterval time.Duration, items map[K]Item[V]) *Cache[K, V]

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than 1, the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than 1, expired items are not deleted from the cache before calling c.Clean().

NewFrom() also accepts an items map which will serve as the underlying map for the cache. This is useful for starting from a preallocated cache like make(map[string]Item, 500) to improve startup performance when the cache is expected to reach a certain minimum size.

Only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.Items() (subject to the same caveat.)

func (Cache) Add

func (c Cache) Add(k K, v V) bool

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns false otherwise.

func (Cache) AddWithTTL added in v1.0.27

func (c Cache) AddWithTTL(k K, v V, d time.Duration) bool

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns false otherwise.

func (Cache) Clean added in v1.0.27

func (c Cache) Clean()

Clean Remove all expired items from the cache.

func (Cache) Clear

func (c Cache) Clear()

Clear Remove all items from the cache.

func (Cache) Each added in v1.0.27

func (c Cache) Each(f func(K, Item[V]) bool)

Each Iterate all unexpired items in the cache to call function `f`. Function `f` returns false can break th iteration.

func (Cache) Get

func (c Cache) Get(k K) (V, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) GetWithTTL added in v1.0.27

func (c Cache) GetWithTTL(k K) (V, time.Time, bool)

GetWithTTL returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (Cache) Increment added in v1.0.15

func (c Cache) Increment(k K, n V) V

Increment an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns the incremented value.

func (Cache) Items

func (c Cache) Items() map[K]Item[V]

Items Copies all unexpired items in the cache into a new map and returns it.

func (Cache) Len added in v1.0.27

func (c Cache) Len() int

Len Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Cache) Remove added in v1.0.27

func (c Cache) Remove(k K)

Remove an item from the cache. Does nothing if the key is not in the cache.

func (Cache) Replace

func (c Cache) Replace(k K, v V) bool

Replace a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns false otherwise.

func (Cache) ReplaceWithTTL added in v1.0.27

func (c Cache) ReplaceWithTTL(k K, v V, d time.Duration) bool

Replace a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns false otherwise.

func (Cache) Set

func (c Cache) Set(k K, v V)

Set an item to the cache, replacing any existing item. The cache's default expiration time is used.

func (Cache) SetTTL added in v1.2.3

func (c Cache) SetTTL(d time.Duration)

SetTTL sets the default TTL (time-to-live) for the cache. Existing items will not be affected.

func (Cache) SetWithTTL added in v1.0.27

func (c Cache) SetWithTTL(k K, v V, d time.Duration)

Set an item to the cache, replacing any existing item. If d <= 0, the item never expires.

func (Cache) TTL added in v1.2.3

func (c Cache) TTL() time.Duration

TTL returns the default TTL (time-to-live) for the cache.

type Item

type Item[V any] struct {
	Val V     // Cache Value
	TTL int64 // Time-To-Live (time.UnixMilli())
}

func (Item[V]) Expired

func (i Item[V]) Expired() bool

Expired Returns true if the item has expired.

func (Item[V]) ExpiredAt added in v1.0.27

func (i Item[V]) ExpiredAt(t int64) bool

ExpiredAt Returns true if the item has expired at time `t`.

func (Item[V]) TimeToLive added in v1.2.9

func (i Item[V]) TimeToLive() time.Duration

TimeToLive Returns expires duration from now. Returns 0s if the item has expired. Returns -1s if the item never expires.

func (Item[V]) Working added in v1.0.27

func (i Item[V]) Working() bool

Working returns true to prevent expired

type Worker added in v1.0.27

type Worker interface {
	// Working returns true to prevent expired
	Working() bool
}

Worker interface for cached item to control it's expiration

Jump to

Keyboard shortcuts

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