fix(cache): resolve CacheOptions from DI so multi-call AddCache merges#45
Merged
Merged
Conversation
Singletons in AddCache used to close over the local CacheOptions `o`, so a second AddCache call's RegisterType<> registrations were silently ignored even though AppendPreviousRegistrations + RemoveAll<IOptions> clearly intended to support multi-call composition. Now every factory (IFetchQueue, IManagedCacheMonitor, IEternalCache, ITimeToLiveCache, ITimeToIdleCache, IScopeCache) resolves IOptions<CacheOptions>.Value from the service provider, so the last call's merged options are honored at first resolution. Reported by Eplicta 2026-04-14 (23 production exceptions from "No freshSpan provided and no DefaultFreshSpan configured for cache type EnvironmentOption[]"). Bumps Tharga.Cache 1.0.0 -> 1.1.0. Regression test added in AddCacheIdempotencyTests.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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
AddCachesingletons used to close over the localCacheOptions o, so whenAddCachewas called twice (e.g. an app calling it directly and transitively viaAddQuilt4NetApplicationInsightsClient), the second call'sRegisterType<>registrations were silently dropped. The existingAppendPreviousRegistrationshelper +RemoveAll<IOptions<CacheOptions>>+ re-add pattern clearly intended to support multi-call composition — only the singleton-options binding was broken.The 6 affected factories (
IFetchQueue,IManagedCacheMonitor,IEternalCache,ITimeToLiveCache,ITimeToIdleCache,IScopeCache) now resolveIOptions<CacheOptions>.Valuefrom the service provider at first resolution, so they see the merged options from the lastAddCachecall.InvalidOperationException: "No freshSpan provided and no DefaultFreshSpan configured for cache type EnvironmentOption[]"fromTimeCacheBase.GetDefaultFreshSpan<T>()— the type was registered, just not in theothe first call's closure had captured.AppendPreviousRegistrationsusesTryAdd).IFetchQueueandIManagedCacheMonitorhad the same bug.Tharga.Cache1.0.0 → 1.1.0.Test plan
AddCache_CalledTwice_SecondCallRegistrationHonoredAtRuntimeadded toAddCacheIdempotencyTests— confirmed it fails on master with the exact Eplicta exception, then passes with the fixdotnet test Tharga.Cache.Tests— 474/474 passdotnet test Tharga.Cache.Redis.Tests— 2/2 passdotnet build -c Release— greenTharga.Cacheto 1.1.0 and removes the workaround inCore/Eplicta.Aggregator/Program.cs(follow-up to be tracked inRequests.md)