cache

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CacheEventTypeMiss is the event type for cache misses.
	CacheEventTypeMiss = "cache_miss"
	// CacheEventTypeHit is the event type for cache hits.
	CacheEventTypeHit = "cache_hit"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a thread-safe in-memory key/value store.

func New

func New(maxItems int, interval time.Duration) *Cache

New creates a new cache with the given configuration.

func (Cache) Add

func (c Cache) Add(key string, value interface{}, expiration time.Duration) error

Add an item to the cache, existing items will not be overwritten. To overwrite existing items, use Set. If the cache is full, Add will return an error.

func (Cache) Clear

func (c Cache) Clear()

Clear all items from the cache. This reallocates the underlying array holding the items, so that the memory used by the items is reclaimed.

func (Cache) Delete

func (c Cache) Delete(key string)

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

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

DeleteExpired deletes all expired items from the cache.

func (Cache) Get

func (c Cache) Get(key string) (interface{}, bool)

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

func (Cache) GetExpiration

func (c Cache) GetExpiration(key string) time.Duration

GetExpiration returns the expiration for the given key. Returns zero if the key is not in the cache or the item has already expired.

func (Cache) HasExpired

func (c Cache) HasExpired(key string) bool

HasExpired returns true if the item has expired.

func (Cache) ItemCount

func (c Cache) ItemCount() int

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

func (Cache) Set

func (c Cache) Set(key string, value interface{}, expiration time.Duration) error

Set adds an item to the cache, replacing any existing item. If expiration is zero, the item never expires. If the cache is full, Set will return an error.

func (Cache) SetExpiration

func (c Cache) SetExpiration(key string, expiration time.Duration)

SetExpiration sets the expiration for the given key. Does nothing if the key is not in the cache.

type CacheRecorder added in v0.24.0

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

CacheRecorder is a recorder for cache events.

func MustMakeMetrics added in v0.24.0

func MustMakeMetrics() *CacheRecorder

MustMakeMetrics creates a new CacheRecorder, and registers the metrics collectors in the controller-runtime metrics registry.

func NewCacheRecorder added in v0.24.0

func NewCacheRecorder() *CacheRecorder

NewCacheRecorder returns a new CacheRecorder. The configured labels are: event_type, name, namespace. The event_type is one of:

  • "miss"
  • "hit"
  • "update"

The name is the name of the reconciled resource. The namespace is the namespace of the reconciled resource.

func (*CacheRecorder) Collectors added in v0.24.0

func (r *CacheRecorder) Collectors() []prometheus.Collector

Collectors returns the metrics.Collector objects for the CacheRecorder.

func (*CacheRecorder) DeleteCacheEvent added in v1.1.0

func (r *CacheRecorder) DeleteCacheEvent(event, name, namespace string)

DeleteCacheEvent deletes the cache event metric.

func (*CacheRecorder) IncCacheEvents added in v0.24.0

func (r *CacheRecorder) IncCacheEvents(event, name, namespace string)

IncCacheEventCount increment by 1 the cache event count for the given event type, name and namespace.

type Item

type Item struct {
	// Object is the item's value.
	Object interface{}
	// Expiration is the item's expiration time.
	Expiration int64
}

Item is an item stored in the cache.

Jump to

Keyboard shortcuts

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