Skip to content

Commit 03eaf74

Browse files
committed
Improve caching behaviors, simplify registration
1 parent 1326631 commit 03eaf74

5 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/Arbiter.CommandQuery/Behaviors/DistributedCacheQueryBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Arbiter.CommandQuery.Behaviors;
1212
/// <typeparam name="TRequest">The type of the request.</typeparam>
1313
/// <typeparam name="TResponse">The type of the response.</typeparam>
1414
public partial class DistributedCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15-
where TRequest : class, IRequest<TResponse>
15+
where TRequest : class, IRequest<TResponse>, ICacheResult
1616
{
1717
private readonly IDistributedCache _distributedCache;
1818
private readonly IDistributedCacheSerializer _distributedCacheSerializer;

src/Arbiter.CommandQuery/Behaviors/HybridCacheExpireBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Arbiter.CommandQuery.Behaviors;
1212
/// <typeparam name="TRequest">The type of the request.</typeparam>
1313
/// <typeparam name="TResponse">The type of the response.</typeparam>
1414
public partial class HybridCacheExpireBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15-
where TRequest : class, IRequest<TResponse>
15+
where TRequest : class, IRequest<TResponse>, ICacheExpire
1616
{
1717
private readonly HybridCache _hybridCache;
1818

src/Arbiter.CommandQuery/Behaviors/HybridCacheQueryBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Arbiter.CommandQuery.Behaviors;
1212
/// <typeparam name="TRequest">The type of the request.</typeparam>
1313
/// <typeparam name="TResponse">The type of the response.</typeparam>
1414
public partial class HybridCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15-
where TRequest : class, IRequest<TResponse>
15+
where TRequest : class, IRequest<TResponse>, ICacheResult
1616
{
1717
private readonly HybridCache _hybridCache;
1818

src/Arbiter.CommandQuery/Behaviors/MemoryCacheQueryBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Arbiter.CommandQuery.Behaviors;
1212
/// <typeparam name="TRequest">The type of the request.</typeparam>
1313
/// <typeparam name="TResponse">The type of the response.</typeparam>
1414
public partial class MemoryCacheQueryBehavior<TRequest, TResponse> : PipelineBehaviorBase<TRequest, TResponse>
15-
where TRequest : class, IRequest<TResponse>
15+
where TRequest : class, IRequest<TResponse>, ICacheResult
1616
{
1717
private readonly IMemoryCache _memoryCache;
1818

src/Arbiter.CommandQuery/CommandQueryExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static IServiceCollection AddCommandValidation(this IServiceCollection se
6161
return services;
6262
}
6363

64+
6465
/// <summary>
6566
/// Adds the remote dispatcher to the service collection with configuration for the HTTP client.
6667
/// </summary>
@@ -248,6 +249,31 @@ public static IServiceCollection AddEntityHybridCache<TKey, TReadModel, TCreateM
248249
return services;
249250
}
250251

252+
/// <summary>
253+
/// Adds the hybrid cache behaviors for entity commands and queries to the service collection.
254+
/// </summary>
255+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
256+
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
257+
/// <remarks>
258+
/// This method registers generic hybrid cache behaviors for all requests that implement the appropriate interfaces:
259+
/// <list type="bullet">
260+
/// <item><description>Query behaviors are registered for requests implementing <see cref="ICacheResult"/> - these provide automatic caching of query results.</description></item>
261+
/// <item><description>Expire behaviors are registered for requests implementing <see cref="ICacheExpire"/> - these handle automatic cache invalidation for commands.</description></item>
262+
/// </list>
263+
/// This is a more flexible alternative to the strongly-typed entity-specific cache registration methods,
264+
/// allowing any request type to participate in caching by implementing the appropriate marker interfaces.
265+
/// </remarks>
266+
public static IServiceCollection AddEntityHybridCache(this IServiceCollection services)
267+
{
268+
ArgumentNullException.ThrowIfNull(services);
269+
270+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(HybridCacheQueryBehavior<,>));
271+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(HybridCacheExpireBehavior<,>));
272+
273+
return services;
274+
}
275+
276+
251277
/// <summary>
252278
/// Adds the entity query behaviors to the service collection.
253279
/// </summary>

0 commit comments

Comments
 (0)