cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultCapacity int64 = 10000
View Source
const DefaultItemTTL = time.Minute * 5

Variables

This section is empty.

Functions

func DecorateFunction

func DecorateFunction[T any, R any](cache Cache[T, R], fn func(T) (R, error)) func(T) (R, error)

func DefaultCacheResultPredicate added in v0.5.0

func DefaultCacheResultPredicate(_ any, _ error) bool

func DefaultKeyToHash

func DefaultKeyToHash(key any) (uint64, uint64)

Types

type Cache

type Cache[K any, V any] interface {
	Name() string
	Metrics() Metrics
	EventListener() EventListener
	WithMarshalFn(func(V) any, func(any) V) Cache[K, V]
	GetOrLoad(key K, loader func(K) (V, error)) (V, error)
}

func NewCache

func NewCache[K any, V any](name string, configs ...ConfigBuilder) Cache[K, V]

type Config

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

func (*Config) String

func (config *Config) String() string

type ConfigBuilder

type ConfigBuilder func(*Config)

func WithCacheResultPredicate added in v0.5.0

func WithCacheResultPredicate(function func(any, error) bool) ConfigBuilder

func WithCapacity

func WithCapacity(capacity int64) ConfigBuilder

func WithItemTTL

func WithItemTTL(itemTTL time.Duration) ConfigBuilder

func WithKeyToHash

func WithKeyToHash(function func(any) (uint64, uint64)) ConfigBuilder

type Event

type Event interface {
	fmt.Stringer
	CacheName() string
	CreationTime() time.Time
	CacheKey() any
	EventType() EventType
}

type EventListener

type EventListener interface {
	OnCacheHitFunc(func(HitEvent)) EventListener
	OnCacheMissFunc(func(MissEvent)) EventListener
	DismissCacheHitFunc(func(HitEvent)) EventListener
	DismissCacheMissFunc(func(MissEvent)) EventListener

	OnCacheHit(fn.Consumer[HitEvent]) EventListener
	OnCacheMiss(fn.Consumer[MissEvent]) EventListener
	DismissCacheHit(fn.Consumer[HitEvent]) EventListener
	DismissCacheMiss(fn.Consumer[MissEvent]) EventListener
}

type EventType

type EventType string
const (
	OnHit  EventType = "CACHE_HIT"
	OnMiss EventType = "CACHE_MISS"
)

type HitEvent added in v0.4.1

type HitEvent interface {
	Event
}

type Metrics

type Metrics interface {
	NumberOfCacheHits() uint64
	NumberOfCacheMisses() uint64
}

type MissEvent added in v0.4.1

type MissEvent interface {
	Event
}

Jump to

Keyboard shortcuts

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