cache

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cache is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheFull is returned if Put fails due to cache being filled with pinned elements
	ErrCacheFull = &serviceerror.ResourceExhausted{
		Cause:   enumspb.RESOURCE_EXHAUSTED_CAUSE_SYSTEM_OVERLOADED,
		Scope:   enumspb.RESOURCE_EXHAUSTED_SCOPE_SYSTEM,
		Message: "cache capacity is fully occupied with pinned elements",
	}
	// ErrCacheItemTooLarge is returned if Put fails due to item size being larger than max cache capacity
	ErrCacheItemTooLarge = serviceerror.NewInternal("cache item size is larger than max cache capacity")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves an element based on a key, returning nil if the element
	// does not exist
	Get(key interface{}) interface{}

	// Put adds an element to the cache, returning the previous element
	Put(key interface{}, value interface{}) interface{}

	// PutIfNotExist puts a value associated with a given key if it does not exist
	PutIfNotExist(key interface{}, value interface{}) (interface{}, error)

	// Delete deletes an element in the cache
	Delete(key interface{})

	// Release decrements the ref count of a pinned element. If the ref count
	// drops to 0, the element can be evicted from the cache.
	Release(key interface{})

	// Iterator returns the iterator of the cache
	Iterator() Iterator

	// Size returns current size of the Cache, the size definition is implementation of SizeGetter interface
	// for the entry size, if the entry does not implement SizeGetter interface, the size is 1
	Size() int
}

A Cache is a generalized interface to a cache. See cache.LRU for a specific implementation (bounded cache with LRU eviction)

func New

func New(maxSize int, opts *Options) Cache

New creates a new cache with the given options

func NewLRU

func NewLRU(maxSize int, handler metrics.Handler) Cache

NewLRU creates a new LRU cache of the given size, setting initial capacity to the max size

func NewWithMetrics added in v1.25.0

func NewWithMetrics(maxSize int, opts *Options, handler metrics.Handler) Cache

NewWithMetrics creates a new cache that will emit capacity and ttl metrics. handler should be tagged with metrics.CacheTypeTag.

type Entry added in v0.3.5

type Entry interface {
	// Key represents the key
	Key() interface{}
	// Value represents the value
	Value() interface{}
	// CreateTime represents the time when the entry is created
	CreateTime() time.Time
}

Entry represents a key-value entry within the map.

type Iterator added in v0.3.5

type Iterator interface {
	// Close closes the iterator
	// and releases any allocated resources
	Close()
	// HasNext return true if there is more items to be returned
	HasNext() bool
	// Next return the next item
	Next() Entry
}

Iterator represents the interface for cache iterators.

type MockSizeGetter added in v1.21.3

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

MockSizeGetter is a mock of SizeGetter interface.

func NewMockSizeGetter added in v1.21.3

func NewMockSizeGetter(ctrl *gomock.Controller) *MockSizeGetter

NewMockSizeGetter creates a new mock instance.

func (*MockSizeGetter) CacheSize added in v1.21.3

func (m *MockSizeGetter) CacheSize() int

CacheSize mocks base method.

func (*MockSizeGetter) EXPECT added in v1.21.3

EXPECT returns an object that allows the caller to indicate expected use.

type MockSizeGetterMockRecorder added in v1.21.3

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

MockSizeGetterMockRecorder is the mock recorder for MockSizeGetter.

func (*MockSizeGetterMockRecorder) CacheSize added in v1.21.3

func (mr *MockSizeGetterMockRecorder) CacheSize() *gomock.Call

CacheSize indicates an expected call of CacheSize.

type Options

type Options struct {
	// TTL controls the time-to-live for a given cache entry.  Cache entries that
	// are older than the TTL will not be returned.
	TTL time.Duration

	// Pin prevents in-use objects from getting evicted.
	Pin bool

	// TimeSource is an optional clock to use for time-skipping and testing. If this is nil, a real clock will be used.
	TimeSource clock.TimeSource

	OnPut func(val any)

	OnEvict func(val any)
}

Options control the behavior of the cache.

type RemovedFunc

type RemovedFunc func(interface{})

RemovedFunc is a type for notifying applications when an item is scheduled for removal from the Cache. If f is a function with the appropriate signature and i is the interface{} scheduled for deletion, Cache calls go f(i)

type SimpleOptions added in v0.27.0

type SimpleOptions struct {
	// RemovedFunc is an optional function called when an element
	// is scheduled for deletion
	RemovedFunc RemovedFunc
}

SimpleOptions provides options that can be used to configure SimpleCache.

type SizeGetter added in v1.21.3

type SizeGetter interface {
	CacheSize() int
}

SizeGetter is an interface that can be implemented by cache entries to provide their size. Cache uses CacheSize() to determine the size of a cache entry. Please be aware that if the size of the cache entry changes while the cache is being used without pinning enabled, the cache won't be able to automatically adjust for this size change. In such instances, it's necessary to call Put() again to ensure the cache size remains accurate.

Jump to

Keyboard shortcuts

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