cache

package
v0.1.87 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func New

func New[V any](defaultExpiration, cleanupInterval time.Duration) *Cache[V]

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

func (*Cache[V]) Close

func (c *Cache[V]) Close()

func (*Cache[V]) Delete

func (c *Cache[V]) Delete(k string)

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

func (*Cache[V]) Flush

func (c *Cache[V]) Flush()

Delete all items from the cache.

func (*Cache[V]) Get

func (c *Cache[V]) Get(k string) (val V, ok bool)

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

func (*Cache[V]) ItemCount

func (c *Cache[V]) ItemCount() 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[V]) Iterate

func (c *Cache[V]) Iterate(it func(key string, v V))

func (*Cache[V]) Modify added in v0.1.12

func (c *Cache[V]) Modify(k string, m func(x V) (V, error)) error

func (*Cache[V]) OnEvicted

func (c *Cache[V]) OnEvicted(f func(string, V))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (*Cache[V]) Set

func (c *Cache[V]) Set(k string, x V)

Add an item to the cache, replacing any existing item, using the default expiration.

func (*Cache[V]) SetExpiration added in v0.1.13

func (c *Cache[V]) SetExpiration(k string, d time.Duration) error

func (*Cache[V]) SetWithExpiration

func (c *Cache[V]) SetWithExpiration(k string, x V, d time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

type Item

type Item[V any] struct {
	Object     V
	Expiration int64
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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