cache

package
v0.31.3 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: Apache-2.0 Imports: 5 Imported by: 528

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock defines an interface for obtaining the current time

type Expiring added in v0.17.0

type Expiring struct {
	// AllowExpiredGet causes the expiration check to be skipped on Get.
	// It should only be used when a key always corresponds to the exact same value.
	// Thus when this field is true, expired keys are considered valid
	// until the next call to Set (which causes the GC to run).
	// It may not be changed concurrently with calls to Get.
	AllowExpiredGet bool
	// contains filtered or unexported fields
}

Expiring is a map whose entries expire after a per-entry timeout.

func NewExpiring added in v0.17.0

func NewExpiring() *Expiring

NewExpiring returns an initialized expiring cache.

func NewExpiringWithClock added in v0.17.0

func NewExpiringWithClock(clock clock.Clock) *Expiring

NewExpiringWithClock is like NewExpiring but allows passing in a custom clock for testing.

func (*Expiring) Delete added in v0.17.0

func (c *Expiring) Delete(key interface{})

Delete deletes an entry in the map.

func (*Expiring) Get added in v0.17.0

func (c *Expiring) Get(key interface{}) (val interface{}, ok bool)

Get looks up an entry in the cache.

func (*Expiring) Len added in v0.17.0

func (c *Expiring) Len() int

Len returns the number of items in the cache.

func (*Expiring) Set added in v0.17.0

func (c *Expiring) Set(key interface{}, val interface{}, ttl time.Duration)

Set sets a key/value/expiry entry in the map, overwriting any previous entry with the same key. The entry expires at the given expiry time, but its TTL may be lengthened or shortened by additional calls to Set(). Garbage collection of expired entries occurs during calls to Set(), however calls to Get() will not return expired entries that have not yet been garbage collected.

type LRUExpireCache

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

LRUExpireCache is a cache that ensures the mostly recently accessed keys are returned with a ttl beyond which keys are forcibly expired.

func NewLRUExpireCache

func NewLRUExpireCache(maxSize int) *LRUExpireCache

NewLRUExpireCache creates an expiring cache with the given size

func NewLRUExpireCacheWithClock

func NewLRUExpireCacheWithClock(maxSize int, clock Clock) *LRUExpireCache

NewLRUExpireCacheWithClock creates an expiring cache with the given size, using the specified clock to obtain the current time.

func (*LRUExpireCache) Add

func (c *LRUExpireCache) Add(key interface{}, value interface{}, ttl time.Duration)

Add adds the value to the cache at key with the specified maximum duration.

func (*LRUExpireCache) Get

func (c *LRUExpireCache) Get(key interface{}) (interface{}, bool)

Get returns the value at the specified key from the cache if it exists and is not expired, or returns false.

func (*LRUExpireCache) Keys

func (c *LRUExpireCache) Keys() []interface{}

Keys returns all unexpired keys in the cache.

Keep in mind that subsequent calls to Get() for any of the returned keys might return "not found".

Keys are returned ordered from least recently used to most recently used.

func (*LRUExpireCache) Remove

func (c *LRUExpireCache) Remove(key interface{})

Remove removes the specified key from the cache if it exists

func (*LRUExpireCache) RemoveAll added in v0.29.0

func (c *LRUExpireCache) RemoveAll(predicate func(key any) bool)

RemoveAll removes all keys that match predicate.

Jump to

Keyboard shortcuts

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