-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
429 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/MediatR.CommandQuery/Behaviors/HybridCacheExpireBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using MediatR.CommandQuery.Definitions; | ||
|
||
using Microsoft.Extensions.Caching.Hybrid; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MediatR.CommandQuery.Behaviors; | ||
|
||
public partial class HybridCacheExpireBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse> | ||
where TRequest : class, IRequest<TResponse> | ||
{ | ||
private readonly HybridCache _hybridCache; | ||
|
||
public HybridCacheExpireBehavior( | ||
ILoggerFactory loggerFactory, | ||
HybridCache hybridCache) | ||
: base(loggerFactory) | ||
{ | ||
ArgumentNullException.ThrowIfNull(hybridCache); | ||
|
||
_hybridCache = hybridCache; | ||
} | ||
|
||
protected override async Task<TResponse> Process( | ||
TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
ArgumentNullException.ThrowIfNull(next); | ||
|
||
var response = await next().ConfigureAwait(false); | ||
|
||
// expire cache | ||
if (request is not ICacheExpire cacheRequest) | ||
return response; | ||
|
||
var cacheTag = cacheRequest.GetCacheTag(); | ||
if (!string.IsNullOrEmpty(cacheTag)) | ||
await _hybridCache.RemoveByTagAsync(cacheTag, cancellationToken); | ||
|
||
return response; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/MediatR.CommandQuery/Behaviors/HybridCacheQueryBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using MediatR.CommandQuery.Definitions; | ||
|
||
using Microsoft.Extensions.Caching.Hybrid; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MediatR.CommandQuery.Behaviors; | ||
|
||
public partial class HybridCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse> | ||
where TRequest : class, IRequest<TResponse> | ||
{ | ||
private readonly HybridCache _hybridCache; | ||
|
||
public HybridCacheQueryBehavior( | ||
ILoggerFactory loggerFactory, | ||
HybridCache hybridCache) | ||
: base(loggerFactory) | ||
{ | ||
_hybridCache = hybridCache ?? throw new ArgumentNullException(nameof(hybridCache)); | ||
} | ||
|
||
protected override async Task<TResponse> Process( | ||
TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
ArgumentNullException.ThrowIfNull(next); | ||
|
||
// cache only if implements interface | ||
var cacheRequest = request as ICacheResult; | ||
if (cacheRequest?.IsCacheable() != true) | ||
return await next().ConfigureAwait(false); | ||
|
||
var cacheKey = cacheRequest.GetCacheKey(); | ||
var cacheTag = cacheRequest.GetCacheTag(); | ||
|
||
var cacheOptions = new HybridCacheEntryOptions | ||
{ | ||
Expiration = cacheRequest.SlidingExpiration(), | ||
LocalCacheExpiration = cacheRequest.SlidingExpiration(), | ||
}; | ||
|
||
return await _hybridCache.GetOrCreateAsync( | ||
key: cacheKey, | ||
factory: async token => await next().ConfigureAwait(false), | ||
options: cacheOptions, | ||
tags: string.IsNullOrEmpty(cacheTag) ? null : [cacheTag], | ||
cancellationToken: cancellationToken); | ||
} | ||
} |
Oops, something went wrong.