Skip to content

Commit

Permalink
Removed ICqrsBuilderExtensions as they were redundant with "CacheDyna…
Browse files Browse the repository at this point in the history
…micallyCompiledExpressions" extensions, updated the appropriate extensions, and fixed examples.
  • Loading branch information
jasonmwebb-lv committed Nov 8, 2024
1 parent db6ce60 commit 409b346
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.ApplicationServices\RCommon.ApplicationServices.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.FluentValidation\RCommon.FluentValidation.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.MemoryCache\RCommon.MemoryCache.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using RCommon.ApplicationServices;
using RCommon.Caching;
using RCommon.FluentValidation;
using RCommon.MemoryCache;
using System.Diagnostics;
using System.Reflection;

Expand All @@ -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<IMemoryCachingBuilder>();
})
.WithValidation<FluentValidationBuilder>(validation =>
{
Expand All @@ -43,8 +43,17 @@
options.ValidateCommands = true;
options.ValidateQueries = true;
});
})
.WithMemoryCaching<InMemoryCachingBuilder>(cache =>
{
cache.Configure(x =>
{
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
cache.CacheDynamicallyCompiledExpressions();
});



services.AddTransient<ITestApplicationService, TestApplicationService>();

}).Build();
Expand Down
1 change: 1 addition & 0 deletions Examples/Caching/Examples.Caching.MemoryCaching/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
cache.CacheDynamicallyCompiledExpressions();

})
.WithDistributedCaching<DistributedMemoryCacheBuilder>(cache =>
{
Expand Down
42 changes: 0 additions & 42 deletions Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ public static IDistributedMemoryCachingBuilder Configure(this IDistributedMemory
/// </summary>
/// <param name="builder">Builder</param>
/// <returns>Same builder to allow chaining</returns>
/// <remarks>The most performant way to do this is through InMemoryCache but this works fine</remarks>
/// <remarks>This is the most performant way to cache expressions!</remarks>
public static IDistributedMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this IDistributedMemoryCachingBuilder builder)
{

// Add Caching services
builder.Services.TryAddTransient<ICacheService, DistributedMemoryCacheService>();
builder.Services.TryAddTransient<DistributedMemoryCacheService>();
builder.Services.TryAddTransient<ICommonFactory<ExpressionCachingStrategy, ICacheService>, CommonFactory<ExpressionCachingStrategy, ICacheService>>();
ConfigureCachingOptions(builder);

// Add Caching Factory
builder.Services.TryAddTransient<Func<ExpressionCachingStrategy, ICacheService>>(serviceProvider => strategy =>
{
switch (strategy)
Expand All @@ -37,14 +45,26 @@ public static IDistributedMemoryCachingBuilder CacheDynamicallyCompiledExpressio
return serviceProvider.GetService<DistributedMemoryCacheService>();
}
});
builder.Services.TryAddTransient<ICommonFactory<ExpressionCachingStrategy, ICacheService>, CommonFactory<ExpressionCachingStrategy, ICacheService>>();

builder.Services.Configure<CachingOptions>(x =>
{
x.CachingEnabled = true;
x.CacheDynamicallyCompiledExpressions = true;
});
return builder;
}

private static void ConfigureCachingOptions(IDistributedMemoryCachingBuilder builder, Action<CachingOptions> configure = null)
{

if (configure == null)
{
builder.Services.Configure<CachingOptions>(x =>
{
x.CachingEnabled = true;
x.CacheDynamicallyCompiledExpressions = true;
});
}
else
{
builder.Services.Configure(configure);
}

}
}
}
35 changes: 27 additions & 8 deletions Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public static IInMemoryCachingBuilder Configure(this IInMemoryCachingBuilder bui
/// <remarks>This is the most performant way to cache expressions!</remarks>
public static IInMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this IInMemoryCachingBuilder builder)
{

// Add Caching services
builder.Services.TryAddTransient<ICacheService, InMemoryCacheService>();
builder.Services.TryAddTransient<InMemoryCacheService>();
builder.Services.TryAddTransient<ICommonFactory<ExpressionCachingStrategy, ICacheService>, CommonFactory<ExpressionCachingStrategy, ICacheService>>();
ConfigureCachingOptions(builder);

// Add Caching Factory
builder.Services.TryAddTransient<Func<ExpressionCachingStrategy, ICacheService>>(serviceProvider => strategy =>
{
switch (strategy)
Expand All @@ -37,15 +45,26 @@ public static IInMemoryCachingBuilder CacheDynamicallyCompiledExpressions(this I
return serviceProvider.GetService<InMemoryCacheService>();
}
});
builder.Services.TryAddTransient<ICacheService, InMemoryCacheService>();
builder.Services.TryAddTransient<ICommonFactory<ExpressionCachingStrategy, ICacheService>, CommonFactory<ExpressionCachingStrategy, ICacheService>>();

builder.Services.Configure<CachingOptions>(x =>
{
x.CachingEnabled = true;
x.CacheDynamicallyCompiledExpressions = true;
});

return builder;
}

private static void ConfigureCachingOptions(IInMemoryCachingBuilder builder, Action<CachingOptions> configure = null)
{

if (configure == null)
{
builder.Services.Configure<CachingOptions>(x =>
{
x.CachingEnabled = true;
x.CacheDynamicallyCompiledExpressions = true;
});
}
else
{
builder.Services.Configure(configure);
}

}
}
}

0 comments on commit 409b346

Please sign in to comment.