Documentation
¶
Overview ¶
Package cache implements a cache that supports single-flight value computation.
Single-flight value computation means that for concurrent callers of the cache, only one caller will compute the value to avoid redundant work which could be system intensive.
Index ¶
- type SingleFlight
- func (s *SingleFlight) Delete(key interface{})
- func (s *SingleFlight) Load(key interface{}) (val interface{}, err error, loaded bool)
- func (s *SingleFlight) LoadOrStore(key interface{}, valFn func() (val interface{}, err error)) (interface{}, error)
- func (s *SingleFlight) Reset()
- func (s *SingleFlight) Store(key interface{}, val interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SingleFlight ¶
type SingleFlight struct {
// contains filtered or unexported fields
}
SingleFlight is a cache that supports single-flight value computation.
func (*SingleFlight) Delete ¶
func (s *SingleFlight) Delete(key interface{})
Delete removes a key from the cache.
func (*SingleFlight) Load ¶
func (s *SingleFlight) Load(key interface{}) (val interface{}, err error, loaded bool)
Load is similar to a sync.Map.Load.
Callers must check loaded before val and err. Their value is meaninful only if loaded is true. The err is not the last returned value because it would likely lead the reader to think that err must be checked before loaded.
func (*SingleFlight) LoadOrStore ¶
func (s *SingleFlight) LoadOrStore(key interface{}, valFn func() (val interface{}, err error)) (interface{}, error)
LoadOrStore is similar to a sync.Map except that it receives a function that computes the value to store instead of the value directly. It ensures that the function is only executed once for concurrent callers of the LoadOrStore function.
func (*SingleFlight) Store ¶
func (s *SingleFlight) Store(key interface{}, val interface{})
Store forcefully updates the given cache key with val. Note that unlike LoadOrStore, Store accepts a value instead of a valFn since it is intended to be only used in cases where updates are lightweight and do not involve computing the cache value.