Skip to content

feat(typed): add Typed[T,V] wrapper for compile-time type-safe cache access#105

Merged
hyp3rd merged 2 commits intomainfrom
feat/modernize
May 4, 2026
Merged

feat(typed): add Typed[T,V] wrapper for compile-time type-safe cache access#105
hyp3rd merged 2 commits intomainfrom
feat/modernize

Conversation

@hyp3rd
Copy link
Copy Markdown
Owner

@hyp3rd hyp3rd commented May 4, 2026

Implements RFC 0002 – Option A: a thin, zero-breaking-change wrapper over HyperCache[T] that eliminates caller-side type assertions on every Get.

Changes:

  • Add hypercache.Typed[T, V] with Set, Get, GetTyped, GetOrSet, GetWithInfo, GetMultiple, Remove, and Clear; GetTyped surfaces ErrTypeMismatch vs ErrKeyNotFound for callers that need the distinction
  • Add sentinel.ErrTypeMismatch for wrong-type stored values
  • Add full test suite covering pointer/primitive round-trips, miss semantics, fail-soft type-mismatch on Get, explicit-error GetTyped, GetOrSet both branches + wrong-type guard, GetMultiple dual-map shape, and shared-underlying-cache composition pattern
  • Add docs/rfcs/0001-backend-owned-eviction.md: closed/rejected design artifact documenting the Item-embedded eviction spike, benchmark results (+11% Set, +53% Get regression), and lessons learned
  • Add docs/rfcs/0002-generic-item-typing.md: live RFC driving this change; sketches the v3 deep-generics path conditional on v2.x usage

…access

Implements RFC 0002 – Option A: a thin, zero-breaking-change wrapper over
HyperCache[T] that eliminates caller-side type assertions on every Get.

Changes:
- Add hypercache.Typed[T, V] with Set, Get, GetTyped, GetOrSet,
  GetWithInfo, GetMultiple, Remove, and Clear; GetTyped surfaces
  ErrTypeMismatch vs ErrKeyNotFound for callers that need the distinction
- Add sentinel.ErrTypeMismatch for wrong-type stored values
- Add full test suite covering pointer/primitive round-trips, miss
  semantics, fail-soft type-mismatch on Get, explicit-error GetTyped,
  GetOrSet both branches + wrong-type guard, GetMultiple dual-map shape,
  and shared-underlying-cache composition pattern
- Add docs/rfcs/0001-backend-owned-eviction.md: closed/rejected design
  artifact documenting the Item-embedded eviction spike, benchmark
  results (+11% Set, +53% Get regression), and lessons learned
- Add docs/rfcs/0002-generic-item-typing.md: live RFC driving this
  change; sketches the v3 deep-generics path conditional on v2.x usage
Copilot AI review requested due to automatic review settings May 4, 2026 10:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in, compile-time typed wrapper around HyperCache[T] to remove caller-side type assertions while keeping the existing untyped API intact, plus supporting docs and tests.

Changes:

  • Introduce hypercache.Typed[T, V] wrapper with typed Set/Get/GetTyped/GetOrSet/GetWithInfo/GetMultiple/Remove/Clear.
  • Add internal/sentinel.ErrTypeMismatch and a comprehensive typed-wrapper test suite.
  • Add RFC documentation for the typing approach (RFC 0002) and archive a rejected eviction design (RFC 0001); update cspell dictionary.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
internal/sentinel/sentinel.go Adds ErrTypeMismatch sentinel used by the typed wrapper.
hypercache_typed.go Implements the new Typed[T, V] wrapper API over HyperCache[T].
hypercache_typed_test.go Adds tests validating typed round-trips, miss/mismatch semantics, and shared-cache composition.
docs/rfcs/0002-generic-item-typing.md Documents the motivation/options for typed access (including this wrapper approach).
docs/rfcs/0001-backend-owned-eviction.md Adds a closed/rejected RFC capturing eviction spike results and lessons learned.
cspell.config.yaml Adds domain terms used in the new RFCs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread hypercache_typed.go
Comment on lines +12 to +16
// Typed is a thin wrapper around HyperCache that adds compile-time
// type-safety on Set/Get/GetOrSet/GetMultiple/GetWithInfo without
// breaking the existing untyped API. Internally Item.Value is still
// stored as any; the wrapper performs a single type assertion on read
// and rejects cross-typed entries as ErrTypeMismatch.
Comment thread hypercache_typed.go
Comment on lines +80 to +91
func (t *Typed[T, V]) GetTyped(ctx context.Context, key string) (V, error) {
var zero V

raw, ok := t.hc.Get(ctx, key)
if !ok {
return zero, sentinel.ErrKeyNotFound
}

v, ok := raw.(V)
if !ok {
return zero, sentinel.ErrTypeMismatch
}
session, ok := val.(*Session) // unsafe in general; tedious
```

Wrong type assertions panic at runtime. There is no compile-time guarantee that what was Set as `*Session` is what Get returns. In a high-stakes financial environment this is a class of latent bug the type system *can* prevent — generics are the right tool.
Comment on lines +239 to +244
## Implementation plan (Option A, v2.x)

1. Add `hypercache_typed.go` with `Typed[T, V]`. Wrap each public method on `HyperCache[T]` (Set, Get, GetOrSet, GetWithInfo, GetMultiple, Remove, Clear, List).
1. `GetMultiple` returns `map[string]V` and `map[string]error` — wrong-type entries land in the error map under a new sentinel `ErrTypeMismatch`.
1. Document the wrapper as the recommended access pattern in the package doc. Examples in [`__examples/typed/`](../../__examples/typed/) (new).
1. Tests: confirm the wrapper round-trips Set→Get cleanly for several V types ([]byte, struct, pointer, slice, map). Confirm cross-typed read returns miss without panic.
Comment thread hypercache_typed_test.go
"github.com/stretchr/testify/require"

"github.com/hyp3rd/hypercache"
"github.com/hyp3rd/hypercache/internal/sentinel"
Introduce CHANGELOG.md capturing the full v2.0.0 release history:
breaking changes, performance wins (sharded eviction, iter.Seq2
migration, xxhash consolidation), new features, and removed items.

Update README.md to document v2.0.0 additions:
- Sharded eviction section with WithEvictionShardCount guidance
- Type-safe access (Typed[V]) section with usage example
- WithDistHTTPLimits and WithDistHTTPAuth options in the reference table
- Transport hardening section covering TLS, bearer-token auth, body
  limits, lifecycle context, and LastServeError
- Roadmap table: mark TLS/auth as done since v2.0.0
@hyp3rd hyp3rd merged commit 48825dd into main May 4, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants