Skip to content

[BUG] GetOrDefault always try to read from distributed cache #472

@permagne

Description

@permagne

Describe the bug

I was doing some performance testing, and I noticed that things were slow when there was a lot of GetOrDefault with missing cache entries. These should always just try to read from memory cache. However, it looks like SkipDistributedCacheRead is not working, unless I am doing something wrong that is:

To Reproduce

   fusionCache.GetOrDefault<string>("123", options =>
        {
            options.SkipDistributedCacheRead = true;
        });

My tests show that this is significantly slower than it should be when reading only from memory cache.
The behaviour is the same for GetOrDefaultAsync/TryGet/TryGetAsync.
GetOrCreate/GetOrCreateAsync does not have this issue though.

My tests running locally, with an redis setup in azure, each GetOrDefault takes around 15-25 milliseconds.
Each tests tried to fetch a missing entry from cache

It has been setup like this:

.AddFusionCache()
                .WithDefaultEntryOptions(new FusionCacheEntryOptions {
                    Duration = TimeSpan.FromMinutes(1),
                    IsFailSafeEnabled = true,
                    FailSafeMaxDuration = TimeSpan.FromHours(2),
                    FailSafeThrottleDuration = TimeSpan.FromSeconds(30),
                    FactorySoftTimeout = TimeSpan.FromMilliseconds(100),
                    FactoryHardTimeout = TimeSpan.FromMilliseconds(1500),
                    DistributedCacheSoftTimeout = TimeSpan.FromSeconds(1),
                    DistributedCacheHardTimeout = TimeSpan.FromSeconds(2),
                    AllowBackgroundDistributedCacheOperations = true,
                    JitterMaxDuration = TimeSpan.FromSeconds(2)
                })
                .WithSerializer(
                    new FusionCacheSystemTextJsonSerializer()
                )
                .WithDistributedCache(
                    new RedisCache(new RedisCacheOptions
                    {
                        Configuration = "xxx",
                    })
                ) 

If I remove .WithDistributedCache(...), the performance is good again.

Here is one of the tests I've been using:

        var stopWatch = new Stopwatch();
        for (var i = 0; i < 10; i++)
        {
            cacheKey = Guid.NewGuid().ToString();
            stopWatch.Restart();
            _ = fusionCache.GetOrDefault<string>(cacheKey, options =>
            {
                options.SkipDistributedCacheRead = true;
            });
            output.WriteLine($"took: {stopWatch.ElapsedMilliseconds}ms");
        }

and results:

took: 26ms
took: 25ms
took: 27ms
took: 26ms
took: 23ms
took: 23ms
took: 24ms
took: 26ms
took: 26ms
took: 27ms

Expected behavior

Should be super duper fast when only trying to read from memory cache

Versions

I've encountered this issue on:

  • FusionCache 2.2.0
  • .NET 9

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions