Skip to content

Commit

Permalink
Caching middleware and config sample updated
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Sep 22, 2024
1 parent 1e8dbba commit 3eb6f65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
11 changes: 4 additions & 7 deletions samples/Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Polly;

namespace Sample;
Expand Down Expand Up @@ -37,12 +38,8 @@ public static MauiApp CreateMauiApp()
builder.Logging.AddDebug();
builder.Services.AddBlazorWebViewDeveloperTools();
#endif
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string>
{
// NOTE: Mediator:Http is the based sub config - the namespace follows PER http generation
// the value is the base URI
{ "Mediator:Http:Http.TheActual", "https://localhost:7192/" }
}!);
builder.Configuration.AddJsonStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Sample.appsettings.json")!);

builder.Services.AddShinyMediator(x => x
.UseMaui()
.UseBlazor()
Expand Down
1 change: 1 addition & 0 deletions samples/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.1"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" Condition="'$(Configuration)' == 'Debug'"/>
Expand Down
3 changes: 2 additions & 1 deletion samples/Sample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"Http": {
// * works here - all http contracts will pick up a baseURI
"My.Namespace.Contact": "https://otherlocalhost",
"My.Namespace.*" : "https://localhost"
"My.Namespace.*" : "https://localhost",
"Http.TheActual": "https://localhost:7192"
},
"Performance": {
// * works here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,39 @@ CancellationToken cancellationToken
if (typeof(TResult) == typeof(Unit))
return await next().ConfigureAwait(false);

CacheAttribute? attribute = null;
var cacheKey = CacheExtensions.GetCacheKey(request!);
var section = configuration.GetHandlerSection("Cache", request!, requestHandler);

if (section != null)
{
// TODO: ICacheControl
var priority = section.GetValue("Priority", CacheItemPriority.Normal);
var absoluteExpirationSeconds = section.GetValue("AbsoluteExpirationSeconds", 60);
var slidingExpirationSeconds = section.GetValue("SlidingExpirationSeconds", 0);

attribute = new CacheAttribute
{
Priority = priority,
AbsoluteExpirationSeconds = absoluteExpirationSeconds,
SlidingExpirationSeconds = slidingExpirationSeconds
};
}
else
{
var attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, CacheAttribute>();
attribute = requestHandler.GetHandlerHandleMethodAttribute<TRequest, CacheAttribute>();
attribute ??= request!.GetType().GetCustomAttribute<CacheAttribute>();
if (attribute == null && request is not ICacheControl)
return await next().ConfigureAwait(false);
}

if (attribute == null && request is not ICacheControl)
return await next().ConfigureAwait(false);

TResult result = default!;
var cacheKey = CacheExtensions.GetCacheKey(request!);
if (request is ICacheControl control && control.ForceRefresh)
if (request is ICacheControl { ForceRefresh: true })
{
result = await next().ConfigureAwait(false);

var entry = cache.CreateEntry(cacheKey);
entry.Value = result;
// this.SetCacheEntry(attribute, request, entry);
this.SetCacheEntry(attribute, request, entry);
}
else
{
Expand All @@ -51,7 +62,7 @@ CancellationToken cancellationToken
cacheKey,
entry =>
{
// this.SetCacheEntry(attribute, request, entry);
this.SetCacheEntry(attribute, request, entry);
return next();
}
)
Expand All @@ -67,8 +78,7 @@ protected virtual void SetCacheEntry(CacheAttribute? attribute, TRequest request
{
entry.Priority = attribute.Priority;
if (attribute.AbsoluteExpirationSeconds > 0)
entry.AbsoluteExpirationRelativeToNow =
TimeSpan.FromSeconds(attribute.AbsoluteExpirationSeconds);
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(attribute.AbsoluteExpirationSeconds);

if (attribute.SlidingExpirationSeconds > 0)
entry.SlidingExpiration = TimeSpan.FromSeconds(attribute.SlidingExpirationSeconds);
Expand Down

0 comments on commit 3eb6f65

Please sign in to comment.