cache

package
v6.0.0-...-4ba06ae Latest Latest
Warning

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

Go to latest
Published: May 9, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NoTTL = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Expired

type Expired struct {
	Key   interface{}
	When  time.Time
	Value interface{}
}

Expired is a special type of error indicating that a key is no longer valid (value is still attached in the error)

func (*Expired) Error

func (e *Expired) Error() string

type MLCLayer

type MLCLayer[K comparable, V comparable] interface {
	Get(ctx context.Context, key K) (V, error)
	Set(ctx context.Context, key K, value V) error
}

MLCLayer is the interface that should be implemented for all caching structs to be used with this piece of code.

type Miss

type Miss struct {
	Where string
	Key   interface{}
}

Miss is a special type of error indicating that a key was not found

func (*Miss) Error

func (c *Miss) Error() string

type MultiLevelCache

type MultiLevelCache[K comparable, V comparable] interface {
	Get(ctx context.Context, key K) (V, error)
}

MultiLevelCache bundles a list of ordered cache layers (upper -> lower)

type MultiLevelCacheImpl

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

MultiLevelCacheImpl implements the MultiLevelCache interface

func NewMultiLevel

func NewMultiLevel[K comparable, V comparable](layers []MLCLayer[K, V], logger logging.LoggerInterface) (*MultiLevelCacheImpl[K, V], error)

NewMultiLevel creates and returns a new MultiLevelCache instance

func (*MultiLevelCacheImpl[K, V]) Get

func (c *MultiLevelCacheImpl[K, V]) Get(ctx context.Context, key K) (V, error)

Get returns the value of the requested key (if found) and populates upper levels with it

type SimpleLRU

type SimpleLRU[K comparable, V any] interface {
	Get(key K) (V, error)
	Set(key K, value V) error
	FlushKey(key K)
}

SimpleLRU is an in-memory TTL & LRU cache

type SimpleLRUImpl

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

SimpleLRUImpl implements the Simple interface

func NewSimpleLRU

func NewSimpleLRU[K comparable, V any](maxSize int, ttl time.Duration) (*SimpleLRUImpl[K, V], error)

NewSimple returns a new Simple instance of the specified size and TTL

func (*SimpleLRUImpl[K, V]) FlushKey

func (c *SimpleLRUImpl[K, V]) FlushKey(key K)

FlushKey removes an entry form the cache if it exists. Does nothing otherwise

func (*SimpleLRUImpl[K, V]) Get

func (c *SimpleLRUImpl[K, V]) Get(key K) (V, error)

Get retrieves an item if exist, nil + an error otherwise

func (*SimpleLRUImpl[K, V]) Set

func (c *SimpleLRUImpl[K, V]) Set(key K, value V) error

Set adds a new item. Since the cache being full results in removing the LRU element, this method never fails.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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