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

loresoft / MediatR.CommandQuery / 14351800154

09 Apr 2025 07:46AM UTC coverage: 59.379%. First build
14351800154

Pull #645

github

web-flow
Merge 8eaeefc4e into d43d672f0
Pull Request #645: Bump System.Text.Json from 9.0.3 to 9.0.4

495 of 971 branches covered (50.98%)

Branch coverage included in aggregate %.

1474 of 2345 relevant lines covered (62.86%)

22.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MediatR.CommandQuery/Behaviors/HybridCacheQueryBehavior.cs
1
using MediatR.CommandQuery.Definitions;
2

3
using Microsoft.Extensions.Caching.Hybrid;
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="HybridCache"/>.
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 HybridCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15
    where TRequest : class, IRequest<TResponse>
16
{
17
    private readonly HybridCache _hybridCache;
18

19
    /// <summary>
20
    /// Initializes a new instance of the <see cref="HybridCacheQueryBehavior{TRequest, TResponse}"/> class.
21
    /// </summary>
22
    /// <param name="loggerFactory">The logger factory.</param>
23
    /// <param name="hybridCache">The hybrid cache.</param>
24
    /// <exception cref="System.ArgumentNullException">hybridCache</exception>
25
    public HybridCacheQueryBehavior(
26
        ILoggerFactory loggerFactory,
27
        HybridCache hybridCache)
28
        : base(loggerFactory)
×
29
    {
30
        _hybridCache = hybridCache ?? throw new ArgumentNullException(nameof(hybridCache));
×
31
    }
×
32

33
    /// <inheritdoc />
34
    protected override async Task<TResponse> Process(
35
        TRequest request,
36
        RequestHandlerDelegate<TResponse> next,
37
        CancellationToken cancellationToken)
38
    {
39
        ArgumentNullException.ThrowIfNull(request);
×
40
        ArgumentNullException.ThrowIfNull(next);
×
41

42
        // cache only if implements interface
43
        var cacheRequest = request as ICacheResult;
×
44
        if (cacheRequest?.IsCacheable() != true)
×
45
            return await next().ConfigureAwait(false);
×
46

47
        var cacheKey = cacheRequest.GetCacheKey();
×
48
        var cacheTag = cacheRequest.GetCacheTag();
×
49

50
        var cacheOptions = new HybridCacheEntryOptions
×
51
        {
×
52
            Expiration = cacheRequest.SlidingExpiration(),
×
53
            LocalCacheExpiration = cacheRequest.SlidingExpiration(),
×
54
        };
×
55

56
        return await _hybridCache
×
57
            .GetOrCreateAsync(
×
58
                key: cacheKey,
×
59
                factory: async token => await next().ConfigureAwait(false),
×
60
                options: cacheOptions,
×
61
                tags: string.IsNullOrEmpty(cacheTag) ? null : [cacheTag],
×
62
                cancellationToken: cancellationToken)
×
63
            .ConfigureAwait(false);
×
64
    }
×
65
}
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