imc

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New(defaultExpires, cleanupInterval time.Duration) *Cache

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.DeleteExpired().

func NewFrom

func NewFrom(defaultExpires, cleanupInterval time.Duration, items map[string]Item) *Cache

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.DeleteExpired().

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 string, x any, d ...time.Duration) error

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 an error otherwise.

func (Cache) Clear

func (c Cache) Clear()

Delete all items from the cache.

func (Cache) Count

func (c Cache) Count() int

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

func (Cache) Decrement added in v1.0.15

func (c Cache) Decrement(k string, n any, x ...any) error

Decrement an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found and default value 'x[0]' is not supplied, or if it is not possible to decrement it by n.

func (Cache) Delete

func (c Cache) Delete(k string)

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

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (any, bool)

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

func (Cache) GetWithExpires added in v1.0.15

func (c Cache) GetWithExpires(k string) (any, time.Time, bool)

GetWithExpires 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 string, n any, x ...any) error

Increment an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found and default value 'x[0]' is not supplied, or if it is not possible to increment it by n.

func (Cache) Items

func (c Cache) Items() map[string]Item

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

func (Cache) Replace

func (c Cache) Replace(k string, x any, d ...time.Duration) error

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

func (Cache) Set

func (c Cache) Set(k string, x any, d ...time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0, the cache's default expiration time is used. If it < 0, the item never expires.

type Item

type Item struct {
	Object  any   // Cache Object
	Expires int64 // UnixNano
}

func (Item) Expired

func (item Item) Expired() bool

Returns true if the item has expired.

Jump to

Keyboard shortcuts

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