• 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/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
        ArgumentNullException.ThrowIfNull(request);
×
24
        ArgumentNullException.ThrowIfNull(next);
×
25

26
        // cache only if implements interface
27
        var cacheRequest = request as ICacheResult;
×
28
        if (cacheRequest?.IsCacheable() != true)
×
29
            return await next().ConfigureAwait(false);
×
30

31
        var cacheKey = cacheRequest.GetCacheKey();
×
32

33
        if (_memoryCache.TryGetValue(cacheKey, out TResponse? cachedResult))
×
34
        {
35
            LogCacheAction(Logger, "Hit", cacheKey);
×
36
            return cachedResult!;
×
37
        }
38

39
        LogCacheAction(Logger, "Miss", cacheKey);
×
40

41
        // continue if not found in cache
42
        var result = await next().ConfigureAwait(false);
×
43
        if (result == null)
×
44
            return result;
×
45

46
        using (var entry = _memoryCache.CreateEntry(cacheKey))
×
47
        {
48
            entry.SlidingExpiration = cacheRequest.SlidingExpiration();
×
49
            entry.AbsoluteExpiration = cacheRequest.AbsoluteExpiration();
×
50
            entry.SetValue(result);
×
51

52
            LogCacheAction(Logger, "Insert", cacheKey);
×
53
        }
×
54

55
        return result;
×
56
    }
×
57

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