Add TypedCache[T] for pre-deserialized tree cache#54
Merged
Merged
Conversation
jerr
approved these changes
Mar 16, 2026
jerr
left a comment
There was a problem hiding this comment.
I'm not a zk expert, so I might have missed something, but this PR makes sense and should be a good improvement. 👍
TypedCache wraps TreeCache and maintains a pre-deserialized view of the tree using generics. Data is deserialized once on write (ZK event) and served as concrete objects on read, avoiding per-access unmarshal. API: - NewTypedCache[T](cache, unmarshal, opts...) — wraps a TreeCache - GetTyped(path) → (*T, *Stat, error) — lock-free typed read - AllTyped() → iterator over all typed entries - Len() → number of typed entries - WithTypedCacheListener[T](l) — chains an inner TreeCacheListener Thread safety: atomic.Pointer[sync.Map] gives lock-free reads. On re-sync, a fresh map is built and swapped atomically. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
TypedCache[T], a generic wrapper aroundTreeCachethat deserializes node data once on write (ZK event) and serves concrete typed objects on read. This eliminates per-accessUnmarshalcalls.API
Design
atomic.Pointer[sync.Map]gives lock-free concurrent reads. On re-sync after reconnection, a fresh map is built by walking the TreeCache and swapped atomically — no reader ever sees partial state.TreeCacheListenerand installs itself on the TreeCache.WithTypedCacheListenerchains an inner listener that receives all events after TypedCache processes them.GetTypedreturnsErrNoNodefor them.WithTreeCacheAbsolutePaths).Tests
GetTyped-race)