kv

package
v0.57.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache added in v0.55.0

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

Cache is a namespaced, TTL-bound cache over a KV store. It layers cache semantics on top of the storage contract: reads treat every error as a miss (missing/expired keys silently, real failures logged at debug), and write failures are logged rather than surfaced, so callers degrade to uncached behavior instead of handling errors.

func NewCache added in v0.55.0

func NewCache[T any](store KV, namespace string, ttl time.Duration) *Cache[T]

NewCache returns a Cache over store, prefixing keys with namespace and expiring entries after ttl. store must be non-nil.

func (*Cache[T]) Get added in v0.55.0

func (c *Cache[T]) Get(ctx context.Context, key string) (T, bool)

Get returns the cached value for key, or ok=false on a miss.

func (*Cache[T]) Set added in v0.55.0

func (c *Cache[T]) Set(ctx context.Context, key string, value T)

Set stores value under key with the cache's TTL.

type Entry

type Entry struct {
	Key       string
	Value     json.RawMessage
	ExpiresAt *time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

Entry represents a raw KV entry with metadata.

type KV

type KV interface {
	Get(ctx context.Context, key string, dest any) error
	Set(ctx context.Context, key string, value any) error
	SetTTL(ctx context.Context, key string, value any, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	Has(ctx context.Context, key string) (bool, error)
	ListKeys(ctx context.Context) ([]string, error)
	GetRaw(ctx context.Context, key string) (Entry, error)
}

KV is the interface for a persistent key-value store. Keys are strings, values are JSON-serializable. Get on a missing key returns an error wrapping sql.ErrNoRows.

type TypedKV

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

TypedKV provides type-safe access to a KV store for a specific type T.

func Scoped

func Scoped[T any](store KV, namespace string) *TypedKV[T]

Scoped returns a TypedKV[T] that prefixes all keys with "namespace:".

func (*TypedKV[T]) Delete

func (t *TypedKV[T]) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*TypedKV[T]) Get

func (t *TypedKV[T]) Get(ctx context.Context, key string) (T, error)

Get retrieves and deserializes a value by key.

func (*TypedKV[T]) Has

func (t *TypedKV[T]) Has(ctx context.Context, key string) (bool, error)

Has returns whether a key exists.

func (*TypedKV[T]) Set

func (t *TypedKV[T]) Set(ctx context.Context, key string, value T) error

Set stores a value with no expiry.

func (*TypedKV[T]) SetTTL

func (t *TypedKV[T]) SetTTL(ctx context.Context, key string, value T, ttl time.Duration) error

SetTTL stores a value that expires after the given duration.

Jump to

Keyboard shortcuts

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