• 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/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