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

loresoft / MediatR.CommandQuery / 12567339125

01 Jan 2025 04:52AM UTC coverage: 60.229% (-0.1%) from 60.328%
12567339125

push

github

pwelter34
switch to Testcontainers

402 of 761 branches covered (52.83%)

Branch coverage included in aggregate %.

1282 of 2035 relevant lines covered (63.0%)

19.24 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
/// <summary>
9
/// A behavior for caching the response of a query to <see cref="IMemoryCache"/>.
10
/// <typeparamref name="TRequest"/> must implement <see cref="ICacheResult"/> for the response to be cached.
11
/// </summary>
12
/// <typeparam name="TRequest">The type of the request.</typeparam>
13
/// <typeparam name="TResponse">The type of the response.</typeparam>
14
public partial class MemoryCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15
    where TRequest : class, IRequest<TResponse>
16
{
17
    private readonly IMemoryCache _memoryCache;
18

19
    public MemoryCacheQueryBehavior(ILoggerFactory loggerFactory, IMemoryCache memoryCache) : base(loggerFactory)
×
20
    {
21
        _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
×
22
    }
×
23

24
    protected override async Task<TResponse> Process(
25
        TRequest request,
26
        RequestHandlerDelegate<TResponse> next,
27
        CancellationToken cancellationToken)
28
    {
29
        ArgumentNullException.ThrowIfNull(request);
×
30
        ArgumentNullException.ThrowIfNull(next);
×
31

32
        // cache only if implements interface
33
        var cacheRequest = request as ICacheResult;
×
34
        if (cacheRequest?.IsCacheable() != true)
×
35
            return await next().ConfigureAwait(false);
×
36

37
        var cacheKey = cacheRequest.GetCacheKey();
×
38

39
        if (_memoryCache.TryGetValue(cacheKey, out TResponse? cachedResult))
×
40
        {
41
            LogCacheAction(Logger, "Hit", cacheKey);
×
42
            return cachedResult!;
×
43
        }
44

45
        LogCacheAction(Logger, "Miss", cacheKey);
×
46

47
        // continue if not found in cache
48
        var result = await next().ConfigureAwait(false);
×
49
        if (result == null)
×
50
            return result;
×
51

52
        using (var entry = _memoryCache.CreateEntry(cacheKey))
×
53
        {
54
            entry.SlidingExpiration = cacheRequest.SlidingExpiration();
×
55
            entry.AbsoluteExpiration = cacheRequest.AbsoluteExpiration();
×
56
            entry.SetValue(result);
×
57

58
            LogCacheAction(Logger, "Insert", cacheKey);
×
59
        }
×
60

61
        return result;
×
62
    }
×
63

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

© 2026 Coveralls, Inc