pool

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Pool container for Go

Go PkgGoDev NewReleases

Installation

Run go get resenje.org/pool from command line.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expiring

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

Expiring is a pool of elements associated with string keys that have an expiration time after no one is using them.

The elements will be evicted from the pool only on the next Get function call or Prune function call. There is no assurances that the eviction will happen at the expiration time, just that it will not happen before.

func NewExpiring

func NewExpiring[K comparable, V any](
	constructor func(key K) (V, error),
	destructor func(V) error,
) *Expiring[K, V]

NewExpiring creates a new Expiring pool with constructor and destructor for functions for pool elements.

func (*Expiring[K, V]) Clear

func (p *Expiring[K, V]) Clear() error

Clear removes all elements in the pool regardless if they are expired or not.

func (*Expiring[K, V]) Get

func (p *Expiring[K, V]) Get(key K) (t V, err error)

Get retrieves a value from the pool referenced by the key. If the value is not in the pool, a new instance will be created using the pool's constructor function.

func (*Expiring[K, V]) Prune

func (p *Expiring[K, V]) Prune() error

Prune removes all expired elements.

func (*Expiring[K, V]) Release

func (p *Expiring[K, V]) Release(key K, ttl time.Duration)

Release marks the key in pool as no longer used by the previous Get caller and sets it eventual expiration time.

Jump to

Keyboard shortcuts

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