cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cache is a complete Go cache library that brings you multiple ways of managing your caches.

Package cache is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] interface {
	// Set stores the object with the given key in the cache.
	Set(ctx context.Context, key any, obj T) error
	// Get retrieves the object from the cache based on the given key.
	Get(ctx context.Context, key any) (T, error)
	// SetWithTTL stores the object with the given key and time-to-live (TTL) in the cache.
	SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error
	// GetWithTTL retrieves the object and its time-to-live (TTL) from the cache based on the given key.
	GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)
	// Del deletes the object from the cache based on the given key.
	Del(ctx context.Context, key any) error
	// Clear clears the cache.
	Clear(ctx context.Context) error
	// Wait waits for any pending operations to complete.
	Wait(ctx context.Context)
}

Cache represents the interface for all caches (aggregates, metric, memory, redis, ...)

type CacheType

type CacheType string

CacheType represents the type of cache, such as "noop", "l2", "chain", or "loadable".

const (
	NoopCacheType     CacheType = "noop"
	L2CacheType       CacheType = "l2"
	ChainCacheType    CacheType = "chain"
	LoadableCacheType CacheType = "loadable"
)

func (CacheType) String

func (ct CacheType) String() string

type ChainCache

type ChainCache[T any] struct {
	// contains filtered or unexported fields
}

ChainCache represents the configuration needed by a cache aggregator.

func NewChain

func NewChain[T any](caches ...Cache[T]) *ChainCache[T]

NewChain instantiates a new cache aggregator.

func (*ChainCache[T]) Clear

func (c *ChainCache[T]) Clear(ctx context.Context) error

Clear resets all cache data.

func (*ChainCache[T]) Del

func (c *ChainCache[T]) Del(ctx context.Context, key any) error

Del removes a value from all available caches.

func (*ChainCache[T]) Get

func (c *ChainCache[T]) Get(ctx context.Context, key any) (T, error)

Get returns the obj stored in cache if it exists.

func (*ChainCache[T]) GetWithTTL

func (c *ChainCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)

GetWithTTL returns the object and its TTL from the first cache where it exists.

func (*ChainCache[T]) Set

func (c *ChainCache[T]) Set(ctx context.Context, key any, obj T) error

Set sets a value in available caches.

func (*ChainCache[T]) SetWithTTL

