cache

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 7 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 (
	// ErrInvalid is returned by the Sink when the set
	// value is of the wrong type
	ErrInvalid = errors.New("invalid type")
)

Functions

This section is empty.

Types

type Cache

type Cache 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 string, value []byte, expire time.Time, cacheType Type) error
	// Get reads an entry into a Sink
	Get(ctx context.Context, key string, dest Sink) error
	// Remove removes an entry from the Cache
	Remove(ctx context.Context, key string)
}

A Cache namespace

type Getter

type Getter 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 string, dest Sink) error
}

A Getter loads data for a key.

type GetterFunc

type GetterFunc func(ctx context.Context, key string, dest Sink) error

A GetterFunc implements Getter with a function.

func (GetterFunc) Get

func (f GetterFunc) Get(ctx context.Context, key string, dest Sink) error

Get allows a GetterFunc to implement the Getter interface

type GobSink

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

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

func NewGobSink

func NewGobSink[T any](out *T) *GobSink[T]

NewGobSink creates a new Sink using Gob as encoding and stores the object in the provided pointer

func (*GobSink[T]) Bytes

func (sink *GobSink[T]) Bytes() []byte

Bytes returns the Gob encoded representation of the object in the Sink

func (*GobSink[T]) Expire

func (sink *GobSink[T]) Expire() time.Time

Expire tells when this object will be evicted from the Cache

func (*GobSink[T]) Len

func (sink *GobSink[T]) Len() int

Len returns the length of the Gob encoded representation of the object in the Sink. 0 if empty.

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(v []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]) SetString

func (sink *GobSink[T]) SetString(string, time.Time) error

SetString isn't supported by GobSink, but its needed to satisfy the Sink interface

func (*GobSink[T]) SetValue

func (sink *GobSink[T]) SetValue(v any, 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

Value gives a copy of the stored object

type Setter

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

A Setter stores data for a key

type SetterFunc

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

A SetterFunc implements Setter with a function

func (SetterFunc) Set

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

Set allows a SetterFunc to implement the Setter interface

type Sink

type Sink interface {
	// SetString sets the value to s.
	SetString(s string, e time.Time) error

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

	// SetValue sets the value to the object v.
	// The caller retains ownership of v.
	SetValue(v any, 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 Stats

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

Stats provides an snapshot on the state of a Cache namespace

type Store

type Store interface {
	// GetCache returns the named cache previously created with
	// NewCache, or nil if there's no such namespace.
	GetCache(name string) Cache
	// NewCache creates a new Cache namespace
	NewCache(name string, cacheBytes int64, getter Getter) Cache
	// 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 Type

type Type int

Type represetns a type of cache

const (
	// MainCache is the cache for items we own
	MainCache Type = iota + 1
	// HotCache is the cache for items that seem popular
	// even if we don't necessarily own
	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