• 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/DistributedCacheQueryBehavior.cs
1
using MediatR.CommandQuery.Definitions;
2

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

6
namespace MediatR.CommandQuery.Behaviors;
7

8
public partial class DistributedCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
9
    where TRequest : class, IRequest<TResponse>
10
{
11
    private readonly IDistributedCache _distributedCache;
12
    private readonly IDistributedCacheSerializer _distributedCacheSerializer;
13

14
    public DistributedCacheQueryBehavior(
15
        ILoggerFactory loggerFactory,
16
        IDistributedCache distributedCache,
17
        IDistributedCacheSerializer distributedCacheSerializer)
18
        : base(loggerFactory)
×
19
    {
20
        ArgumentNullException.ThrowIfNull(distributedCache);
×
21
        ArgumentNullException.ThrowIfNull(distributedCacheSerializer);
×
22

23
        _distributedCache = distributedCache;
×
24
        _distributedCacheSerializer = distributedCacheSerializer;
×
25
    }
×
26

27
    protected override async Task<TResponse> Process(
28
        TRequest request,
29
        RequestHandlerDelegate<TResponse> next,
30
        CancellationToken cancellationToken)
31
    {
32
        ArgumentNullException.ThrowIfNull(request);
×
33
        ArgumentNullException.ThrowIfNull(next);
×
34

35
        // cache only if implements interface
36
        var cacheRequest = request as ICacheResult;
×
37
        if (cacheRequest?.IsCacheable() != true)
×
38
            return await next().ConfigureAwait(false);
×
39

40
        var cacheKey = cacheRequest.GetCacheKey();
×
41

42
        // check cache
43
        var cachedBuffer = await _distributedCache
×
44
            .GetAsync(cacheKey, cancellationToken)
×
45
            .ConfigureAwait(false);
×
46

47
        if (cachedBuffer != null)
×
48
        {
49
            var cachedItem = await _distributedCacheSerializer
×
50
                .FromByteArrayAsync<TResponse>(cachedBuffer)
×
51
                .ConfigureAwait(false);
×
52

53
            LogCacheAction(Logger, "Hit", cacheKey);
×
54

55
            return cachedItem;
×
56
        }
57

58
        LogCacheAction(Logger, "Miss", cacheKey);
×
59

60
        // continue if not found in cache
61
        var result = await next().ConfigureAwait(false);
×
62
        if (result == null)
×
63
            return result;
×
64

65
        // save to cache
66
        var itemBuffer = await _distributedCacheSerializer
×
67
            .ToByteArrayAsync(result)
×
68
            .ConfigureAwait(false);
×
69

70
        var options = new DistributedCacheEntryOptions
×
71
        {
×
72
            SlidingExpiration = cacheRequest.SlidingExpiration(),
×
73
            AbsoluteExpiration = cacheRequest.AbsoluteExpiration(),
×
74

×
75
        };
×
76

77
        await _distributedCache
×
78
            .SetAsync(cacheKey, itemBuffer, options, cancellationToken)
×
79
            .ConfigureAwait(false);
×
80

81
        LogCacheAction(Logger, "Insert", cacheKey);
×
82

83
        return result;
×
84
    }
×
85

86
    [LoggerMessage(1, LogLevel.Trace, "Cache {Action}; Key: '{CacheKey}'")]
87
    static partial void LogCacheAction(ILogger logger, string action, string cacheKey);
88

89
}
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