Skip to content

[FEATURE] Make setting up NullFusionCache easier in ASP.NET #440

@eskild-th

Description

@eskild-th

Problem

Configuring FusionCache in a test scenario isn't as straight forward at it could be :)

I found NullFusionCache via issues and discussions.
I then had to guess how I should use/configure it.

Solution

Add some docs in docs/Testing.md 😄 (or similar)
So it's easier to find and figure out the best way to configure it.

Alternatives

Could also consider making an extension helper

builder.Services.AddNullFusionCache();

or something like that

Additional context

Be mindful of the AsHybridCache. It doesn't inject IFusionCache

return new FusionHybridCache(builder.Build(sp));

So replacing IFusionCache with the NullFusionCache doesn't work when using AsHybridCache as it'll still use whatever cache was configured first.

That means that this won't work

services.Replace(
    ServiceDescriptor.Singleton<IFusionCache>(_ => new NullFusionCache(new FusionCacheOptions()))
);

I had to do this:

services.Replace(
    ServiceDescriptor.Singleton<IFusionCache>(_ => new NullFusionCache(new FusionCacheOptions()))
);
services.Replace(
    ServiceDescriptor.Singleton<HybridCache>(sp => new FusionHybridCache(
        sp.GetRequiredService<IFusionCache>()
    ))
);

Perhaps AsHybridCache can be changed to

return new FusionHybridCache(sp.GetRequiredService<IFusionCache>());

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions