inmem

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is essentially a wrapper around map[T]K that support concurrent safety

func NewCache

func NewCache[T comparable, K any]() *Cache[T, K]

func (*Cache[T, K]) Evict

func (c *Cache[T, K]) Evict(key T) bool

func (*Cache[T, K]) Get

func (c *Cache[T, K]) Get(key T, _ ...Opt) (K, bool)

func (*Cache[T, K]) Inc

func (c *Cache[T, K]) Inc(_ string) bool

func (*Cache[T, K]) Put

func (c *Cache[T, K]) Put(key T, value K, _ ...Opt) bool

type Getter added in v0.4.1

type Getter[K comparable, V any] func(ctx context.Context, key K) (V, time.Duration, error)

Getter Type used for auto cache filling

type Item added in v0.4.1

type Item[T any] struct {
	Expire time.Time
	Value  T
}

func (Item[V]) IsExpired added in v0.4.4

func (i Item[V]) IsExpired() bool

type LFU

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

LFU cache (least frequently used)

The zero value is not ready for use. Refer to NewLFU for the factory method.

func NewLFU

func NewLFU[T comparable, K any](threshold int) *LFU[T, K]

NewLFU creates an in memory cache that applies an LFU policy.

When the cache must eviction keys and multiple keys have the same usage count, LFU fallbacks to an LRU policy to determine which key to evict.

func (*LFU[T, K]) Evict

func (l *LFU[T, K]) Evict(key T) bool

func (*LFU[T, K]) Get

func (l *LFU[T, K]) Get(key T, opts ...Opt) (K, bool)

func (*LFU[T, K]) Inc

func (l *LFU[T, K]) Inc(key T) bool

func (*LFU[T, K]) Put

func (l *LFU[T, K]) Put(key T, value K, opts ...Opt) bool

func (*LFU[T, K]) Values

func (l *LFU[T, K]) Values() map[T]K

type LRU

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

LRU cache (least recently used)

The zero value is not ready for use. Refer to NewLRU for the factory method.

func NewLRU

func NewLRU[T comparable, K any](threshold int) *LRU[T, K]

NewLRU creates an in memory cache that applies an LRU policy.

func (*LRU[T, K]) Evict

func (l *LRU[T, K]) Evict(key T) bool

func (*LRU[T, K]) Get

func (l *LRU[T, K]) Get(key T, opts ...Opt) (K, bool)

func (*LRU[T, K]) Inc

func (l *LRU[T, K]) Inc(key T) bool

func (*LRU[T, K]) Put

func (l *LRU[T, K]) Put(key T, value K, opts ...Opt) bool

func (*LRU[T, K]) Values

func (l *LRU[T, K]) Values() map[T]K

type Opt

type Opt func(*opts)

func SkipInc

func SkipInc(skip bool) Opt

SkipInc specify whether the "count" used to apply the LFU/LRU policies must be incremented.

Defaults to false

This is useful when multiple get/put operations must count as a single access.

type Storage added in v0.4.1

type Storage[K comparable, V any] interface {
	Put(key K, value Item[V], opt ...Opt) bool
	Evict(key K) bool
	Get(key K, opt ...Opt) (Item[V], bool)
}

type Store added in v0.4.1

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

func NewStore added in v0.4.1

func NewStore[K comparable, V any](opts ...StoreOpt[K, V]) *Store[K, V]

func (Store[K, V]) Evict added in v0.4.1

func (s Store[K, V]) Evict(key K)

func (Store[K, V]) EvictAll added in v0.4.2

func (s Store[K, V]) EvictAll(keys []K)

func (Store[K, V]) Get added in v0.4.1

func (s Store[K, V]) Get(ctx context.Context, key K) (Item[V], bool, error)

func (Store[K, V]) NewItem added in v0.4.1

func (s Store[K, V]) NewItem(value V, ttl time.Duration) Item[V]

func (Store[K, V]) Put added in v0.4.1

func (s Store[K, V]) Put(key K, item Item[V])

func (Store[K, V]) Update added in v0.4.1

func (s Store[K, V]) Update(
	ctx context.Context,
	key K,
	args []byte,
	fn func(context.Context, V, []byte) (V, time.Duration, error),
) (Item[V], error)

type StoreOpt added in v0.4.1

type StoreOpt[K comparable, V any] func(*Store[K, V])

func WithGetter added in v0.4.1

func WithGetter[K comparable, V any](getter Getter[K, V]) StoreOpt[K, V]

func WithStorage added in v0.4.1

func WithStorage[K comparable, V any](storage Storage[K, V]) StoreOpt[K, V]

Jump to

Keyboard shortcuts

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