Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct { // Delete optionally specifies a function that will be called with any // value that has been deleted from the cache. It should not be changed // after the Cache has been used. Delete DeleteFunc // Delay specifies an optional delay before a value with zero ref count // is deleted from the Cache. It should not be changed after the Cache // has been used. The behaviour for negative values of Delay is // undefined. Delay time.Duration // contains filtered or unexported fields }
Cache provides a key-value cache with ref count. Values will be deleted from the cache when their ref count drops to zero.
Cache is safe for concurrent use by multiple goroutines without additional locking or coordination. The zero Cache is empty and ready for use. A Cache must not be copied after first use.
func (*Cache) Load ¶
Load returns a reference to the value stored in the cache for a key, or nil if no value is present. The second return value is true iff the value was found in the cache. Creation of a reference will increment the ref count for the stored value. A finaliser is installed (via runtime.SertFinalizer) on the returned reference that will automatically decrement the ref count when the reference is garbage collected. The user must not clear this finalizer, otherwise the ref count will never drop to zero and the value will remain in the cache.
func (*Cache) LoadOrCreate ¶
func (c *Cache) LoadOrCreate(key interface{}, f CreateFunc) (*Reference, error)
LoadOrCreate returns a reference to the existing value for the key if present. Otherwise, it stores the value created by the function f and returns a reference to that value. If f returns an error, then no value will be stored and the error will be returned. Note: If you need to know whether f was called -- thus recovering the functionality of the boolean return value in LoadOrStore -- you can do this via a closure in f. See Load for remarks on the lifetime of the reference.
func (*Cache) LoadOrStore ¶
LoadOrStore returns a reference to the existing value for the key if present. Otherwise, it stores the given value and returns a reference to that value. The second return value is true iff the value was loaded. See Load for remarks on the lifetime of the reference.
type CreateFunc ¶
type CreateFunc func() (value interface{}, err error)
CreateFunc is a function that can be called to create a missing value for the cache.
type DeleteFunc ¶
type DeleteFunc func(value interface{})
DeleteFunc is a function that is called when a value is deleted.