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 ¶
- type Cache
- type CacheType
- type ChainCache
- func (c *ChainCache[T]) Clear(ctx context.Context) error
- func (c *ChainCache[T]) Del(ctx context.Context, key any) error
- func (c *ChainCache[T]) Get(ctx context.Context, key any) (T, error)
- func (c *ChainCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)
- func (c *ChainCache[T]) Set(ctx context.Context, key any, obj T) error
- func (c *ChainCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error
- func (c *ChainCache[T]) Sync()
- func (c *ChainCache[T]) Wait(ctx context.Context)
- type DelegateCache
- func (c *DelegateCache[T]) Clear(ctx context.Context) error
- func (c *DelegateCache[T]) Del(ctx context.Context, key any) error
- func (c *DelegateCache[T]) Get(ctx context.Context, key any) (T, error)
- func (c *DelegateCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)
- func (c *DelegateCache[T]) Set(ctx context.Context, key any, obj T) error
- func (c *DelegateCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error
- func (c *DelegateCache[T]) Wait(ctx context.Context)
- type KeyFunc
- type KeyGetter
- type L2Cache
- func (c *L2Cache[T]) Clear(ctx context.Context) error
- func (c *L2Cache[T]) Del(ctx context.Context, key any) error
- func (c *L2Cache[T]) Get(ctx context.Context, key any) (T, error)
- func (c *L2Cache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)
- func (c *L2Cache[T]) Set(ctx context.Context, key any, obj T) error
- func (c *L2Cache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error
- func (c *L2Cache[T]) Wait(ctx context.Context)
- type L2Option
- type L2Options
- type LoadFunction
- type LoadableCache
- func (c *LoadableCache[T]) Clear(ctx context.Context) error
- func (c *LoadableCache[T]) Close() error
- func (c *LoadableCache[T]) Del(ctx context.Context, key any) error
- func (c *LoadableCache[T]) Get(ctx context.Context, key any) (T, error)
- func (c *LoadableCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)
- func (c *LoadableCache[T]) Set(ctx context.Context, key any, obj T) error
- func (c *LoadableCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error
- func (c *LoadableCache[T]) Sync()
- func (c *LoadableCache[T]) Wait(ctx context.Context)
- type MockKeyGetter
- type MockKeyGetterMockRecorder
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".
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 (*L2Cache[T]) GetWithTTL ¶
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]) SetWithTTL ¶
SetWithTTL populates the cache item using the given key and TTL.
type L2Option ¶
type L2Option func(o *L2Options)
L2Option represents a cache option function.
func L2WithDisableCache ¶
L2WithDisableCache enables or disables the local cache for L2.
func L2WithMetrics ¶
L2WithMetrics sets whether cache statistics are kept during the cache's lifetime for L2 cache.
func L2WithNumCounters ¶
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.
type LoadFunction ¶
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 ¶
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 ¶
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 ¶
func (m *MockKeyGetter) EXPECT() *MockKeyGetterMockRecorder
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.