Documentation ¶
Overview ¶
Package cache implements a cache which collaborates on concurrent calculations of the same hash key to reduce time spent calculating expensive values.
The Cache.Get() method ensures that at most one goroutine is calculating a specific key value at any time. It also ensures that frequently accessed values are recalculated before their expiration time. It does this by creating a cacheItem which contains a wait condition on the value completion only if one does not already exist.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct { MaxSize int MaxStorage uint64 Refresh bool ExtendOnUse bool GetTimeout time.Duration Recover bool // contains filtered or unexported fields }
Cache implements a cache
func (*Cache) Get ¶
func (c *Cache) Get(key interface{}, ttl time.Duration, generate func(interface{}) (interface{}, error)) func() (interface{}, error)
Get begins the process of fetching a specific cache item.
When Get is called a new goroutine is spawned to run the provided generate method if no result is available in the cache. A function is return which, when called, will wait for the goroutine to finish execution and return the value/error returned. In the case that the item has expired before the retrieve method is called the goroutine may be called again.
If the cache entry is half way through its TTL a new goroutine will be spawned at the time of retrieval, though the current cached values will be returned immediately.
Expiration/Refresh conditions are evaluated immediately upon calling Get(), the retrieval function returns the cache query as it was evaluated during the Get operation.
func (*Cache) Purge ¶
func (c *Cache) Purge()
Purge finds and removes all expired cache entires from the cache, allowing the data to be freed by the garbage collector.
func (*Cache) PurgeCount ¶
PurgeCount finds and removes all expired cache entires from the cache, checking at moust count items.