Documentation ¶
Index ¶
- Variables
- type BatchFetcher
- type Cache
- func (c *Cache) Close()
- func (c *Cache) Contains(key string) bool
- func (c *Cache) Delete(key string)
- func (c *Cache) DeleteFunc(match func(key string) bool)
- func (c *Cache) Get(key string) (any, error)
- func (c *Cache) GetOrDefault(key string, defaultVal any) any
- func (c *Cache) SetDefault(key string, value any) (exists bool)
- func (c *Cache) Update(key string, value any)
- type Fetcher
- type FuncFetcher
- type Options
Constants ¶
This section is empty.
Variables ¶
var ErrFetchTimeout = errors.New("fetch timeout")
ErrFetchTimeout indicates a timeout error when refresh a cached value if Options.FetchTimeout is specified.
Functions ¶
This section is empty.
Types ¶
type BatchFetcher ¶ added in v2.14.0
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is an asynchronous cache which prevents duplicate functions calls that is massive or maybe expensive, or some data which rarely change, and we want to get it quickly.
Zero value of Cache is not ready to use. Use the function NewCache to make a new Cache instance. A Cache value shall not be copied after initialized.
func (*Cache) Close ¶
func (c *Cache) Close()
Close closes the Cache. It signals the background goroutines to shut down.
It should be called when the Cache is no longer needed, or may lead resource leaks.
func (*Cache) Contains ¶
Contains tells whether the cache contains the specified key. It returns false if key is never accessed from the cache, true means that a value or an error for key exists in the cache.
func (*Cache) DeleteFunc ¶
DeleteFunc iterates the cache and deletes entries that the key matches the given function.
func (*Cache) Get ¶
Get tries to fetch a value corresponding to the given key from the cache. If it's not cached, a calling to Fetcher.Fetch will be fired and the result will be cached.
If error occurs during the first fetching, the error will be cached until the subsequent fetching requests triggered by refreshing succeed. The cached error will be returned, it does not trigger a calling to Options.Fetcher again.
If a default value is set by SetDefault, the default value will be used, it does not trigger a calling to Options.Fetcher.
func (*Cache) GetOrDefault ¶
GetOrDefault tries to fetch a value corresponding to the given key from the cache. If it's not cached, a calling to Options.Fetcher will be fired and the result will be cached.
If error occurs during the first fetching, defaultVal will be set into the cache and returned, the default value will also be used for further calling of Get and GetOrDefault.
func (*Cache) SetDefault ¶
SetDefault sets the default value of a given key if it is new to the cache. The param val should not be nil, else it panics. The returned bool value indicates whether the key already exists in the cache, if it already exists, this is a no-op.
It's useful to warm up the cache.
type FuncFetcher ¶ added in v2.14.0
FuncFetcher is a function that implements the interface Fetcher.
type Options ¶
type Options struct { // Fetcher fetches data from upstream system for a given key. // Result value or error will be cached till next refresh execution. // // The provided Fetcher implementation must return consistently // typed values, else it panics when storing a value of different // type into the underlying sync/atomic.Value. // // The returned value from this function should not be changed after // retrieved from the Cache, else data race happens since there may be // many goroutines access the same value concurrently. Fetcher Fetcher // FetchTimeout is used to timeout the fetch request if given, // default is zero (no timeout). // // NOTE: properly configured timeout will prevent task which take very long // time that don't fail fast, which may further block many requests, and // consume huge amount of resources, cause system overload or out of memory. FetchTimeout time.Duration // RefreshInterval specifies the interval to refresh the cache values, // default is zero which means don't refresh the cached values. // // If there is valid cache value and the subsequent fetch requests // failed, the existing cache value will be kept untouched. RefreshInterval time.Duration // ExpireInterval optionally enables purging unused cached values, // default is zero which means no expiration. // // Note this is mainly used to purge unused data to prevent the cache // growing endlessly, the timing is inaccurate. Also note that // it may delete unused default values set by SetDefault. // // Cached values are deleted using a mark-then-delete strategy. // In each tick of expire interval, an active value will be marked as inactive, // if it's not accessed within the next expire interval, it will be // deleted from the Cache by the next expire execution. // // Each access of the cache value will touch it and mark it as active, which // prevents it being deleted from the cache. ExpireInterval time.Duration // ErrorCallback is an optional callback which will be called when // an error is returned by Fetcher during refresh. ErrorCallback func(err error, keys []string) // ChangeCallback is an optional callback which will be called when // new value is returned by Fetcher during refresh. ChangeCallback func(key string, oldData, newData any) // DeleteCallback is an optional callback which will be called when // a value is deleted from the cache. DeleteCallback func(key string, data any) }
Options configures the behavior of Cache.