Documentation
¶
Overview ¶
Package cache provides implementations of loading cache, including support for LRU and LFU.
Index ¶
- Variables
- type Cache
- func (tc *Cache[K, V]) Close() error
- func (tc *Cache[K, V]) Get(ctx context.Context, key K) (V, error)
- func (tc *Cache[K, V]) GetIfPresent(ctx context.Context, key K) (V, bool)
- func (tc *Cache[K, V]) Invalidate(ctx context.Context, key K)
- func (tc *Cache[K, V]) InvalidateAll(ctx context.Context)
- func (tc *Cache[K, V]) Put(ctx context.Context, key K, val V)
- func (tc *Cache[K, V]) Refresh(ctx context.Context, key K) error
- func (tc *Cache[K, V]) SetLoader(lf LoaderFunc[K, V])
- type CacheParams
- type CacheType
- type Func
- type Key
- type LoaderFunc
- type LoadingCache
- type SomeCache
- type Value
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrNotFound = errors.New("key not found in cache")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
func (*Cache[K, V]) GetIfPresent ¶
func (*Cache[K, V]) Invalidate ¶
func (*Cache[K, V]) InvalidateAll ¶
func (*Cache[K, V]) SetLoader ¶
func (tc *Cache[K, V]) SetLoader(lf LoaderFunc[K, V])
type CacheParams ¶
type LoaderFunc ¶
LoaderFunc retrieves the value corresponding to given Key. You must ensure the context is not canceled before use it in database queries!
type LoadingCache ¶
type LoadingCache[K Key, V Value] interface { SomeCache[K, V] // Get returns value associated with Key or call underlying LoaderFunc // to load value if it is not present. Get(context.Context, K) (V, error) // SetLoader changes the underlying loader func SetLoader(LoaderFunc[K, V]) // Refresh loads new value for Key. If the Key already existed, the previous value // will continue to be returned by Get while the new value is loading. // If Key does not exist, this function will block until the value is loaded. Refresh(context.Context, K) error }
LoadingCache is a cache with values are loaded automatically and stored in the cache until either evicted or manually invalidated.
type SomeCache ¶
type SomeCache[K Key, V Value] interface { // GetIfPresent returns value associated with Key or (nil, false) // if there is no cached value for Key. GetIfPresent(context.Context, K) (V, bool) // Put associates value with Key. If a value is already associated // with Key, the old one will be replaced with Value. Put(context.Context, K, V) // Invalidate discards cached value of the given Key. Invalidate(context.Context, K) // InvalidateAll discards all entries. InvalidateAll(context.Context) // Close implements io.Closer for cleaning up all resources. // Users must ensure the cache is not being used before closing or // after closed. Close() error }
SomeCache is a key-value cache which entries are added and stayed in the cache until either are evicted or manually invalidated.
Click to show internal directories.
Click to hide internal directories.