From 409b346fc8864eb857e963ac5a46b5f9cde95c09 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Fri, 8 Nov 2024 14:54:32 -0700 Subject: [PATCH] Removed ICqrsBuilderExtensions as they were redundant with "CacheDynamicallyCompiledExpressions" extensions, updated the appropriate extensions, and fixed examples. --- .../Examples.ApplicationServices.CQRS.csproj | 1 + .../Program.cs | 13 +++++- .../Examples.Caching.MemoryCaching/Program.cs | 1 + .../ICqrsBuilderExtensions.cs | 42 ------------------- ...stributedMemoryCachingBuilderExtensions.cs | 34 +++++++++++---- .../IInMemoryCachingBuilderExtensions.cs | 35 ++++++++++++---- 6 files changed, 67 insertions(+), 59 deletions(-) delete mode 100644 Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs diff --git a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Examples.ApplicationServices.CQRS.csproj b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Examples.ApplicationServices.CQRS.csproj index cf1f4e63..f014f8de 100644 --- a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Examples.ApplicationServices.CQRS.csproj +++ b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Examples.ApplicationServices.CQRS.csproj @@ -10,6 +10,7 @@ + diff --git a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs index 3c176004..4b5e5c0c 100644 --- a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs +++ b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs @@ -7,6 +7,7 @@ using RCommon.ApplicationServices; using RCommon.Caching; using RCommon.FluentValidation; +using RCommon.MemoryCache; using System.Diagnostics; using System.Reflection; @@ -32,7 +33,6 @@ // Or this way which uses a little magic but is simple cqrs.AddCommandHandlers((typeof(Program).GetTypeInfo().Assembly)); cqrs.AddQueryHandlers((typeof(Program).GetTypeInfo().Assembly)); - cqrs.AddMemoryCachingForHandlers(); }) .WithValidation(validation => { @@ -43,8 +43,17 @@ options.ValidateCommands = true; options.ValidateQueries = true; }); + }) + .WithMemoryCaching(cache => + { + cache.Configure(x => + { + x.ExpirationScanFrequency = TimeSpan.FromMinutes(1); + }); + cache.CacheDynamicallyCompiledExpressions(); }); - + + services.AddTransient(); }).Build(); diff --git a/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs b/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs index e59be1e8..56c4ebee 100644 --- a/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs +++ b/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs @@ -31,6 +31,7 @@ x.ExpirationScanFrequency = TimeSpan.FromMinutes(1); }); cache.CacheDynamicallyCompiledExpressions(); + }) .WithDistributedCaching(cache => { diff --git a/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs b/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs deleted file mode 100644 index fd9f4966..00000000 --- a/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs +++ /dev/null @@ -1,42 +0,0 @@ -using RCommon.Caching; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.ApplicationServices -{ - public static class ICqrsBuilderExtensions - { - public static ICqrsBuilder AddMemoryCachingForHandlers(this ICqrsBuilder builder) - where T : IMemoryCachingBuilder - { - return AddMemoryCachingForHandlers(builder, x => { }); - } - - public static ICqrsBuilder AddMemoryCachingForHandlers(this ICqrsBuilder builder, Action actions) - where T : IMemoryCachingBuilder - { - Guard.IsNotNull(actions, nameof(actions)); - var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); - actions(cachingConfig); - return builder; - } - - public static ICqrsBuilder AddDistributedCachingForHandlers(this ICqrsBuilder builder) - where T : IDistributedCachingBuilder - { - return AddDistributedCachingForHandlers(builder, x => { }); - } - - public static ICqrsBuilder AddDistributedCachingForHandlers(this ICqrsBuilder builder, Action actions) - where T : IDistributedCachingBuilder - { - Guard.IsNotNull(actions, nameof(actions)); - var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); - actions(cachingConfig); - return builder; - } - } -} diff --git a/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs index 5139cb42..d4dcda72 100644 --- a/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs +++ b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs @@ -24,9 +24,17 @@ public static IDistributedMemoryCachingBuilder Configure(this IDistributedMemory /// /// Builder /// Same builder to allow chaining - /// The most performant way to do this is through InMemoryCache but this works fine + /// This is the most performant way to cache expressions! public static IDistributedMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this IDistributedMemoryCachingBuilder builder) { + + // Add Caching services + builder.Services.TryAddTransient(); + builder.Services.TryAddTransient(); + builder.Services.TryAddTransient, CommonFactory>(); + ConfigureCachingOptions(builder); + + // Add Caching Factory builder.Services.TryAddTransient>(serviceProvider => strategy => { switch (strategy) @@ -37,14 +45,26 @@ public static IDistributedMemoryCachingBuilder CacheDynamicallyCompiledExpressio return serviceProvider.GetService(); } }); - builder.Services.TryAddTransient, CommonFactory>(); - builder.Services.Configure(x => - { - x.CachingEnabled = true; - x.CacheDynamicallyCompiledExpressions = true; - }); return builder; } + + private static void ConfigureCachingOptions(IDistributedMemoryCachingBuilder builder, Action configure = null) + { + + if (configure == null) + { + builder.Services.Configure(x => + { + x.CachingEnabled = true; + x.CacheDynamicallyCompiledExpressions = true; + }); + } + else + { + builder.Services.Configure(configure); + } + + } } } diff --git a/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs b/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs index 55a09805..f7ab9a40 100644 --- a/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs +++ b/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs @@ -27,6 +27,14 @@ public static IInMemoryCachingBuilder Configure(this IInMemoryCachingBuilder bui /// This is the most performant way to cache expressions! public static IInMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this IInMemoryCachingBuilder builder) { + + // Add Caching services + builder.Services.TryAddTransient(); + builder.Services.TryAddTransient(); + builder.Services.TryAddTransient, CommonFactory>(); + ConfigureCachingOptions(builder); + + // Add Caching Factory builder.Services.TryAddTransient>(serviceProvider => strategy => { switch (strategy) @@ -37,15 +45,26 @@ public static IInMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this I return serviceProvider.GetService(); } }); - builder.Services.TryAddTransient(); - builder.Services.TryAddTransient, CommonFactory>(); - - builder.Services.Configure(x => - { - x.CachingEnabled = true; - x.CacheDynamicallyCompiledExpressions = true; - }); + return builder; } + + private static void ConfigureCachingOptions(IInMemoryCachingBuilder builder, Action configure = null) + { + + if (configure == null) + { + builder.Services.Configure(x => + { + x.CachingEnabled = true; + x.CacheDynamicallyCompiledExpressions = true; + }); + } + else + { + builder.Services.Configure(configure); + } + + } } }