Skip to content

Commit

Permalink
WIP - moving attributes and improving cache/replay key management
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 23, 2024
1 parent cfb988e commit 5371299
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 48 deletions.
14 changes: 7 additions & 7 deletions Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public static MauiApp CreateMauiApp()
ErrorMessage = "You did something wrong",
ShowFullException = false
})
// .AddResiliencyMiddleware(
// ("Test", builder =>
// {
// // builder.AddRetry(new RetryStrategyOptions());
// builder.AddTimeout(TimeSpan.FromSeconds(2.0));
// })
// )
.AddResiliencyMiddleware(
("Test", builder =>
{
// builder.AddRetry(new RetryStrategyOptions());
builder.AddTimeout(TimeSpan.FromSeconds(2.0));
})
)
);
builder.Services.AddDiscoveredMediatorHandlersFromSample();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
// [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Shiny.Mediator.Tests")]
namespace Shiny.Mediator;


public enum StoreType
{
File,
Memory
}

/// <summary>
/// Implementing this interface will allow you to create your own cache key, otherwise the cache key is based on the name
/// of the request model
/// </summary>
public interface ICacheKeyProvider<TRequest, TResult> : IRequestHandler<TRequest, TResult> where TRequest : IRequest<TResult>
{
string GetCacheKey(TRequest request);
}


[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class CacheAttribute : Attribute
{
/// <summary>
///
/// </summary>
public string? Category { get; set; }

/// <summary>
/// Setting this to true will tell the middleware to always passthrough to the server if the app detects online connectivity
/// False will follow the standard expiration
Expand All @@ -29,27 +42,6 @@ public class CacheAttribute : Attribute
public StoreType Storage { get; set; }
}


[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class MainThreadAttribute : Attribute {}


[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class TimedLoggingAttribute(double errorThresholdMillis) : Attribute
{
public double ErrorThresholdMillis => errorThresholdMillis;
}


[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ReplayAttribute : Attribute {}

public interface IReplayKey<TResult> : IStreamRequest<TResult>
{
string Key { get; }
}


public record FlushAllCacheRequest : IRequest;
public record FlushCacheItemRequest(object Request) : IRequest;

Expand Down
4 changes: 4 additions & 0 deletions src/Shiny.Mediator.Maui/MainThreadAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Shiny.Mediator;

[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class MainThreadAttribute : Attribute {}
10 changes: 0 additions & 10 deletions src/Shiny.Mediator.Maui/Middleware/CacheRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,3 @@ RequestHandlerDelegate<TResult> next
);
}


/// <summary>
/// Implementing this interface will allow you to create your own cache key, otherwise the cache key is based on the name
/// of the request model
/// </summary>
public interface ICacheItem
{
string CacheKey { get; }
}

19 changes: 12 additions & 7 deletions src/Shiny.Mediator.Maui/Middleware/ReplayStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ CancellationToken cancellationToken
if (attribute == null)
return next();

return this.Iterate(request, next, cancellationToken);
return this.Iterate(request, requestHandler, next, cancellationToken);
}


protected virtual async IAsyncEnumerable<TResult> Iterate(TRequest request, StreamRequestHandlerDelegate<TResult> next, [EnumeratorCancellation] CancellationToken ct)
protected virtual async IAsyncEnumerable<TResult> Iterate(
TRequest request,
IStreamRequestHandler<TRequest, TResult> requestHandler,
StreamRequestHandlerDelegate<TResult> next,
[EnumeratorCancellation] CancellationToken ct
)
{
var path = this.GetCacheFilePath(request);

Check failure on line 37 in src/Shiny.Mediator.Maui/Middleware/ReplayStreamMiddleware.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'requestHandler' of 'ReplayStreamMiddleware<TRequest, TResult>.GetCacheFilePath(TRequest, IStreamRequestHandler<TRequest, TResult>)'

Check failure on line 37 in src/Shiny.Mediator.Maui/Middleware/ReplayStreamMiddleware.cs

View workflow job for this annotation

GitHub Actions / build

There is no argument given that corresponds to the required parameter 'requestHandler' of 'ReplayStreamMiddleware<TRequest, TResult>.GetCacheFilePath(TRequest, IStreamRequestHandler<TRequest, TResult>)'
if (File.Exists(path))
Expand All @@ -46,17 +51,17 @@ protected virtual async IAsyncEnumerable<TResult> Iterate(TRequest request, Stre
}
}

protected virtual string GetCacheFilePath(TRequest request)
protected virtual string GetCacheFilePath(TRequest request, IStreamRequestHandler<TRequest, TResult> requestHandler)
{
var key = this.GetCacheKey(request);
var key = this.GetCacheKey(request, requestHandler);
return Path.Combine(fileSystem.CacheDirectory, $"{key}.replay");
}


protected virtual string GetCacheKey(TRequest request)
protected virtual string GetCacheKey(TRequest request, IStreamRequestHandler<TRequest, TResult> requestHandler)
{
if (request is IReplayKey<TResult> key)
return key.Key;
if (requestHandler is IReplayKeyProvider<TRequest, TResult> provider)
return provider.GetReplayKey(request);

var t = request.GetType();
return $"{t.Namespace}_{t.Name}";
Expand Down
9 changes: 9 additions & 0 deletions src/Shiny.Mediator.Maui/ReplayModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Shiny.Mediator;

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ReplayAttribute : Attribute {}

public interface IReplayKeyProvider<TRequest, TResult> : IStreamRequestHandler<TRequest, TResult> where TRequest : IStreamRequest<TResult>
{
string GetReplayKey(TRequest request);
}
1 change: 1 addition & 0 deletions src/Shiny.Mediator.Maui/Shiny.Mediator.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Shiny.Mediator</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/Shiny.Mediator.Maui/TimedLoggingAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Shiny.Mediator;

[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class TimedLoggingAttribute(double errorThresholdMillis) : Attribute
{
public double ErrorThresholdMillis => errorThresholdMillis;
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.1.0-beta.{height}",
"version": "1.3.0-beta.{height}",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit 5371299

Please sign in to comment.