Cache stack builder delegate which provides IServiceProvider
See original GitHub issueMy current scenario is that I have a class which provides my Redis connection, and which I register as a singleton in DI. I share this connection with other services in my project via DI. The new fluent cache stack builder doesn’t provide me a way to resolve my connection provider when building the cache stack.
I also don’t need the context in my cache stacks, but I do need to register multiple cache stacks. So below, I’ve subclassed CacheStack
to register within DI.
I’m having to do something like this currently.
services.AddSingleton(sp =>
{
var redisProvider = sp.GetRequiredService<IRedisConnectionProvider>();
return new MainCacheStack(
new ICacheLayer[]
{
new MemoryCacheLayer(),
new RedisCacheLayer(redisProvider.RedisConnection, new RedisCacheLayerOptions(ProtobufCacheSerializer.Instance, redisOptions.Database))
},
new ICacheExtension[]
{
new AutoCleanupExtension(TimeSpan.FromMinutes(5)),
new RedisLockExtension(redisProvider.RedisConnection,
new RedisLockOptions(TimeSpan.FromMinutes(1), "CacheTower:CacheLock", "Lock:{0}", redisOptions.LockDatabase,
LockCheckStrategy.WithSpinLock(TimeSpan.FromMilliseconds(100)))),
new RedisRemoteEvictionExtension(redisProvider.RedisConnection)
});
});
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
c# on application start get data from database and cache it
I am trying in my ASP.NET Core 6 MVC web application, on start, to get data from the database and cache it. I've...
Read more >Caching in .NET
This article introduces the two primary types of caching, and provides sample source code for both: Microsoft.Extensions.Caching.
Read more >ServiceProviderCache adds more and ...
When running .NET Core integration tests using WebApplicationFactory the ServiceProviderCache seems to add more and more IServiceProviders ...
Read more >Autofac.Core.DependencyResolutionException Sitecore 9.2
DependencyResolutionException: A delegate registered to create instances of ... GetService[T](IServiceProvider provider) at Sitecore.
Read more >Dependency Injection in ASP.NET Core
The delegate constructs an instance of the CompositeNotificationService, which accepts an array of INotificationService. This constructs a basic ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
On first look it’s exactly what I need. I guess it’s actually more like
IOptionsSnapshot<T>
.One thought though. Your current implementation creates new provider, lookup, and accessor singletons for every type of
ICacheStack<TContext>
. I don’t think that’s strictly necessary. The provider and lookup could holdobject
which gets typed in the accessor.I would have been comfortable using
ICacheStack<T>
with different context types for my different layers if I could have resolved dependencies. In fact I was going to use it in my solution, however I wasn’t able to directly create aCacheStack<T>
due to the protection level ofServiceProviderContextActivator
. I didn’t feel like duplicating that code locally.My initial thought for supporting multiple stacks was something akin to
IHttpContextFactory
where you could register a named stack and retrieve it through a factory class.The background refreshes are an interesting situation (having re-read it now). Thinking on my usage I believe I’m ok without using the context (project has been running for almost 2 years without it). I don’t refresh directly from any disposables that might be disposed externally, it’s all api based data fetches.