-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
677 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Sample.Contracts; | ||
|
||
public record CacheRequest : IRequest<string>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
using Shiny.Mediator.Middleware; | ||
|
||
namespace Sample.Contracts; | ||
|
||
public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>, ICacheItem | ||
{ | ||
public string CacheKey { get; } | ||
}; | ||
|
||
public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>; | ||
public record MyMessageResponse(string Response); | ||
|
||
public record MyMessageEvent(string Arg, bool FireAndForgetEvents) : IEvent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Sample.Contracts; | ||
|
||
public record OfflineRequest : IRequest<string>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Sample.Contracts; | ||
|
||
namespace Sample.Handlers; | ||
|
||
|
||
[RegisterHandler] | ||
public class OfflineRequestHandler : IRequestHandler<OfflineRequest, string> | ||
{ | ||
[OfflineAvailable(true)] | ||
public Task<string> Handle(OfflineRequest request, CancellationToken cancellationToken) | ||
{ | ||
var r = DateTimeOffset.Now.ToString("h:mm:ss tt"); | ||
return Task.FromResult(r); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] | ||
public class CacheAttribute : Attribute | ||
{ | ||
public CacheItemPriority Priority { get; set; } = CacheItemPriority.Normal; | ||
public int AbsoluteExpirationSeconds { get; set; } | ||
public int SlidingExpirationSeconds { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shiny.Mediator.Caching.Infrastructure; | ||
using Shiny.Mediator.Middleware; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
|
||
public static class CacheExtensions | ||
{ | ||
public static ShinyConfigurator AddMemoryCaching(this ShinyConfigurator cfg, Action<MemoryCacheOptions> configureCache) | ||
{ | ||
cfg.Services.AddMemoryCache(configureCache); | ||
cfg.Services.AddSingleton<IEventHandler<FlushAllStoresEvent>, FlushCacheEventHandler>(); | ||
cfg.AddOpenRequestMiddleware(typeof(CachingRequestMiddleware<,>)); | ||
return cfg; | ||
} | ||
|
||
public static void Clear(this IMemoryCache cache) | ||
=> (cache as MemoryCache)?.Clear(); | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Shiny.Mediator.Caching/Infrastructure/CachingRequestMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Shiny.Mediator.Caching.Infrastructure; | ||
|
||
|
||
public class CachingRequestMiddleware<TRequest, TResult>(IMemoryCache cache) : IRequestMiddleware<TRequest, TResult> | ||
{ | ||
public async Task<TResult> Process( | ||
TRequest request, | ||
RequestHandlerDelegate<TResult> next, | ||
IRequestHandler requestHandler, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
if (typeof(TResult) == typeof(Unit)) | ||
return await next().ConfigureAwait(false); | ||
|
||
var cfg = requestHandler.GetHandlerHandleMethodAttribute<TRequest, CacheAttribute>(); | ||
if (cfg == null) | ||
return await next().ConfigureAwait(false); | ||
|
||
var cacheKey = this.GetCacheKey(request!, requestHandler); | ||
var result = await cache.GetOrCreateAsync<TResult>( | ||
cacheKey, | ||
entry => | ||
{ | ||
entry.Priority = cfg.Priority; | ||
|
||
if (cfg.AbsoluteExpirationSeconds > 0) | ||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cfg.AbsoluteExpirationSeconds); | ||
|
||
if (cfg.SlidingExpirationSeconds > 0) | ||
entry.SlidingExpiration = TimeSpan.FromSeconds(cfg.SlidingExpirationSeconds); | ||
|
||
return next(); | ||
} | ||
); | ||
|
||
return result!; | ||
} | ||
|
||
|
||
protected virtual string GetCacheKey(object request, IRequestHandler handler) | ||
{ | ||
if (request is IRequestKey keyProvider) | ||
return keyProvider.GetKey(); | ||
|
||
var t = request.GetType(); | ||
var key = $"{t.Namespace}_{t.Name}"; | ||
return key; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Shiny.Mediator.Caching/Infrastructure/FlushCacheEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Shiny.Mediator.Middleware; | ||
|
||
namespace Shiny.Mediator.Caching.Infrastructure; | ||
|
||
|
||
public class FlushCacheEventHandler(IMemoryCache cache) : IEventHandler<FlushAllStoresEvent> | ||
{ | ||
public Task Handle(FlushAllStoresEvent @event, CancellationToken cancellationToken) | ||
{ | ||
cache.Clear(); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Shiny.Mediator\Shiny.Mediator.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Shiny.Mediator; | ||
|
||
/// <summary> | ||
/// This is viewed by replay, cache, and various other services where you can control an entry | ||
/// Simply mark your IRequest or IStreamRequest and provide the necessary key to determine uniqueness | ||
/// </summary> | ||
public interface IRequestKey | ||
{ | ||
string GetKey(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Shiny.Mediator.Infrastructure; | ||
|
||
public interface IStorageManager | ||
{ | ||
void Store(object request, object result, bool isPeristent); | ||
|
||
TResult? Get<TResult>(object request, bool isPeristent); | ||
|
||
void ClearAll(); | ||
// TODO: remove(request) & clearall, clearbyrequesttype | ||
} |
Oops, something went wrong.