Documentation ¶
Overview ¶
Package cache provides partial implementations of Guava Cache, including support for LRU, Segmented LRU and TinyLFU.
Index ¶
- type Cache
- type Func
- type Hash
- type Key
- type LoaderFunc
- type LoadingCache
- type Option
- func WithExpireAfterAccess(d time.Duration) Option
- func WithExpireAfterWrite(d time.Duration) Option
- func WithMaximumSize(size int) Option
- func WithPolicy(name string) Option
- func WithRefreshAfterWrite(d time.Duration) Option
- func WithReloader(reloader Reloader) Option
- func WithRemovalListener(onRemoval Func) Option
- func WithStatsCounter(st StatsCounter) Option
- type Reloader
- type Stats
- type StatsCounter
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // GetIfPresent returns value associated with Key or (nil, false) // if there is no cached value for Key. GetIfPresent(Key) (Value, bool) // Get all keys. GetAllKeys() []interface{} // Get all values. GetAllValues() []interface{} // Get all entries. GetAll() map[interface{}]interface{} // Put associates value with Key. If a value is already associated // with Key, the old one will be replaced with Value. Put(Key, Value) // Invalidate discards cached value of the given Key. Invalidate(Key) // InvalidateAll discards all entries. InvalidateAll() // Size of cache Size() int // Stats copies cache statistics to given Stats pointer. Stats(*Stats) // 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 }
Cache is a key-value cache which entries are added and stayed in the cache until either are evicted or manually invalidated.
type Hash ¶
type Hash interface {
Sum64() uint64
}
Hash is an interface implemented by cache keys to override default hash function.
type Key ¶
type Key interface{}
Key is any value which is comparable. See http://golang.org/ref/spec#Comparison_operators for details.
type LoaderFunc ¶
LoaderFunc retrieves the value corresponding to given Key.
type LoadingCache ¶
type LoadingCache interface { Cache // Get returns value associated with Key or call underlying LoaderFunc // to load value if it is not present. Get(Key) (Value, error) // 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(Key) }
LoadingCache is a cache with values are loaded automatically and stored in the cache until either evicted or manually invalidated.
func NewLoadingCache ¶
func NewLoadingCache(loader LoaderFunc, options ...Option) LoadingCache
NewLoadingCache returns a new LoadingCache with given loader function and cache options.
type Option ¶
type Option func(c *localCache)
Option add options for default Cache.
func WithExpireAfterAccess ¶
WithExpireAfterAccess returns an option to expire a cache entry after the given duration without being accessed.
func WithExpireAfterWrite ¶ added in v1.0.1
WithExpireAfterWrite returns an option to expire a cache entry after the given duration from creation.
func WithMaximumSize ¶
WithMaximumSize returns an Option which sets maximum size for the cache. Any non-positive numbers is considered as unlimited.
func WithPolicy ¶
WithPolicy returns an option which sets cache policy associated to the given name. Supported policies are: lru, slru, tinylfu.
func WithRefreshAfterWrite ¶
WithRefreshAfterWrite returns an option to refresh a cache entry after the given duration. This option is only applicable for LoadingCache.
func WithReloader ¶ added in v1.0.1
WithReloader returns an option which sets reloader for a loading cache. By default, each asynchronous reload is run in a go routine. This option is only applicable for LoadingCache.
func WithRemovalListener ¶
WithRemovalListener returns an Option to set cache to call onRemoval for each entry evicted from the cache.
func WithStatsCounter ¶
func WithStatsCounter(st StatsCounter) Option
WithStatsCounter returns an option which overrides default cache stats counter.
type Reloader ¶ added in v1.0.1
type Reloader interface { // Reload should reload the value asynchronously. // Application must call setFunc to set new value or error. Reload(key Key, oldValue Value, setFunc func(Value, error)) // Close shuts down all running tasks. Currently, the error returned is not being used. Close() error }
Reloader specifies how cache loader is run to refresh value for the given Key. If Reloader is not set, cache values are refreshed in a new go routine.
type Stats ¶
type Stats struct { HitCount uint64 MissCount uint64 LoadSuccessCount uint64 LoadErrorCount uint64 TotalLoadTime time.Duration EvictionCount uint64 }
Stats is statistics about performance of a cache.
func (*Stats) AverageLoadPenalty ¶
AverageLoadPenalty returns the average time spent loading new values.
func (*Stats) LoadErrorRate ¶
LoadErrorRate returns the ratio of cache loading attempts which returned errors.
func (*Stats) RequestCount ¶
RequestCount returns a total of HitCount and MissCount.
type StatsCounter ¶
type StatsCounter interface { // RecordHits records cache hits. RecordHits(count uint64) // RecordMisses records cache misses. RecordMisses(count uint64) // RecordLoadSuccess records successful load of a new entry. RecordLoadSuccess(loadTime time.Duration) // RecordLoadError records failed load of a new entry. RecordLoadError(loadTime time.Duration) // RecordEviction records eviction of an entry from the cache. RecordEviction() // Snapshot writes snapshot of this counter values to the given Stats pointer. Snapshot(*Stats) }
StatsCounter accumulates statistics of a cache.