refcount

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 4 Imported by: 3

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

func (c *Cache) Load(key interface{}) (*Reference, bool)

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

func (c *Cache) LoadOrStore(key interface{}, value interface{}) (*Reference, bool)

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.

func (*Cache) Range

func (c *Cache) Range(f func(key interface{}, r *Reference) bool)

Range calls f sequentially for each key and value present in the cache. If f returns false, range stops the iteration. 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.

type Reference

type Reference struct {
	// contains filtered or unexported fields
}

Reference is a reference to a value.

func (*Reference) RefCount

func (r *Reference) RefCount() int

RefCount returns the ref count for the referenced value.

func (*Reference) Value

func (r *Reference) Value() interface{}

Value returns the value referenced by r. The caller must take care to keep the reference r in scope whilst the value is in use. This can be achieved by runtime.KeepAlive(r).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL