cache

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 6 Imported by: 4

README

Cache abstraction with TTL and stampede control

Go Reference Codebeat Score

Documentation

Overview

Package cache provides a generic cache interface

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSink tells the [Sink] isn't in a usable state.
	ErrInvalidSink = core.QuietWrap(core.ErrInvalid, "%s", "invalid sink")

	// ErrNoData indicates to data was provided.
	ErrNoData = core.Wrap(core.ErrInvalid, "no data")
)

Functions

func DecodeGob added in v0.3.0

func DecodeGob[T any](b []byte, out *T) error

DecodeGob attempts to transform a byte slice into a Go object using Gob encoding.

func DefaultClone added in v0.3.0

func DefaultClone[T any](src *T) (*T, bool)

DefaultClone attempts to make a safe copy of the given object using interfaces.

- Clone() *T - Copy() *T - Clone() T - Copy() T

func EncodeGob added in v0.3.0

func EncodeGob[T any](p *T) ([]byte, error)

EncodeGob attempts to transform a Go object to a bytes slice using Gob encoding.

Types

type ByteSink added in v0.3.0

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

ByteSink implements the simplest form of Sink

func (*ByteSink) Bytes added in v0.3.0

func (s *ByteSink) Bytes() []byte

Bytes returns the stored bytes

func (*ByteSink) Expire added in v0.3.0

func (s *ByteSink) Expire() time.Time

Expire indicates when the stored bytes are due to expire

func (*ByteSink) Len added in v0.3.0

func (s *ByteSink) Len() int

Len returns the length of the store bytes

func (*ByteSink) Reset added in v0.3.0

func (s *ByteSink) Reset()

Reset blanks the stored data

func (*ByteSink) SetBytes added in v0.3.0

func (s *ByteSink) SetBytes(b []byte, e time.Time) error

SetBytes stores a copy of the given bytes and expiration date.

func (*ByteSink) UnsafeSetBytes added in v0.3.0

func (s *ByteSink) UnsafeSetBytes(b []byte, e time.Time) error

UnsafeSetBytes is like SetBytes but it uses the given byte slice directly instead of making a copy.

type Cache

type Cache[K comparable] interface {
	// Stats returns stats about the Cache namespace
	Stats(Type) Stats
	// Name returns the name of the cache
	Name() string

	// Set adds an entry to the Cache
	Set(ctx context.Context, key K, value []byte, expire time.Time, cacheType Type) error
	// Get reads an entry into a Sink
	Get(ctx context.Context, key K, dest Sink) error
	// Remove removes an entry from the Cache
	Remove(ctx context.Context, key K)
}

A Cache namespace

type Getter

type Getter[K comparable] interface {
	// Get returns the value identified by key, populating dest.
	//
	// The returned data must be unversioned. That is, key must
	// uniquely describe the loaded data, without an implicit
	// current time, and without relying on cache expiration
	// mechanisms.
	Get(ctx context.Context, key K, dest Sink) error
}

A Getter loads data for a key.

type GetterFunc

type GetterFunc[K comparable] func(ctx context.Context, key K, dest Sink) error

A GetterFunc implements Getter with a function.

func (GetterFunc[K]) Get

func (f GetterFunc[K]) Get(ctx context.Context, key K, dest Sink) error

Get allows a GetterFunc to implement the Getter interface

type GobSink

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

GobSink is a Sink using generics for type safety and Gob for encoding

func (*GobSink[T]) Reset

func (sink *GobSink[T]) Reset()

Reset clears everything but the type pointer assigned during creation

func (*GobSink[T]) SetBytes

func (sink *GobSink[T]) SetBytes(b []byte, e time.Time) error

SetBytes sets the object of the GobSink and its expiration time from a Gob encoded byte array

func (*GobSink[T]) SetValue

func (sink *GobSink[T]) SetValue(v *T, e time.Time) error

SetValue sets the object of the GobSink and its expiration time

func (*GobSink[T]) Value

func (sink *GobSink[T]) Value() (*T, bool)

Value gives a copy of the stored object

type Setter

type Setter[K comparable] interface {
	Set(ctx context.Context, key K, value []byte,
		expire time.Time, cacheType Type) error
}

A Setter stores data for a key

type SetterFunc

type SetterFunc[K comparable] func(ctx context.Context, key K, value []byte,
	expire time.Time, cacheType Type) error

A SetterFunc implements Setter with a function

func (SetterFunc[K]) Set

func (f SetterFunc[K]) Set(ctx context.Context, key K, value []byte,
	expire time.Time, cacheType Type) error

Set allows a SetterFunc to implement the Setter interface

type Sink

type Sink interface {
	// SetBytes sets the value to the contents of v.
	// The caller retains ownership of v.
	SetBytes(v []byte, e time.Time) error

	// Bytes returns the value encoded as a slice
	// of bytes
	Bytes() []byte

	// Len tells the length of the internally encoded
	// representation of the value
	Len() int

	// Expire returns the time whe this entry will
	// be evicted from the Cache
	Expire() time.Time

	// Reset empties the content of the Sink
	Reset()
}

Sink receives data from a Get call

type SinkFn added in v0.3.1

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

SinkFn is a TSink implementation made using a SinkType factory.

func (*SinkFn[_]) IsZero added in v0.3.1

func (sink *SinkFn[_]) IsZero() bool

IsZero tells if the Sink is valid but empty.

func (*SinkFn[T]) SetBytes added in v0.3.1

func (sink *SinkFn[T]) SetBytes(b []byte, e time.Time) error

SetBytes sets the object of the Sink and its expiration time from an encoded byte array.

func (*SinkFn[T]) SetValue added in v0.3.1

func (sink *SinkFn[T]) SetValue(v *T, e time.Time) error

SetValue sets the object of the Sink and its expiration time.

func (*SinkFn[T]) Valid added in v0.3.1

func (sink *SinkFn[T]) Valid() bool

Valid tells if the Sink can be used.

func (*SinkFn[T]) Value added in v0.3.1

func (sink *SinkFn[T]) Value() (*T, bool)

Value returns a copy of the stored object

type SinkType added in v0.3.1

type SinkType[T any] struct {
	Decode func([]byte, *T) error
	Encode func(*T) ([]byte, error)
	Clone  func(*T) (*T, bool)
}

SinkType describes a Sink factory.

func (*SinkType[T]) New added in v0.3.1

func (typ *SinkType[T]) New() (*SinkFn[T], error)

New creates a new Sink using the SinkType factory.

func (*SinkType[T]) SetDefaults added in v0.3.1

func (typ *SinkType[T]) SetDefaults() error

SetDefaults fills the gaps and identifies errors.

type Stats

type Stats struct {
	Bytes     int64
	Items     int64
	Gets      int64
	Hits      int64
	Evictions int64
}

Stats provides a snapshot on the state of a Cache namespace

type Store

type Store[K comparable] interface {
	// GetCache returns the named cache previously created with NewCache,
	// or nil if there's no such namespace.
	GetCache(name string) Cache[K]
	// NewCache creates a new Cache namespace
	NewCache(name string, cacheBytes int64, getter Getter[K]) Cache[K]
	// DeregisterCache removes a Cache namespace
	DeregisterCache(name string)

	// SetLogger binds the Store and its Cache namespaces to a logger
	SetLogger(log slog.Logger)
}

A Store allows us to create or access Cache namespaces

type TSink added in v0.3.0

type TSink[T any] interface {
	Sink

	// SetValue sets the value to the object v.
	// The caller retains ownership of v.
	SetValue(v *T, e time.Time) error

	// Value returns a copy of the object previously
	// stored in the Sink.
	Value() (*T, bool)
}

TSink extends Sink with type-specific SetValue/Value methods.

type Type

type Type int

Type represents a type of cache

const (
	// MainCache represents the cache for items we owner by the application.
	MainCache Type = iota + 1
	// HotCache represents the cache for frequently accessed items, even if
	// not owned by the application.
	HotCache
)

Directories

Path Synopsis
x
groupcache module
memcache module
protosink module
simplelru module

Jump to

Keyboard shortcuts

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