func (c *ChainCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error

SetWithTTL sets a value in available caches with a specified TTL.

func (*ChainCache[T]) Sync

func (c *ChainCache[T]) Sync()

Sync synchronizes a value in available caches, until a given cache layer.

func (*ChainCache[T]) Wait

func (c *ChainCache[T]) Wait(ctx context.Context)

Wait waits for all cache operations to complete.

type DelegateCache

type DelegateCache[T any] struct {
	// contains filtered or unexported fields
}

DelegateCache is a representative cache used to represent the store. By representing the store, different stores can be encapsulated into a unified cache and perform some unified operations.

func New

func New[T any](store store.Store) *DelegateCache[T]

New instantiates a new delegate cache entry.

func (*DelegateCache[T]) Clear

func (c *DelegateCache[T]) Clear(ctx context.Context) error

Clear resets all cache data.

func (*DelegateCache[T]) Del

func (c *DelegateCache[T]) Del(ctx context.Context, key any) error

Del removes the cache item using the given key.

func (*DelegateCache[T]) Get

func (c *DelegateCache[T]) Get(ctx context.Context, key any) (T, error)

Get returns the obj stored in cache if it exists.

func (*DelegateCache[T]) GetWithTTL

func (c *DelegateCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)

GetWithTTL returns the obj stored in cache and its corresponding TTL.

func (*DelegateCache[T]) Set

func (c *DelegateCache[T]) Set(ctx context.Context, key any, obj T) error

Set populates the cache item using the given key.

func (*DelegateCache[T]) SetWithTTL

func (c *DelegateCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error

SetWithTTL populates the cache item using the given key with a specified TTL.

func (*DelegateCache[T]) Wait

func (c *DelegateCache[T]) Wait(ctx context.Context)

Wait waits for all cache operations to complete.

type KeyFunc

type KeyFunc func(obj interface{}) (string, error)

KeyFunc knows how to make a key from an object. Implementations should be deterministic.

type KeyGetter

type KeyGetter interface {
	CacheKey() string
}

KeyGetter is an interface for objects that can provide a cache key.

type L2Cache

type L2Cache[T any] struct {
	// contains filtered or unexported fields
}

L2Cache represents a two-level cache configuration.

func NewL2

func NewL2[T any](remote Cache[T], options ...L2Option) *L2Cache[T]

NewL2 instantiates a new L2 cache.

func (*L2Cache[T]) Clear

func (c *L2Cache[T]) Clear(ctx context.Context) error

Clear resets all cache data.

func (*L2Cache[T]) Del

func (c *L2Cache[T]) Del(ctx context.Context, key any) error

Del removes the cache item using the given key.

func (*L2Cache[T]) Get

func (c *L2Cache[T]) Get(ctx context.Context, key any) (T, error)

Get returns the obj stored in cache if it exists.

func (*L2Cache[T]) GetWithTTL

func (c *L2Cache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)

GetWithTTL returns the obj stored in cache and its corresponding TTL, also a bool that is true if the item was found and is not expired.

func (*L2Cache[T]) Set

func (c *L2Cache[T]) Set(ctx context.Context, key any, obj T) error

Set populates the cache item using the given key.

func (*L2Cache[T]) SetWithTTL

func (c *L2Cache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error

SetWithTTL populates the cache item using the given key and TTL.

func (*L2Cache[T]) Wait

func (c *L2Cache[T]) Wait(ctx context.Context)

// Wait waits for all cache operations to complete.

type L2Option

type L2Option func(o *L2Options)

L2Option represents a cache option function.

func L2WithDisableCache

func L2WithDisableCache(disable bool) L2Option

L2WithDisableCache enables or disables the local cache for L2.

func L2WithMetrics

func L2WithMetrics(enable bool) L2Option

L2WithMetrics sets whether cache statistics are kept during the cache's lifetime for L2 cache.

func L2WithNumCounters

func L2WithNumCounters(numCounters int64) L2Option

L2WithNumCounters sets the number of counters for L2 cache.

type L2Options

type L2Options struct {
	// Disable local cache. To enable or disable the local cache,
	// you need to restart the service.
	Disable bool

	// NumCounters determines the number of counters (keys) to keep that hold
	// access frequency information. It's generally a good idea to have more
	// counters than the max cache capacity, as this will improve eviction
	// accuracy and subsequent hit ratios.
	//
	// For example, if you expect your cache to hold 1,000,000 items when full,
	// NumCounters should be 10,000,000 (10x). Each counter takes up roughly
	// 3 bytes (4 bits for each counter * 4 copies plus about a byte per
	// counter for the bloom filter). Note that the number of counters is
	// internally rounded up to the nearest power of 2, so the space usage
	// may be a little larger than 3 bytes * NumCounters.
	NumCounters int64
	// MaxCost can be considered as the cache capacity, in whatever units you
	// choose to use.
	//
	// For example, if you want the cache to have a max capacity of 100MB, you
	// would set MaxCost to 100,000,000 and pass an item's number of bytes as
	// the `cost` parameter for calls to Set. If new items are accepted, the
	// eviction process will take care of making room for the new item and not
	// overflowing the MaxCost value.
	MaxCost int64
	// BufferItems determines the size of Get buffers.
	//
	// Unless you have a rare use case, using `64` as the BufferItems value
	// results in good performance.
	BufferItems int64
	// Metrics determines whether cache statistics are kept during the cache's
	// lifetime. There *is* some overhead to keeping statistics, so you should
	// only set this flag to true when testing or throughput performance isn't a
	// major factor.
	Metrics bool
}

L2Options represents the options for L2 cache configuration.

func NewL2Options

func NewL2Options() *L2Options

NewL2Options instantiates a new L2Options with default values.

func (*L2Options) ApplyTo

func (o *L2Options) ApplyTo(cfg *ristretto.Config)

ApplyTo applies the L2Options to a ristretto.Config.

type LoadFunction

type LoadFunction[T any] func(ctx context.Context, key any) (T, error)

LoadFunction is a function type for loading data into the cache.

type LoadableCache

type LoadableCache[T any] struct {
	// contains filtered or unexported fields
}

LoadableCache represents a cache that uses a function to load data.

func NewLoadable

func NewLoadable[T any](loadFunc LoadFunction[T], cache Cache[T]) *LoadableCache[T]

NewLoadable instanciates a new cache that uses a function to load data.

func (*LoadableCache[T]) Clear

func (c *LoadableCache[T]) Clear(ctx context.Context) error

Clear resets all cache data.

func (*LoadableCache[T]) Close

func (c *LoadableCache[T]) Close() error

Close closes the setChannel and waits for all operations to finish.

func (*LoadableCache[T]) Del

func (c *LoadableCache[T]) Del(ctx context.Context, key any) error

Del removes a value from cache.

func (*LoadableCache[T]) Get

func (c *LoadableCache[T]) Get(ctx context.Context, key any) (T, error)

Get returns the obj stored in cache if it exists.

func (*LoadableCache[T]) GetWithTTL

func (c *LoadableCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)

GetWithTTL retrieves the object from the cache with its time to live (TTL) or loads it using the load function if not found.

func (*LoadableCache[T]) Set

func (c *LoadableCache[T]) Set(ctx context.Context, key any, obj T) error

Set sets a value in available caches.

func (*LoadableCache[T]) SetWithTTL

func (c *LoadableCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error

SetWithTTL sets a value in the cache with a specified time to live (TTL).

func (*LoadableCache[T]) Sync

func (c *LoadableCache[T]) Sync()

Sync processes items in the setChannel and sets them in the cache.

func (*LoadableCache[T]) Wait

func (c *LoadableCache[T]) Wait(ctx context.Context)

Wait waits for all operations to finish.

type MockKeyGetter

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

MockKeyGetter is a mock of KeyGetter interface.

func NewMockKeyGetter

func NewMockKeyGetter(ctrl *gomock.Controller) *MockKeyGetter

NewMockKeyGetter creates a new mock instance.

func (*MockKeyGetter) CacheKey

func (m *MockKeyGetter) CacheKey() string

CacheKey mocks base method.

func (*MockKeyGetter) EXPECT

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

type MockKeyGetterMockRecorder

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

MockKeyGetterMockRecorder is the mock recorder for MockKeyGetter.

func (*MockKeyGetterMockRecorder) CacheKey

func (mr *MockKeyGetterMockRecorder) CacheKey() *gomock.Call

CacheKey indicates an expected call of CacheKey.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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