Documentation
¶
Overview ¶
Package cachecore defines the Store contract, shared driver configuration, inspection metadata, and the value-shaping wrapper used by every cache backend.
Index ¶
- Variables
- func DecodeOffsetCursor(cursor string) (int, error)
- func EncodeOffsetCursor(offset int) string
- func ListFilterTerm(opts ListPageOptions) string
- func NormalizeListLimit(limit int) int
- func ValidateBaseConfig(cfg BaseConfig) error
- type BaseConfig
- type CacheEntry
- type CompressionCodec
- type Driver
- type Inspector
- type InspectorCapabilities
- type ListPageOptions
- type ListPageResult
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( // ErrValueTooLarge reports that a logical or encoded cache value exceeds MaxValueBytes. ErrValueTooLarge = errors.New("cache: value exceeds max size") // ErrInvalidMaxValueBytes reports a negative MaxValueBytes configuration. ErrInvalidMaxValueBytes = errors.New("cache: max value bytes must not be negative") // ErrUnsupportedCodec reports a compression codec that this release cannot encode or decode. ErrUnsupportedCodec = errors.New("cache: unsupported compression codec") // ErrCorruptCompression reports a malformed compressed value envelope. ErrCorruptCompression = errors.New("cache: corrupt compressed payload") // ErrEncryptionKey reports an AES key whose length is not 16, 24, or 32 bytes. ErrEncryptionKey = errors.New("cache: encryption key must be 16, 24, or 32 bytes") // ErrDecryptFailed reports an invalid or wrong-key encrypted value envelope. ErrDecryptFailed = errors.New("cache: decrypt failed") // ErrEncryptValueTooBig is retained for compatibility with callers that classify shaping failures. ErrEncryptValueTooBig = errors.New("cache: encrypt value too large") )
var ErrInspectorUnsupported = errors.New("cache: inspector unsupported for this store")
ErrInspectorUnsupported reports that a Store cannot browse cache metadata.
Functions ¶
func DecodeOffsetCursor ¶ added in v0.2.0
DecodeOffsetCursor decodes an offset cursor and rejects malformed or negative values.
func EncodeOffsetCursor ¶ added in v0.2.0
EncodeOffsetCursor encodes a positive offset and returns an empty cursor for the first page.
func ListFilterTerm ¶ added in v0.2.0
func ListFilterTerm(opts ListPageOptions) string
ListFilterTerm returns Query when present and otherwise honors the legacy Prefix field.
func NormalizeListLimit ¶ added in v0.2.0
NormalizeListLimit applies the default page size and the inspector safety cap.
func ValidateBaseConfig ¶ added in v0.4.0
func ValidateBaseConfig(cfg BaseConfig) error
ValidateBaseConfig checks shaping settings without constructing or contacting a backend.
Types ¶
type BaseConfig ¶
type BaseConfig struct {
// DefaultTTL is used when an operation does not provide a positive TTL.
DefaultTTL time.Duration
// Prefix namespaces logical keys within a shared backend.
Prefix string
// Compression selects value compression when the constructing driver supports shaping.
Compression CompressionCodec
// MaxValueBytes limits value size; zero disables the limit and negative values are invalid.
MaxValueBytes int
// EncryptionKey enables AES-GCM value encryption when the constructing driver supports shaping.
EncryptionKey []byte
}
BaseConfig contains shared, backend-agnostic driver configuration.
type CacheEntry ¶ added in v0.2.0
type CacheEntry struct {
// Key is the unprefixed logical cache key.
Key string
// SizeBytes is the stored value size reported by the backend.
SizeBytes int
// ExpiresAt is the Unix timestamp in milliseconds, or nil when unavailable or non-expiring.
ExpiresAt *int64
}
CacheEntry describes one cache item without exposing its value.
func FilterAndSortEntries ¶ added in v0.2.0
func FilterAndSortEntries(entries []CacheEntry, filter string) []CacheEntry
FilterAndSortEntries applies a substring filter and returns entries in deterministic key order.
type CompressionCodec ¶
type CompressionCodec string
CompressionCodec represents a value compression algorithm.
const ( // CompressionNone leaves values uncompressed. CompressionNone CompressionCodec = "none" // CompressionGzip encodes values with gzip. CompressionGzip CompressionCodec = "gzip" // CompressionSnappy is reserved for Snappy support and is currently unsupported. CompressionSnappy CompressionCodec = "snappy" )
type Driver ¶
type Driver string
Driver identifies cache backend.
const ( // DriverNull identifies the no-op backend. DriverNull Driver = "null" // DriverFile identifies the local filesystem backend. DriverFile Driver = "file" // DriverMemory identifies the in-process memory backend. DriverMemory Driver = "memory" // DriverMemcached identifies the Memcached backend. DriverMemcached Driver = "memcached" // DriverDynamo identifies the DynamoDB backend. DriverDynamo Driver = "dynamodb" // DriverSQL identifies the shared SQL backend implementation. DriverSQL Driver = "sql" // DriverRedis identifies the Redis backend. DriverRedis Driver = "redis" // DriverNATS identifies the NATS JetStream key-value backend. DriverNATS Driver = "nats" )
type Inspector ¶ added in v0.2.0
type Inspector interface {
Capabilities() InspectorCapabilities
ListPage(ctx context.Context, opts ListPageOptions) (ListPageResult, error)
}
Inspector is an optional cache store extension for safe key browsing and metadata inspection.
Not every driver can support this efficiently or at all. Callers should check for support with a type assertion and respect Capabilities().
type InspectorCapabilities ¶ added in v0.2.0
type InspectorCapabilities struct {
// CanList reports whether the store can enumerate cache metadata.
CanList bool
// CanRead reports whether listed keys can be read through the Store contract.
CanRead bool
// CanDelete reports whether listed keys can be deleted through the Store contract.
CanDelete bool
// CanTTL reports whether entries include expiration metadata.
CanTTL bool
}
InspectorCapabilities reports which browsing features a store can support.
type ListPageOptions ¶ added in v0.2.0
type ListPageOptions struct {
// Query filters entries by matching any part of the cache key.
Query string
// Prefix is retained as a backward-compatible alias for Query.
Prefix string
// Cursor is an opaque continuation value returned by a previous page.
Cursor string
// Limit bounds the page size; implementations normalize it to the supported range.
Limit int
}
ListPageOptions controls filtering and offset-based pagination for cache inspection.
type ListPageResult ¶ added in v0.2.0
type ListPageResult struct {
// Entries contains the current page in deterministic key order when the backend supports it.
Entries []CacheEntry
// NextCursor continues the listing when HasMore is true.
NextCursor string
// HasMore reports whether another page is available.
HasMore bool
}
ListPageResult contains one page of cache metadata and its continuation state.
func SliceEntries ¶ added in v0.2.0
func SliceEntries(entries []CacheEntry, offset int, limit int) ListPageResult
SliceEntries applies normalized offset pagination to an ordered entry list.
type Store ¶
type Store interface {
// Driver identifies the concrete backend.
Driver() Driver
// Ready verifies that the backend can serve operations.
Ready(ctx context.Context) error
// Get retrieves a value and distinguishes cache misses from backend errors.
Get(ctx context.Context, key string) ([]byte, bool, error)
// Set writes a value with the requested TTL.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Add writes a value only when the key does not already exist.
Add(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error)
// Increment atomically adds delta to a numeric value when the backend supports its contract.
Increment(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
// Decrement atomically subtracts delta from a numeric value when the backend supports its contract.
Decrement(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
// Delete removes one key and succeeds when the key is absent.
Delete(ctx context.Context, key string) error
// DeleteMany removes the provided keys.
DeleteMany(ctx context.Context, keys ...string) error
// Flush removes all keys in the store's configured scope.
Flush(ctx context.Context) error
}
Store is the shared app cache contract.
func WrapStore ¶ added in v0.4.0
func WrapStore(store Store, cfg BaseConfig) (Store, error)
WrapStore applies BaseConfig value shaping to store while preserving atomic counter operations.
Existing unmarked values remain readable. When both encryption and compression are enabled, the persisted format preserves the original cache package order: an ENC1 encryption envelope wrapped by a CMP1 compression envelope. A configuration error returns both an error and a driver-preserving Store whose operations report that error, allowing constructors without an error return to remain fail-closed.