Skip to content

feat(base-foundation): add Memoizer.computeWith for ad-hoc per-call suppliers - #98

Merged
deer merged 1 commit into
mainfrom
memoizer_computeWith
Jul 22, 2026
Merged

feat(base-foundation): add Memoizer.computeWith for ad-hoc per-call suppliers#98
deer merged 1 commit into
mainfrom
memoizer_computeWith

Conversation

@deer

@deer deer commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Memoizer previously only supported memoizing a single fixed Function<T, R> supplied at construction time via Memoizer.of(fn), so callers who wanted to share one cache across call sites with different computations for the same key type had no way to do so.

This adds computeWith(T input, Supplier<R> supplier) to the Memoizer interface, which checks the cache for input exactly like compute but falls back to invoking supplier on a miss instead of the memoizer's fixed function. On a cache hit the supplier is never invoked, and if two callers race on the same key with different suppliers only one result is memoized. compute itself is now implemented in terms of computeWith, so the two abstract bases (AbstractMemoizer and AbstractConcurrentMemoizer, including the latter's per-key ReentrantLock re-entrancy handling) each keep a single code path rather than two, and all six concrete implementations (StrongMemoizer, SoftMemoizer, WeakMemoizer, and their concurrent counterparts) get the new method for free.

@deer
deer merged commit 53b3faf into main Jul 22, 2026
3 checks passed
@deer
deer deleted the memoizer_computeWith branch July 22, 2026 05:55
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.

1 participant