codec

package
v0.0.0-...-8c930d8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package codec encodes cache values for the backends that store bytes rather than live objects.

It exists to keep encoding and decoding keyed on the same thing. The compact encoding dispatches on a value's dynamic type, taking a direct path for strings, byte slices and numbers, while decoding dispatches on the static type of its destination. The two agree whenever the cached type is concrete, and disagree exactly when it is an interface: a string stored through a Cache[any] would be written as raw bytes and then read back into an *any, where the compact form is not valid JSON and decoding fails with "unexpected value type". The exported Marshal and Unmarshal route both directions through the static T, which removes the disagreement at its source.

The encoding is process-local and carries no compatibility promise: it is not JSON for scalars, and nothing persists across a restart.

DropUnreadable is the one answer to an entry that still fails to decode, shared by every backend that stores bytes, the Redis one included.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DropUnreadable

func DropUnreadable(key string, err error, drop func() error) error

DropUnreadable answers a Get whose stored bytes cannot be decoded as the caller's type, the same way for every backend that stores bytes: the entry is logged at Warn with its key and the decode error, removed through drop so the next read does not trip over it again, and reported as a miss with types.ErrEntryNotFound, so the caller falls back to its source of truth and rewrites the entry as it would for a key never stored. A drop that fails is logged as well; the read is still a miss and the entry is retried on the next read.

func Marshal

func Marshal[T any](value T) ([]byte, error)

Marshal encodes value for storage in a byte-addressed cache.

func Unmarshal

func Unmarshal[T any](data []byte, value *T) error

Unmarshal decodes a value produced by Marshal for the same T.

Types

This section is empty.

Jump to

Keyboard shortcuts

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