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

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

6
namespace MediatR.CommandQuery.Behaviors;
7

8
public partial class MemoryCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
9
    where TRequest : class, IRequest<TResponse>
10
{
11
    private readonly IMemoryCache _memoryCache;
12

13
    public MemoryCacheQueryBehavior(ILoggerFactory loggerFactory, IMemoryCache memoryCache) : base(loggerFactory)
×
14
    {
15
        _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
×
16
    }
×
17

18
    protected override async Task<TResponse> Process(
19
        TRequest request,
20
        RequestHandlerDelegate<TResponse> next,
21
        CancellationToken cancellationToken)
22
    {
23
        if (request is null)
24
            throw new ArgumentNullException(nameof(request));
25

26
        if (next is null)
27
            throw new ArgumentNullException(nameof(next));
28

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

34
        var cacheKey = cacheRequest.GetCacheKey();
35

36
        if (_memoryCache.TryGetValue(cacheKey, out TResponse? cachedResult))
37
        {
38
            LogCacheAction(Logger, "Hit", cacheKey);
39
            return cachedResult!;
40
        }
41

42
        LogCacheAction(Logger, "Miss", cacheKey);
43

44
        // continue if not found in cache
45
        var result = await next().ConfigureAwait(false);
46
        if (result == null)
47
            return result;
48

49
        using (var entry = _memoryCache.CreateEntry(cacheKey))
50
        {
51
            entry.SlidingExpiration = cacheRequest.SlidingExpiration();
52
            entry.AbsoluteExpiration = cacheRequest.AbsoluteExpiration();
53
            entry.SetValue(result);
54

55
            LogCacheAction(Logger, "Insert", cacheKey);
56
        }
57

58
        return result;
59
    }
60

61
    [LoggerMessage(1, LogLevel.Trace, "Cache {action}; Key: '{cacheKey}'")]
62
    static partial void LogCacheAction(ILogger logger, string action, string cacheKey);
63
}
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