sync

package
v1.2.58 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UnlimitedResident = -1
	UnlimitedCapacity = 0
)

Variables

This section is empty.

Functions

func WithLock added in v1.2.54

func WithLock(lk sync.Locker, fn func())

WithLock runs while holding lk.

Types

type EvictCallback

type EvictCallback[K comparable, V any] func(key K, value V)

EvictCallback is used to get a callback when a cache entry is evicted

type FixedPool added in v1.2.54

type FixedPool[E any] struct {

	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	// It may not be changed concurrently with calls to Get.
	New func() E

	// MinResidentSize controls the minimum number of keep-alive items. items will be prealloced.
	MinResidentSize int
	// MaxResidentSize controls the maximum number of keep-alive items. Negative means no limit.
	MaxResidentSize int
	// MaxCapacity controls the maximum number of allocated items. Zero means no limit.
	MaxCapacity int
	// contains filtered or unexported fields
}

FixedPool is a set of resident and temporary items that may be individually saved and retrieved.

Any item stored in the Pool may be removed automatically at any time without notification. If the Pool holds the only reference when this happens, the item might be deallocated.

A Pool is safe for use by multiple goroutines simultaneously.

func NewCachedPool added in v1.2.54

func NewCachedPool[E any](f func() E) *FixedPool[E]

NewCachedPool Creates a pool that creates new items as needed, but will reuse previously constructed items when they are available. the pool will reuse previously constructed items and items will never be dropped.

func NewFixedPool added in v1.2.54

func NewFixedPool[E any](f func() E, size int) *FixedPool[E]

NewFixedPool returns an initialized fixed pool. resident controls the maximum number of keep-alive items. Negative means no limit. cap controls the maximum number of allocated items. Zero means no limit.

func NewTempPool added in v1.2.54

func NewTempPool[E any](f func() E) *FixedPool[E]

NewTempPool Creates a pool that creates new items as needed, but will be dropped at second GC if only referenced by the pool self. the pool will reuse previously constructed items when they are available and not dropped.

func (*FixedPool[E]) Cap added in v1.2.54

func (p *FixedPool[E]) Cap() int

Cap returns the capacity of pool, that is object len allocated The complexity is O(1).

func (*FixedPool[E]) Emplace added in v1.2.54

func (p *FixedPool[E]) Emplace(x E)

Emplace adds x to the pool. NOTE: Emplace may break through the len and cap boundaries, as x be allocated already.

func (*FixedPool[E]) Finalize added in v1.2.54

func (p *FixedPool[E]) Finalize()

func (*FixedPool[E]) Get added in v1.2.54

func (p *FixedPool[E]) Get() *FixedPoolElement[E]

Get selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to Put and the values returned by Get.

If Get would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.

Get uses context.Background internally; to specify the context, use GetContext.

func (*FixedPool[E]) GetContext added in v1.2.54

func (p *FixedPool[E]) GetContext(ctx context.Context) (*FixedPoolElement[E], error)

GetContext selects an arbitrary item from the Pool, removes it from the Pool, and returns it to the caller. Get may choose to ignore the pool and treat it as empty. Callers should not assume any relation between values passed to Put and the values returned by Get.

If GetContext would otherwise return nil and p.New is non-nil, Get returns the result of calling p.New.

func (*FixedPool[E]) Init added in v1.2.54

func (p *FixedPool[E]) Init() *FixedPool[E]

Init initializes fixed pool l.

func (*FixedPool[E]) Len added in v1.2.54

func (p *FixedPool[E]) Len() int

Len returns the len of pool, that is object len idle, allocated and still in cache The complexity is O(1).

func (*FixedPool[E]) Put added in v1.2.54

func (p *FixedPool[E]) Put(x *FixedPoolElement[E]) (stored bool)

Put adds x to the pool.

func (*FixedPool[E]) TryGet added in v1.2.54

func (p *FixedPool[E]) TryGet() *FixedPoolElement[E]

func (*FixedPool[E]) TryPut added in v1.2.54

func (p *FixedPool[E]) TryPut(x *FixedPoolElement[E]) (stored bool)

type FixedPoolElement added in v1.2.54

type FixedPoolElement[E any] struct {
	// The value stored with this element.
	Value E
	// contains filtered or unexported fields
}

func (*FixedPoolElement[E]) Finalize added in v1.2.54

func (e *FixedPoolElement[E]) Finalize()

func (*FixedPoolElement[E]) Get added in v1.2.54

func (e *FixedPoolElement[E]) Get() E

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU implements a non-thread safe fixed size LRU cache

func NewLRU added in v1.2.14

func NewLRU[K comparable, V any](size int) *LRU[K, V]

NewLRU constructs an LRU of the given size

func (*LRU[K, V]) Add

func (c *LRU[K, V]) Add(key K, value V) (evicted bool)

Add adds a value to the cache. Returns true if an eviction occurred.

func (*LRU[K, V]) Cap added in v1.2.15

func (c *LRU[K, V]) Cap() int

Cap returns the capacity of the cache.

func (*LRU[K, V]) Contains

func (c *LRU[K, V]) Contains(key K) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*LRU[K, V]) Get

func (c *LRU[K, V]) Get(key K) (value V, ok bool)

Get looks up a key's value from the cache.

func (*LRU[K, V]) GetOldest

func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool)

GetOldest returns the oldest entry

func (*LRU[K, V]) Init

func (c *LRU[K, V]) Init() *LRU[K, V]

Init initializes or clears LRU l.

func (*LRU[K, V]) Keys

func (c *LRU[K, V]) Keys() []K

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*LRU[K, V]) Len

func (c *LRU[K, V]) Len() int

Len returns the number of items in the cache.

func (*LRU[K, V]) Peek

func (c *LRU[K, V]) Peek(key K) (value V, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*LRU[K, V]) Purge

func (c *LRU[K, V]) Purge()

Purge is used to completely clear the cache.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU[K, V]) RemoveOldest

func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU[K, V]) Resize

func (c *LRU[K, V]) Resize(size int) (evicted int)

Resize changes the cache size.

func (*LRU[K, V]) SetEvictCallback

func (c *LRU[K, V]) SetEvictCallback(onEvict EvictCallback[K, V]) *LRU[K, V]

SetEvictCallback sets a callback when a cache entry is evicted

func (*LRU[K, V]) SetEvictCallbackFunc added in v1.2.54

func (c *LRU[K, V]) SetEvictCallbackFunc(onEvict func(key K, value V)) *LRU[K, V]

SetEvictCallbackFunc sets a callback when a cache entry is evicted

Jump to

Keyboard shortcuts

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