Documentation
¶
Overview ¶
Package cache provides a generic cache interface
Index ¶
- Variables
- func DecodeGob[T any](b []byte, out *T) error
- func DefaultClone[T any](src *T) (*T, bool)
- func EncodeGob[T any](p *T) ([]byte, error)
- type ByteSink
- type Cache
- type Getter
- type GetterFunc
- type GobSink
- type Setter
- type SetterFunc
- type Sink
- type SinkFn
- type SinkType
- type Stats
- type Store
- type TSink
- type Type
Constants ¶
This section is empty.
Variables ¶
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
DecodeGob attempts to transform a byte slice into a Go object using Gob encoding.
func DefaultClone ¶ added in v0.3.0
DefaultClone attempts to make a safe copy of the given object using interfaces.
- Clone() *T - Copy() *T - Clone() T - Copy() T
Types ¶
type ByteSink ¶ added in v0.3.0
type ByteSink struct {
// contains filtered or unexported fields
}
ByteSink implements the simplest form of Sink
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.
type GobSink ¶
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 ¶
SetBytes sets the object of the GobSink and its expiration time from a Gob encoded byte array
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
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
SinkFn is a TSink implementation made using a SinkType factory.
func (*SinkFn[T]) SetBytes ¶ added in v0.3.1
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
SetValue sets the object of the Sink and its expiration time.
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]) SetDefaults ¶ added in v0.3.1
SetDefaults fills the gaps and identifies errors.
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.