ttlcache

package module
v0.0.0-...-85cedd7 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 8 Imported by: 2

README

TTL cache

Eviction cache for arbitrary data.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOK          error = nil
	ErrNoConfig          = errors.New("no config provided")
	ErrBadCache          = errors.New("cache uninitialized, use New()")
	ErrCacheClosed       = errors.New("cache closed")
	ErrNoHasher          = errors.New("no hasher provided")
	ErrNoBuckets         = errors.New("buckets must be greater than zero")
	ErrShortTTL          = errors.New("TTL must be greater than one second")
	ErrNotFound          = errors.New("entry not found")
	ErrExpire            = errors.New("entry expired")
)

Functions

This section is empty.

Types

type Cache

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

func New

func New[T any](conf *Config[T]) (*Cache[T], error)

func (*Cache[T]) Close

func (c *Cache[T]) Close() error

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string) error

func (*Cache[T]) Extract

func (c *Cache[T]) Extract(key string) (T, error)

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, error)

func (*Cache[T]) Reset

func (c *Cache[T]) Reset() error

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T) error

type Clock

type Clock interface {
	// Start the clock.
	Start()
	// Stop the clock.
	Stop()
	// Active checks if clock is started.
	Active() bool
	// Now returns current time considering jumps.
	Now() time.Time
	// Jump performs time travel to delta (maybe negative). Now and scheduled jobs considers jumps.
	Jump(delta time.Duration)
	// Schedule registers fn to call at every d.
	Schedule(d time.Duration, fn func())
}

Clock describes timer helper with testing features (like Jump).

type Config

type Config[T any] struct {
	Size          uint64
	Buckets       uint
	Hasher        Hasher
	TTLInterval   time.Duration
	EvictInterval time.Duration
	EvictWorkers  uint

	DumpWriter       DumpWriter
	DumpEncoder      Encoder[T]
	DumpInterval     time.Duration
	DumpWriteWorkers uint

	DumpReader      DumpReader
	DumpDecoder     Decoder[T]
	DumpReadBuffer  uint
	DumpReadWorkers uint
	DumpReadAsync   bool

	MetricsWriter MetricsWriter
	Clock         Clock
	Logger        Logger
}

func (*Config[T]) Copy

func (c *Config[T]) Copy() *Config[T]

type Decoder

type Decoder[T any] interface {
	Decode(*T, []byte) error
}

type DummyMetrics

type DummyMetrics struct{}

func (DummyMetrics) Del

func (DummyMetrics) Del(_ string)

func (DummyMetrics) Dump

func (DummyMetrics) Dump(_ string)

func (DummyMetrics) Evict

func (DummyMetrics) Evict(_ string)

func (DummyMetrics) Expire

func (DummyMetrics) Expire(_ string)

func (DummyMetrics) Hit

func (DummyMetrics) Hit(_ string, _ time.Duration)

func (DummyMetrics) Load

func (DummyMetrics) Load(_ string)

func (DummyMetrics) Miss

func (DummyMetrics) Miss(_ string)

func (DummyMetrics) Overflow

func (DummyMetrics) Overflow(_ string)

func (DummyMetrics) Set

func (DummyMetrics) Set(_ string, _ time.Duration)

type DumpReader

type DumpReader interface {
	Read() (Entry, error)
}

type DumpWriter

type DumpWriter interface {
	Write(entry Entry) (int, error)
	Flush() error
}

type Encoder

type Encoder[T any] interface {
	Encode([]byte, T) ([]byte, int, error)
}

type Entry

type Entry struct {
	Key    uint64
	Body   []byte
	Expire uint32
}

type Hasher

type Hasher interface {
	Sum64(string) uint64
}

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Print(v ...interface{})
	Println(v ...interface{})
}

type MarshallerTo

type MarshallerTo interface {
	Size() int
	MarshalTo([]byte) (int, error)
}

type MetricsWriter

type MetricsWriter interface {
	Set(bucket string, dur time.Duration)
	Hit(bucket string, dur time.Duration)
	Del(bucket string)
	Miss(bucket string)
	Expire(bucket string)
	Overflow(bucket string)
	Evict(bucket string)
	Dump(bucket string)
	Load(bucket string)
}

type NativeClock

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

NativeClock is a primitive clock based on time package.

Jump doesn't work in that implementation.

func (*NativeClock) Active

func (n *NativeClock) Active() bool

func (*NativeClock) Jump

func (n *NativeClock) Jump(_ time.Duration)

func (*NativeClock) Now

func (n *NativeClock) Now() time.Time

func (*NativeClock) Schedule

func (n *NativeClock) Schedule(d time.Duration, fn func())

func (*NativeClock) Start

func (n *NativeClock) Start()

func (*NativeClock) Stop

func (n *NativeClock) Stop()

type Unmarshaller

type Unmarshaller interface {
	Unmarshal([]byte) error
}

Directories

Path Synopsis
metrics
prometheus module

Jump to

Keyboard shortcuts

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