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

loresoft / MediatR.CommandQuery / 10111880471

26 Jul 2024 01:13PM CUT coverage: 57.794%. Remained the same
10111880471

push

github

web-flow
Merge pull request #532 from loresoft/dependabot/nuget/Microsoft.EntityFrameworkCore-8.0.7

Bump Microsoft.EntityFrameworkCore from 8.0.6 to 8.0.7

350 of 710 branches covered (49.3%)

Branch coverage included in aggregate %.

1033 of 1683 relevant lines covered (61.38%)

19.53 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
        _distributedCache = distributedCache ?? throw new ArgumentNullException(nameof(distributedCache));
×
21
        _distributedCacheSerializer = distributedCacheSerializer ?? throw new ArgumentNullException(nameof(distributedCacheSerializer));
×
22
    }
×
23

24
    protected override async Task<TResponse> Process(
25
        TRequest request,
26
        RequestHandlerDelegate<TResponse> next,
27
        CancellationToken cancellationToken)
28
    {
29
        if (next is null)
×
30
            throw new ArgumentNullException(nameof(next));
×
31

32
        if (next is null)
×
33
            throw new ArgumentNullException(nameof(next));
×
34

35
        // cache only if implements interface
36
        var cacheRequest = request as ICacheQueryResult;
×
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
        await _distributedCache
×
77
            .SetAsync(cacheKey, itemBuffer, options, cancellationToken)
×
78
            .ConfigureAwait(false);
×
79

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

82
        return result;
×
83
    }
×
84

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

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