• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 11937278774

20 Nov 2024 04:10PM CUT coverage: 59.17% (-0.1%) from 59.302%
11937278774

push

github

pwelter34
make HybridCache optional

389 of 745 branches covered (52.21%)

Branch coverage included in aggregate %.

0 of 6 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

1208 of 1954 relevant lines covered (61.82%)

19.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MediatR.CommandQuery/Behaviors/HybridCacheQueryBehavior.cs
1
using MediatR.CommandQuery.Definitions;
2

3
using Microsoft.Extensions.Caching.Hybrid;
4
using Microsoft.Extensions.Logging;
5

6
namespace MediatR.CommandQuery.Behaviors;
7

8
public partial class HybridCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
9
    where TRequest : class, IRequest<TResponse>
10
{
11
    private readonly HybridCache _hybridCache;
12

13
    public HybridCacheQueryBehavior(
14
        ILoggerFactory loggerFactory,
15
        HybridCache hybridCache)
16
        : base(loggerFactory)
×
17
    {
18
        _hybridCache = hybridCache ?? throw new ArgumentNullException(nameof(hybridCache));
×
19
    }
×
20

21
    protected override async Task<TResponse> Process(
22
        TRequest request,
23
        RequestHandlerDelegate<TResponse> next,
24
        CancellationToken cancellationToken)
25
    {
26
        ArgumentNullException.ThrowIfNull(request);
×
27
        ArgumentNullException.ThrowIfNull(next);
×
28

29
        // cache only if implements interface
30
        var cacheRequest = request as ICacheResult;
×
31
        if (cacheRequest?.IsCacheable() != true)
×
32
            return await next().ConfigureAwait(false);
×
33

34
        var cacheKey = cacheRequest.GetCacheKey();
×
35
        var cacheTag = cacheRequest.GetCacheTag();
×
36

37
        var cacheOptions = new HybridCacheEntryOptions
×
38
        {
×
39
            Expiration = cacheRequest.SlidingExpiration(),
×
40
            LocalCacheExpiration = cacheRequest.SlidingExpiration(),
×
41
        };
×
42

43
        return await _hybridCache
×
44
            .GetOrCreateAsync(
×
45
                key: cacheKey,
×
46
                factory: async token => await next().ConfigureAwait(false),
×
47
                options: cacheOptions,
×
48
                tags: string.IsNullOrEmpty(cacheTag) ? null : [cacheTag],
×
49
                cancellationToken: cancellationToken)
×
50
            .ConfigureAwait(false);
×
51
    }
×
52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc