{"modulePath":"github.com/onexstack/onex","version":"v0.2.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cache\n\nPackage cache is a complete Go cache library that brings you multiple ways of managing your caches.\n\nPackage cache is a generated GoMock package.\n\n## Types\n\n```go\ntype Cache[T any] interface {\n\t// Set stores the object with the given key in the cache.\n\tSet(ctx context.Context, key any, obj T) error\n\t// Get retrieves the object from the cache based on the given key.\n\tGet(ctx context.Context, key any) (T, error)\n\t// SetWithTTL stores the object with the given key and time-to-live (TTL) in the cache.\n\tSetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error\n\t// GetWithTTL retrieves the object and its time-to-live (TTL) from the cache based on the given key.\n\tGetWithTTL(ctx context.Context, key any) (T, time.Duration, error)\n\t// Del deletes the object from the cache based on the given key.\n\tDel(ctx context.Context, key any) error\n\t// Clear clears the cache.\n\tClear(ctx context.Context) error\n\t// Wait waits for any pending operations to complete.\n\tWait(ctx context.Context)\n}\n```\nCache represents the interface for all caches (aggregates, metric, memory, redis, ...)\n\n```go\ntype CacheType string\n```\nCacheType represents the type of cache, such as \"noop\", \"l2\", \"chain\", or \"loadable\".\n\n```go\nconst (\n\tNoopCacheType     CacheType = \"noop\"\n\tL2CacheType       CacheType = \"l2\"\n\tChainCacheType    CacheType = \"chain\"\n\tLoadableCacheType CacheType = \"loadable\"\n)\n```\n\n```go\nfunc (ct CacheType) String() string\n```\n\n```go\ntype ChainCache[T any] struct {\n\t// contains filtered or unexported fields\n}\n```\nChainCache represents the configuration needed by a cache aggregator.\n\n```go\nfunc NewChain[T any](caches ...Cache[T]) *ChainCache[T]\n```\nNewChain instantiates a new cache aggregator.\n\n```go\nfunc (c *ChainCache[T]) Clear(ctx context.Context) error\n```\nClear resets all cache data.\n\n```go\nfunc (c *ChainCache[T]) Del(ctx context.Context, key any) error\n```\nDel removes a value from all available caches.\n\n```go\nfunc (c *ChainCache[T]) Get(ctx context.Context, key any) (T, error)\n```\nGet returns the obj stored in cache if it exists.\n\n```go\nfunc (c *ChainCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)\n```\nGetWithTTL returns the object and its TTL from the first cache where it exists.\n\n```go\nfunc (c *ChainCache[T]) Set(ctx context.Context, key any, obj T) error\n```\nSet sets a value in available caches.\n\n```go\nfunc (c *ChainCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error\n```\nSetWithTTL sets a value in available caches with a specified TTL.\n\n```go\nfunc (c *ChainCache[T]) Sync()\n```\nSync synchronizes a value in available caches, until a given cache layer.\n\n```go\nfunc (c *ChainCache[T]) Wait(ctx context.Context)\n```\nWait waits for all cache operations to complete.\n\n```go\ntype DelegateCache[T any] struct {\n\t// contains filtered or unexported fields\n}\n```\nDelegateCache 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.\n\n```go\nfunc New[T any](store store.Store) *DelegateCache[T]\n```\nNew instantiates a new delegate cache entry.\n\n```go\nfunc (c *DelegateCache[T]) Clear(ctx context.Context) error\n```\nClear resets all cache data.\n\n```go\nfunc (c *DelegateCache[T]) Del(ctx context.Context, key any) error\n```\nDel removes the cache item using the given key.\n\n```go\nfunc (c *DelegateCache[T]) Get(ctx context.Context, key any) (T, error)\n```\nGet returns the obj stored in cache if it exists.\n\n```go\nfunc (c *DelegateCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)\n```\nGetWithTTL returns the obj stored in cache and its corresponding TTL.\n\n```go\nfunc (c *DelegateCache[T]) Set(ctx context.Context, key any, obj T) error\n```\nSet populates the cache item using the given key.\n\n```go\nfunc (c *DelegateCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error\n```\nSetWithTTL populates the cache item using the given key with a specified TTL.\n\n```go\nfunc (c *DelegateCache[T]) Wait(ctx context.Context)\n```\nWait waits for all cache operations to complete.\n\n```go\ntype KeyFunc func(obj any) (string, error)\n```\nKeyFunc knows how to make a key from an object. Implementations should be deterministic.\n\n```go\ntype KeyGetter interface {\n\tCacheKey() string\n}\n```\nKeyGetter is an interface for objects that can provide a cache key.\n\n```go\ntype L2Cache[T any] struct {\n\t// contains filtered or unexported fields\n}\n```\nL2Cache represents a two-level cache configuration.\n\n```go\nfunc NewL2[T any](remote Cache[T], options ...L2Option) *L2Cache[T]\n```\nNewL2 instantiates a new L2 cache.\n\n```go\nfunc (c *L2Cache[T]) Clear(ctx context.Context) error\n```\nClear resets all cache data.\n\n```go\nfunc (c *L2Cache[T]) Del(ctx context.Context, key any) error\n```\nDel removes the cache item using the given key.\n\n```go\nfunc (c *L2Cache[T]) Get(ctx context.Context, key any) (T, error)\n```\nGet returns the obj stored in cache if it exists.\n\n```go\nfunc (c *L2Cache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)\n```\nGetWithTTL 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.\n\n```go\nfunc (c *L2Cache[T]) Set(ctx context.Context, key any, obj T) error\n```\nSet populates the cache item using the given key.\n\n```go\nfunc (c *L2Cache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error\n```\nSetWithTTL populates the cache item using the given key and TTL.\n\n```go\nfunc (c *L2Cache[T]) Wait(ctx context.Context)\n```\n// Wait waits for all cache operations to complete.\n\n```go\ntype L2Option func(o *L2Options)\n```\nL2Option represents a cache option function.\n\n```go\nfunc L2WithDisableCache(disable bool) L2Option\n```\nL2WithDisableCache enables or disables the local cache for L2.\n\n```go\nfunc L2WithMetrics(enable bool) L2Option\n```\nL2WithMetrics sets whether cache statistics are kept during the cache's lifetime for L2 cache.\n\n```go\nfunc L2WithNumCounters(numCounters int64) L2Option\n```\nL2WithNumCounters sets the number of counters for L2 cache.\n\n```go\ntype L2Options struct {\n\t// Disable local cache. To enable or disable the local cache,\n\t// you need to restart the service.\n\tDisable bool\n\n\t// NumCounters determines the number of counters (keys) to keep that hold\n\t// access frequency information. It's generally a good idea to have more\n\t// counters than the max cache capacity, as this will improve eviction\n\t// accuracy and subsequent hit ratios.\n\t//\n\t// For example, if you expect your cache to hold 1,000,000 items when full,\n\t// NumCounters should be 10,000,000 (10x). Each counter takes up roughly\n\t// 3 bytes (4 bits for each counter * 4 copies plus about a byte per\n\t// counter for the bloom filter). Note that the number of counters is\n\t// internally rounded up to the nearest power of 2, so the space usage\n\t// may be a little larger than 3 bytes * NumCounters.\n\tNumCounters int64\n\t// MaxCost can be considered as the cache capacity, in whatever units you\n\t// choose to use.\n\t//\n\t// For example, if you want the cache to have a max capacity of 100MB, you\n\t// would set MaxCost to 100,000,000 and pass an item's number of bytes as\n\t// the `cost` parameter for calls to Set. If new items are accepted, the\n\t// eviction process will take care of making room for the new item and not\n\t// overflowing the MaxCost value.\n\tMaxCost int64\n\t// BufferItems determines the size of Get buffers.\n\t//\n\t// Unless you have a rare use case, using `64` as the BufferItems value\n\t// results in good performance.\n\tBufferItems int64\n\t// Metrics determines whether cache statistics are kept during the cache's\n\t// lifetime. There *is* some overhead to keeping statistics, so you should\n\t// only set this flag to true when testing or throughput performance isn't a\n\t// major factor.\n\tMetrics bool\n}\n```\nL2Options represents the options for L2 cache configuration.\n\n```go\nfunc NewL2Options() *L2Options\n```\nNewL2Options instantiates a new L2Options with default values.\n\n```go\nfunc (o *L2Options) ApplyTo(cfg *ristretto.Config)\n```\nApplyTo applies the L2Options to a ristretto.Config.\n\n```go\ntype LoadFunction[T any] func(ctx context.Context, key any) (T, error)\n```\nLoadFunction is a function type for loading data into the cache.\n\n```go\ntype LoadableCache[T any] struct {\n\t// contains filtered or unexported fields\n}\n```\nLoadableCache represents a cache that uses a function to load data.\n\n```go\nfunc NewLoadable[T any](loadFunc LoadFunction[T], cache Cache[T]) *LoadableCache[T]\n```\nNewLoadable instanciates a new cache that uses a function to load data.\n\n```go\nfunc (c *LoadableCache[T]) Clear(ctx context.Context) error\n```\nClear resets all cache data.\n\n```go\nfunc (c *LoadableCache[T]) Close() error\n```\nClose closes the setChannel and waits for all operations to finish.\n\n```go\nfunc (c *LoadableCache[T]) Del(ctx context.Context, key any) error\n```\nDel removes a value from cache.\n\n```go\nfunc (c *LoadableCache[T]) Get(ctx context.Context, key any) (T, error)\n```\nGet returns the obj stored in cache if it exists.\n\n```go\nfunc (c *LoadableCache[T]) GetWithTTL(ctx context.Context, key any) (T, time.Duration, error)\n```\nGetWithTTL retrieves the object from the cache with its time to live (TTL) or loads it using the load function if not found.\n\n```go\nfunc (c *LoadableCache[T]) Set(ctx context.Context, key any, obj T) error\n```\nSet sets a value in available caches.\n\n```go\nfunc (c *LoadableCache[T]) SetWithTTL(ctx context.Context, key any, obj T, ttl time.Duration) error\n```\nSetWithTTL sets a value in the cache with a specified time to live (TTL).\n\n```go\nfunc (c *LoadableCache[T]) Sync()\n```\nSync processes items in the setChannel and sets them in the cache.\n\n```go\nfunc (c *LoadableCache[T]) Wait(ctx context.Context)\n```\nWait waits for all operations to finish.\n\n```go\ntype MockKeyGetter struct {\n\t// contains filtered or unexported fields\n}\n```\nMockKeyGetter is a mock of KeyGetter interface.\n\n```go\nfunc NewMockKeyGetter(ctrl *gomock.Controller) *MockKeyGetter\n```\nNewMockKeyGetter creates a new mock instance.\n\n```go\nfunc (m *MockKeyGetter) CacheKey() string\n```\nCacheKey mocks base method.\n\n```go\nfunc (m *MockKeyGetter) EXPECT() *MockKeyGetterMockRecorder\n```\nEXPECT returns an object that allows the caller to indicate expected use.\n\n```go\ntype MockKeyGetterMockRecorder struct {\n\t// contains filtered or unexported fields\n}\n```\nMockKeyGetterMockRecorder is the mock recorder for MockKeyGetter.\n\n```go\nfunc (mr *MockKeyGetterMockRecorder) CacheKey() *gomock.Call\n```\nCacheKey indicates an expected call of CacheKey.\n\n","path":"github.com/onexstack/onex/pkg/cache","name":"cache","synopsis":"Package cache is a complete Go cache library that brings you multiple ways of managing your caches.","isRedistributable":true}
