question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cache stack builder delegate which provides IServiceProvider

See original GitHub issue

My 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikPilsits-RJWcommented, Jul 3, 2022

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 hold object which gets typed in the accessor.

1reaction
ErikPilsits-RJWcommented, Jul 2, 2022

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 a CacheStack<T> due to the protection level of ServiceProviderContextActivator. 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found