Documentation ¶
Overview ¶
Package cache provides a generic cache interface
Index ¶
- Variables
- type Cache
- type Getter
- type GetterFunc
- type GobSink
- func (sink *GobSink[T]) Bytes() []byte
- func (sink *GobSink[T]) Expire() time.Time
- func (sink *GobSink[T]) Len() int
- func (sink *GobSink[T]) Reset()
- func (sink *GobSink[T]) SetBytes(v []byte, e time.Time) error
- func (sink *GobSink[T]) SetString(string, time.Time) error
- func (sink *GobSink[T]) SetValue(v any, e time.Time) error
- func (sink *GobSink[T]) Value() T
- type Setter
- type SetterFunc
- type Sink
- type Stats
- type Store
- type Type
Constants ¶
This section is empty.
Variables ¶
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 ¶
A GetterFunc implements Getter with a function.
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 ¶
NewGobSink creates a new Sink using Gob as encoding and stores the object in the provided pointer
func (*GobSink[T]) Len ¶
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 ¶
SetBytes sets the object of the GobSink and its expiration time from a Gob encoded byte array
func (*GobSink[T]) SetString ¶
SetString isn't supported by GobSink, but its needed to satisfy the Sink interface
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
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 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