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

loresoft / MediatR.CommandQuery / 7814242280

07 Feb 2024 11:44AM CUT coverage: 58.562%. Remained the same
7814242280

Pull #486

github

web-flow
Merge 3bedacf95 into 9af6b208d
Pull Request #486: Bump Microsoft.NET.Test.Sdk from 17.8.0 to 17.9.0

230 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

560 of 911 relevant lines covered (61.47%)

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 System;
2
using System.Threading;
3
using System.Threading.Tasks;
4

5
using MediatR.CommandQuery.Definitions;
6

7
using Microsoft.Extensions.Caching.Distributed;
8
using Microsoft.Extensions.Logging;
9

10
namespace MediatR.CommandQuery.Behaviors;
11

12
public partial class DistributedCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
13
    where TRequest : class, IRequest<TResponse>
14
{
15
    private readonly IDistributedCache _distributedCache;
16
    private readonly IDistributedCacheSerializer _distributedCacheSerializer;
17

18
    public DistributedCacheQueryBehavior(
19
        ILoggerFactory loggerFactory,
20
        IDistributedCache distributedCache,
21
        IDistributedCacheSerializer distributedCacheSerializer)
22
        : base(loggerFactory)
×
23
    {
24
        _distributedCache = distributedCache ?? throw new ArgumentNullException(nameof(distributedCache));
×
25
        _distributedCacheSerializer = distributedCacheSerializer ?? throw new ArgumentNullException(nameof(distributedCacheSerializer));
×
26
    }
×
27

28
    protected override async Task<TResponse> Process(
29
        TRequest request,
30
        RequestHandlerDelegate<TResponse> next,
31
        CancellationToken cancellationToken)
32
    {
33
        if (next is null)
34
            throw new ArgumentNullException(nameof(next));
35

36
        if (next is null)
37
            throw new ArgumentNullException(nameof(next));
38

39
        // cache only if implements interface
40
        var cacheRequest = request as ICacheQueryResult;
41
        if (cacheRequest?.IsCacheable() != true)
42
            return await next().ConfigureAwait(false);
43

44
        var cacheKey = cacheRequest.GetCacheKey();
45

46
        // check cache
47
        var cachedBuffer = await _distributedCache
48
            .GetAsync(cacheKey, cancellationToken)
49
            .ConfigureAwait(false);
50

51
        if (cachedBuffer != null)
52
        {
53
            var cachedItem = await _distributedCacheSerializer
54
                .FromByteArrayAsync<TResponse>(cachedBuffer)
55
                .ConfigureAwait(false);
56

57
            LogCacheAction(Logger, "Hit", cacheKey);
58

59
            return cachedItem;
60
        }
61

62
        LogCacheAction(Logger, "Miss", cacheKey);
63

64
        // continue if not found in cache
65
        var result = await next().ConfigureAwait(false);
66
        if (result == null)
67
            return result;
68

69
        // save to cache
70
        var itemBuffer = await _distributedCacheSerializer
71
            .ToByteArrayAsync(result)
72
            .ConfigureAwait(false);
73

74
        var options = new DistributedCacheEntryOptions
75
        {
76
            SlidingExpiration = cacheRequest.SlidingExpiration(),
77
            AbsoluteExpiration = cacheRequest.AbsoluteExpiration()
78
        };
79

80
        await _distributedCache
81
            .SetAsync(cacheKey, itemBuffer, options, cancellationToken)
82
            .ConfigureAwait(false);
83

84
        LogCacheAction(Logger, "Insert", cacheKey);
85

86
        return result;
87
    }
88

89
    [LoggerMessage(1, LogLevel.Trace, "Cache {action}; Key: '{cacheKey}'")]
90
    static partial void LogCacheAction(ILogger logger, string action, string cacheKey);
91

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