diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml
index d5d0476..2a5f39a 100644
--- a/.github/workflows/dotnet.yml
+++ b/.github/workflows/dotnet.yml
@@ -1,22 +1,17 @@
-# This workflow will build a .NET project
-# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
-
name: Build and Test
on:
push:
- branches: [ "*" ]
pull_request:
- branches: [ "main", "dev" ]
+ workflow_dispatch:
jobs:
build:
-
runs-on: ubuntu-latest
permissions:
pull-requests: write
- contents: write
+ contents: read
strategy:
matrix:
@@ -25,35 +20,34 @@ jobs:
build-mode: autobuild
steps:
-
- - uses: actions/checkout@v4
- - name: Setup .NET 9.0.x
- uses: actions/setup-dotnet@v4
- with:
- dotnet-version: '9.0.x'
- - name: Display dotnet version
- run: dotnet --version
- - name: Restore dependencies
- run: dotnet restore
- - name: Build
- run: dotnet build --no-restore
- - name: Test
- run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
- - name: Code Coverage Report
- uses: irongut/CodeCoverageSummary@v1.3.0
- with:
- filename: coverage/**/coverage.cobertura.xml
- badge: true
- fail_below_min: true
- format: markdown
- hide_branch_rate: false
- hide_complexity: true
- indicators: true
- output: both
- thresholds: '0'
- - name: Add Coverage PR Comment
- uses: marocchino/sticky-pull-request-comment@v2
- if: github.event_name == 'pull_request'
- with:
- recreate: true
- path: code-coverage-results.md
+ - uses: actions/checkout@v4
+ - name: Setup .NET
+ uses: actions/setup-dotnet@v4
+ with:
+ dotnet-version: "10.0.x"
+ - name: Display dotnet version
+ run: dotnet --version
+ - name: Restore dependencies
+ run: dotnet restore
+ - name: Build
+ run: dotnet build --no-restore
+ - name: Test
+ run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
+ - name: Code Coverage Report
+ uses: irongut/CodeCoverageSummary@v1.3.0
+ with:
+ filename: coverage/**/coverage.cobertura.xml
+ badge: true
+ fail_below_min: true
+ format: markdown
+ hide_branch_rate: false
+ hide_complexity: true
+ indicators: true
+ output: both
+ thresholds: "0"
+ - name: Add Coverage PR Comment
+ uses: marocchino/sticky-pull-request-comment@v2
+ if: github.event_name == 'pull_request'
+ with:
+ recreate: true
+ path: code-coverage-results.md
diff --git a/.github/workflows/release-abstractions.yml b/.github/workflows/release-abstractions.yml
new file mode 100644
index 0000000..1e06f45
--- /dev/null
+++ b/.github/workflows/release-abstractions.yml
@@ -0,0 +1,41 @@
+name: Publish Abstractions NuGet Package
+
+on:
+ workflow_dispatch:
+
+jobs:
+ publish:
+ permissions:
+ contents: read
+ id-token: write
+
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v4
+
+ - name: NuGet login (OIDC → temp API key)
+ uses: NuGet/login@v1
+ id: login
+ with:
+ user: ${{ secrets.NUGET_USER }}
+
+ - name: Setup .NET
+ uses: actions/setup-dotnet@v4
+ with:
+ dotnet-version: "10.0.x"
+
+ - name: Restore dependencies
+ run: dotnet restore
+
+ - name: Build
+ run: dotnet build --configuration Release --no-restore
+
+ - name: Test
+ run: dotnet test --configuration Release --no-build --logger trx
+
+ - name: Pack
+ run: dotnet pack ./src/MitMediator.InMemoryCache.Abstractions/MitMediator.InMemoryCache.Abstractions.csproj --configuration Release --no-build --output ./nupkg
+
+ - name: Push to NuGet
+ run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ steps.login.outputs.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
\ No newline at end of file
diff --git a/.github/workflows/release.yml b/.github/workflows/release-inmemorycache.yml
similarity index 52%
rename from .github/workflows/release.yml
rename to .github/workflows/release-inmemorycache.yml
index 27a181f..859234d 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release-inmemorycache.yml
@@ -1,21 +1,29 @@
-name: Publish NuGet Package
+name: Publish InMemoryCache NuGet Package
on:
- push:
- tags:
- - '*.*.*'
+ workflow_dispatch:
jobs:
publish:
+ permissions:
+ contents: read
+ id-token: write
+
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
+ - name: NuGet login (OIDC → temp API key)
+ uses: NuGet/login@v1
+ id: login
+ with:
+ user: ${{ secrets.NUGET_USER }}
+
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
- dotnet-version: '9.0.x'
+ dotnet-version: "10.0.x"
- name: Restore dependencies
run: dotnet restore
@@ -30,14 +38,4 @@ jobs:
run: dotnet pack ./src/MitMediator.InMemoryCache/MitMediator.InMemoryCache.csproj --configuration Release --no-build --output ./nupkg
- name: Push to NuGet
- run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
-
- - name: Create GitHub Release
- uses: softprops/action-gh-release@v2
- with:
- tag_name: ${{ github.ref_name }}
- name: v${{ github.ref_name }}
- files: ./nupkg/*.nupkg
- prerelease: ${{ steps.prerelease_check.outputs.prerelease }}
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
\ No newline at end of file
+ run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ steps.login.outputs.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
diff --git a/Directory.Packages.props b/Directory.Packages.props
new file mode 100644
index 0000000..e757189
--- /dev/null
+++ b/Directory.Packages.props
@@ -0,0 +1,18 @@
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/MitMediator.InMemoryCache.sln b/MitMediator.InMemoryCache.sln
deleted file mode 100644
index ccb967e..0000000
--- a/MitMediator.InMemoryCache.sln
+++ /dev/null
@@ -1,70 +0,0 @@
-
-Microsoft Visual Studio Solution File, Format Version 12.00
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{C906525C-0401-436F-95E8-D3376590A4D6}"
-EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BooksWebApi", "BooksWebApi", "{32DC19F4-9612-4B0E-8277-4748D62722C3}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Books.Domain", "samples\BooksWebApi\Books.Domain\Books.Domain.csproj", "{23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Books.Application", "samples\BooksWebApi\Books.Application\Books.Application.csproj", "{B31E83FA-5ADA-4460-BAFF-DBF91D54318F}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Books.Infrastructure", "samples\BooksWebApi\Books.Infrastructure\Books.Infrastructure.csproj", "{B0711161-2EF6-43EA-9F49-5C87D1C5D55B}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Books.WebApi", "samples\BooksWebApi\Books.WebApi\Books.WebApi.csproj", "{49F65175-F68D-4162-9938-2F51DB28C724}"
-EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F685C8EA-5664-4AD2-9769-595AA4A32B6B}"
-EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{FA335458-723E-4D65-A4F0-FCA77CE5DCC2}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MitMediator.InMemoryCache.Tests", "tests\MitMediator.InMemoryCache.Tests\MitMediator.InMemoryCache.Tests.csproj", "{DB870D8E-D7F4-4098-9C6B-A2D8C6533849}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MitMediator.InMemoryCache", "src\MitMediator.InMemoryCache\MitMediator.InMemoryCache.csproj", "{61C9A715-4E52-4316-8DB7-E2214DF5F40F}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleConsoleApp", "samples\SimpleConsoleApp\SimpleConsoleApp.csproj", "{DB19DE03-AB30-4A0F-B499-431F07E18DCC}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Any CPU = Debug|Any CPU
- Release|Any CPU = Release|Any CPU
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E}.Release|Any CPU.Build.0 = Release|Any CPU
- {B31E83FA-5ADA-4460-BAFF-DBF91D54318F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {B31E83FA-5ADA-4460-BAFF-DBF91D54318F}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {B31E83FA-5ADA-4460-BAFF-DBF91D54318F}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {B31E83FA-5ADA-4460-BAFF-DBF91D54318F}.Release|Any CPU.Build.0 = Release|Any CPU
- {B0711161-2EF6-43EA-9F49-5C87D1C5D55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {B0711161-2EF6-43EA-9F49-5C87D1C5D55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {B0711161-2EF6-43EA-9F49-5C87D1C5D55B}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {B0711161-2EF6-43EA-9F49-5C87D1C5D55B}.Release|Any CPU.Build.0 = Release|Any CPU
- {49F65175-F68D-4162-9938-2F51DB28C724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {49F65175-F68D-4162-9938-2F51DB28C724}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {49F65175-F68D-4162-9938-2F51DB28C724}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {49F65175-F68D-4162-9938-2F51DB28C724}.Release|Any CPU.Build.0 = Release|Any CPU
- {DB870D8E-D7F4-4098-9C6B-A2D8C6533849}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {DB870D8E-D7F4-4098-9C6B-A2D8C6533849}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {DB870D8E-D7F4-4098-9C6B-A2D8C6533849}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {DB870D8E-D7F4-4098-9C6B-A2D8C6533849}.Release|Any CPU.Build.0 = Release|Any CPU
- {61C9A715-4E52-4316-8DB7-E2214DF5F40F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {61C9A715-4E52-4316-8DB7-E2214DF5F40F}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {61C9A715-4E52-4316-8DB7-E2214DF5F40F}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {61C9A715-4E52-4316-8DB7-E2214DF5F40F}.Release|Any CPU.Build.0 = Release|Any CPU
- {DB19DE03-AB30-4A0F-B499-431F07E18DCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {DB19DE03-AB30-4A0F-B499-431F07E18DCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {DB19DE03-AB30-4A0F-B499-431F07E18DCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {DB19DE03-AB30-4A0F-B499-431F07E18DCC}.Release|Any CPU.Build.0 = Release|Any CPU
- EndGlobalSection
- GlobalSection(NestedProjects) = preSolution
- {32DC19F4-9612-4B0E-8277-4748D62722C3} = {C906525C-0401-436F-95E8-D3376590A4D6}
- {23F613B2-B1CB-48E2-9C57-A8F8C2E70C2E} = {32DC19F4-9612-4B0E-8277-4748D62722C3}
- {B31E83FA-5ADA-4460-BAFF-DBF91D54318F} = {32DC19F4-9612-4B0E-8277-4748D62722C3}
- {B0711161-2EF6-43EA-9F49-5C87D1C5D55B} = {32DC19F4-9612-4B0E-8277-4748D62722C3}
- {49F65175-F68D-4162-9938-2F51DB28C724} = {32DC19F4-9612-4B0E-8277-4748D62722C3}
- {DB870D8E-D7F4-4098-9C6B-A2D8C6533849} = {FA335458-723E-4D65-A4F0-FCA77CE5DCC2}
- {61C9A715-4E52-4316-8DB7-E2214DF5F40F} = {F685C8EA-5664-4AD2-9769-595AA4A32B6B}
- {DB19DE03-AB30-4A0F-B499-431F07E18DCC} = {C906525C-0401-436F-95E8-D3376590A4D6}
- EndGlobalSection
-EndGlobal
diff --git a/MitMediator.InMemoryCache.slnx b/MitMediator.InMemoryCache.slnx
new file mode 100644
index 0000000..631a7ab
--- /dev/null
+++ b/MitMediator.InMemoryCache.slnx
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/README.md b/README.md
index 5b7c84f..db956b1 100644
--- a/README.md
+++ b/README.md
@@ -1,141 +1,140 @@
# MitMediator.InMemoryCache
-## An attribute-driven in-memory caching extension for the [MitMediator](https://github.com/dzmprt/MitMediator)
+In-memory response caching for [MitMediator](https://github.com/dzmprt/MitMediator)
[](https://github.com/dzmprt/MitMediator.InMemoryCache/actions/workflows/dotnet.yml)

-
-
+

-## Installation
+## Setup
### 1. Add package
```sh
- dotnet add package MitMediator.InMemoryCache -v 9.0.0-alfa-2
+dotnet add package MitMediator.InMemoryCache -v 10.0.0-alfa
+```
+
+For a project that only declares requests, use `MitMediator.InMemoryCache.Abstractions` instead:
+
+```sh
+dotnet add package MitMediator.InMemoryCache.Abstractions -v 10.0.0-alfa
```
### 2. Register services
```csharp
-// Register handlers and IMediator
-builder.Services.AddMitMediator();
+using MitMediator;
-// Register MemoryCache, InMemoryCacheBehavior,
-// scan information about all IRequest<>
-builder.Services.AddRequestsInMemoryCache()
+builder.Services.AddMitMediator();
+builder.Services.AddMitMediatorInMemoryCache();
```
+By default, registration scans all loaded assemblies for cache clearing rules.
+Only the first call per service collection takes effect. Rules and locks are local to each service provider.
+
> [!WARNING]
> Make sure `.AddRequestsInMemoryCache()` is registered as the last `IPipelineBehavior`. Cached responses will short-circuit the pipeline and prevent further execution
-To customize `MemoryCache` options and specify assemblies to scan:
+Optional settings (use instead of the default cache registration):
```csharp
-builder.Services.AddRequestsInMemoryCache(
- new MemoryCacheOptions { SizeLimit = 1000 },
- new []{typeof(GetQuery).Assembly});`
-```
-
-## Usage
+using Microsoft.Extensions.Caching.Memory;
-Decorate your request classes with attribute `[CacheResponse]`
+builder.Services.AddMitMediatorInMemoryCache(
+ new MemoryCacheOptions { SizeLimit = 1000 },
+ lockStripingGatesCount: 128,
+ assemblies: [typeof(GetBookQuery).Assembly]);
+```
-Requests decorated with the `[CacheResponse]` attribute will have their responses cached in memory. You can control expiration, entry size, and define which requests should invalidate the cache
+Include all assemblies with decorated requests. The lock count defaults to 64 per request type and must be positive.
-### CacheResponseAttribute params
+## Usage
-| Name | Description |
-|------------------------|-------------------------------------------------------------------------------------------------------|
-| `expirationSeconds` | Absolute expiration time in seconds, relative to the current moment. Set null for indefinitely |
-| `entrySize` | The size of the cache entry item. For collections, size is calculated per element. Default value is 1 |
-| `requestsToClearCache` | Types of requests that will trigger cache invalidation |
+Add the attribute to a request class or struct. All extensions and the attribute use `MitMediator`:
-Use `IMediator` extension methods to clear cached responses:
+### No time limit
-Clear cache for specific request data:
```csharp
-mediator.ClearResponseCacheAsync(request, ct);
-```
+using MitMediator;
-Clear all cached responses for a request type
-```csharp
-mediator.ClearAllResponseCacheAsync(ct);
+[CacheResponseInMemory]
+public readonly record struct GetGenresQuery : IRequest;
```
-## Example usage
-
-Cache indefinitely:
-```csharp
-[CacheResponse]
-public struct GetGenresQuery : IRequest;
-```
+Stays cached until cleared or evicted.
-> Default `entrySize` is 1
+### Fixed lifetime
-Cache for 10 seconds:
```csharp
-[CacheResponse(10)]
-public struct GetBookQuery : IRequest
+[CacheResponseInMemory(30)]
+public readonly record struct GetBookQuery : IRequest
{
- public int BookId { get; set; }
+ public int BookId { get; init; }
}
```
-Invalidate cache on specific requests:
+Expires after 30 seconds. Cache hits do not extend this time.
+
+### Collection with a time limit
+
```csharp
-[CacheResponse(typeof(DeleteAuthorCommand), typeof(UpdateAuthorCommand))]
-public struct GetAuthorsByFilterQuery : IRequest
-{
- public int? Limit { get; init; }
-
- public int? Offset { get; init; }
-
- public string? FreeText { get; init; }
-}
+[CacheResponseInMemory(30, 4)]
+public readonly record struct GetBooksQuery : IRequest;
```
-Set custom entry size and time (30s)
+Expires after 30 seconds. Each book counts as 4 size units: 5 books use 20 units.
+
+### Time limit and clearing on another command
+
```csharp
-[CacheResponse(30, 4)]
-public struct GetBooksByFilterQuery : IRequest
+[CacheResponseInMemory(30, 1, typeof(UpdateAuthorCommand))]
+public readonly record struct GetAuthorQuery : IRequest
{
- public int? Limit { get; init; }
-
- public int? Offset { get; init; }
-
- public string? FreeText { get; init; }
+ public int AuthorId { get; init; }
}
```
-> For `ICollection` types, the cache entry size is `response.Count * entrySize`
+Expires after 30 seconds. A successful `UpdateAuthorCommand` clears all cached authors sooner.
+
+`expirationSeconds` is positive, or `-1` for no time limit.
+`entrySize` defaults to 1. For `System.Collections.ICollection`, size is `Count * entrySize`.
+`SizeLimit` uses these units, not bytes. There is no capacity limit by default.
+The attribute is not inherited.
+
+## Clear the cache
+
+Clear one request value or all values of a request type:
-Clear cache after updating data:
```csharp
-public async ValueTask HandleAsync(UpdateBookTitleCommand command, CancellationToken cancellationToken)
-{
- var book = await _booksRepository.FirstOrDefaultAsync(b => b.BookId == command.BookId, cancellationToken);
-
- book.SetTitle(command.Title);
-
- await _booksRepository.UpdateAsync(book, cancellationToken);
-
- // Clear cached response for the updated book
- await _mediator.ClearResponseCacheAsync(new GetBookQuery() { BookId = command.BookId }, cancellationToken);
- return book;
-}
-```
+await mediator.ClearResponseCacheAsync(
+ new GetBookQuery { BookId = bookId },
+ cancellationToken);
-> Responses are cached per unique request data.
-> For example, `GetBookQuery` caches a separate response for each `BookId`
+await mediator.ClearAllResponseCacheAsync(cancellationToken);
+```
-## See [samples](./samples)
+Await clearing after a successful data change.
+Automatic clearing also runs on a cached trigger, but not when downstream execution throws.
+The trigger needs no caching attribute.
+For `Query`, automatic clearing covers all used variants, such as `Query` and `Query`, plus matching entries added directly to the cache.
+Clearing waits for active writes before removing entries.
-## License
+## Important
-MIT
+- Cache hits skip the handler and later pipeline behaviors. Register validation and authorization before caching.
+- Keys use the runtime type's `FullName` and default JSON serialization. Use properties, not fields, for request data. Assembly identity is not part of the type prefix.
+- `null` results and exceptions are not cached. Cached objects are not copied; avoid changing them.
+- Concurrent misses for the same key run one handler if its result is cached. Different keys may share a lock.
+- Cancellation while waiting for a lock stops that caller without running its handler.
+- Do not await clearing your own cache inside a cached handler, even through another command. Nested cached requests can also deadlock.
+- The cache is local to one process and is lost on restart.
+## Samples
+- [SimpleConsoleApp](./samples/SimpleConsoleApp)
+- [BooksWebApi](./samples/BooksWebApi)
+## License
+[MIT](./LICENSE)
\ No newline at end of file
diff --git a/samples/BooksWebApi/Books.Application/Books.Application.csproj b/samples/BooksWebApi/Books.Application/Books.Application.csproj
index 2e88db1..f8366c0 100644
--- a/samples/BooksWebApi/Books.Application/Books.Application.csproj
+++ b/samples/BooksWebApi/Books.Application/Books.Application.csproj
@@ -1,15 +1,15 @@
- net9.0
+ net10.0
enable
enable
13
-
-
+
+
diff --git a/samples/BooksWebApi/Books.Application/DependencyInjection.cs b/samples/BooksWebApi/Books.Application/DependencyInjection.cs
index ecc7ef2..bbcc3c5 100644
--- a/samples/BooksWebApi/Books.Application/DependencyInjection.cs
+++ b/samples/BooksWebApi/Books.Application/DependencyInjection.cs
@@ -4,7 +4,6 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using MitMediator;
-using MitMediator.InMemoryCache;
namespace Books.Application;
@@ -22,7 +21,7 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
{
return services
.AddMitMediator()
- .AddRequestsInMemoryCache(new MemoryCacheOptions { SizeLimit = 1000 })
+ .AddMitMediatorInMemoryCache(new MemoryCacheOptions { SizeLimit = 1000 })
.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), includeInternalTypes: true)
.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
}
diff --git a/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthor/GetAuthorQuery.cs b/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthor/GetAuthorQuery.cs
index bbab2ea..02974a9 100644
--- a/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthor/GetAuthorQuery.cs
+++ b/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthor/GetAuthorQuery.cs
@@ -1,13 +1,12 @@
using Books.Domain;
using MitMediator;
-using MitMediator.InMemoryCache;
namespace Books.Application.UseCase.Authors.Queries.GetAuthor;
///
/// Get author query.
///
-[CacheResponse]
+[CacheResponseInMemory]
public struct GetAuthorQuery : IRequest
{
///
diff --git a/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthorsByFilter/GetAuthorsByFilterQuery.cs b/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthorsByFilter/GetAuthorsByFilterQuery.cs
index 336c4be..d460f70 100644
--- a/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthorsByFilter/GetAuthorsByFilterQuery.cs
+++ b/samples/BooksWebApi/Books.Application/UseCase/Authors/Queries/GetAuthorsByFilter/GetAuthorsByFilterQuery.cs
@@ -1,3 +1,4 @@
+using Books.Application.UseCase.Authors.Commands.CreateAuthor;
using Books.Application.UseCase.Authors.Commands.DeleteAuthor;
using Books.Application.UseCase.Authors.Commands.UpdateAuthor;
using Books.Domain;
@@ -9,7 +10,7 @@ namespace Books.Application.UseCase.Authors.Queries.GetAuthorsByFilter;
///
/// Get authors query.
///
-[CacheResponse(typeof(DeleteAuthorCommand), typeof(UpdateAuthorCommand))]
+[CacheResponseInMemory(typeof(DeleteAuthorCommand), typeof(UpdateAuthorCommand), typeof(CreateAuthorCommand))]
public struct GetAuthorsByFilterQuery : IRequest
{
///
diff --git a/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBook/GetBookQuery.cs b/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBook/GetBookQuery.cs
index 0655bd8..afc5233 100644
--- a/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBook/GetBookQuery.cs
+++ b/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBook/GetBookQuery.cs
@@ -1,13 +1,12 @@
using Books.Domain;
using MitMediator;
-using MitMediator.InMemoryCache;
namespace Books.Application.UseCase.Books.Queries.GetBook;
///
/// Get book query.
///
-[CacheResponse]
+[CacheResponseInMemory]
public struct GetBookQuery : IRequest
{
///
diff --git a/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBooksByFilter/GetBooksByFilterQuery.cs b/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBooksByFilter/GetBooksByFilterQuery.cs
index e3060f5..94ff2b6 100644
--- a/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBooksByFilter/GetBooksByFilterQuery.cs
+++ b/samples/BooksWebApi/Books.Application/UseCase/Books/Queries/GetBooksByFilter/GetBooksByFilterQuery.cs
@@ -1,3 +1,6 @@
+using Books.Application.UseCase.Books.Commands.CreateBook;
+using Books.Application.UseCase.Books.Commands.DeleteBook;
+using Books.Application.UseCase.Books.Commands.UpdateBook;
using Books.Domain;
using MitMediator;
using MitMediator.InMemoryCache;
@@ -7,7 +10,7 @@ namespace Books.Application.UseCase.Books.Queries.GetBooksByFilter;
///
/// Get books query.
///
-[CacheResponse(30, 2)]
+[CacheResponseInMemory(30, 2, typeof(CreateBookCommand), typeof(UpdateBookCommand), typeof(DeleteBookCommand))]
public struct GetBooksByFilterQuery : IRequest
{
///
diff --git a/samples/BooksWebApi/Books.Application/UseCase/Genres/Queries/GetGenres/GetGenresQuery.cs b/samples/BooksWebApi/Books.Application/UseCase/Genres/Queries/GetGenres/GetGenresQuery.cs
index ededcc3..861c838 100644
--- a/samples/BooksWebApi/Books.Application/UseCase/Genres/Queries/GetGenres/GetGenresQuery.cs
+++ b/samples/BooksWebApi/Books.Application/UseCase/Genres/Queries/GetGenres/GetGenresQuery.cs
@@ -7,5 +7,5 @@ namespace Books.Application.UseCase.Genres.Queries.GetGenres;
///
/// Get genres query.
///
-[CacheResponse]
+[CacheResponseInMemory]
public struct GetGenresQuery : IRequest;
\ No newline at end of file
diff --git a/samples/BooksWebApi/Books.Domain/Books.Domain.csproj b/samples/BooksWebApi/Books.Domain/Books.Domain.csproj
index bb2703c..01ac7a1 100644
--- a/samples/BooksWebApi/Books.Domain/Books.Domain.csproj
+++ b/samples/BooksWebApi/Books.Domain/Books.Domain.csproj
@@ -1,7 +1,7 @@
- net9.0
+ net10.0
enable
enable
13
diff --git a/samples/BooksWebApi/Books.Infrastructure/Books.Infrastructure.csproj b/samples/BooksWebApi/Books.Infrastructure/Books.Infrastructure.csproj
index 7932754..74d0d10 100644
--- a/samples/BooksWebApi/Books.Infrastructure/Books.Infrastructure.csproj
+++ b/samples/BooksWebApi/Books.Infrastructure/Books.Infrastructure.csproj
@@ -1,15 +1,15 @@
- net9.0
+ net10.0
enable
enable
13
-
-
+
+
diff --git a/samples/BooksWebApi/Books.WebApi/Books.WebApi.csproj b/samples/BooksWebApi/Books.WebApi/Books.WebApi.csproj
index dfc9e7f..bf41948 100644
--- a/samples/BooksWebApi/Books.WebApi/Books.WebApi.csproj
+++ b/samples/BooksWebApi/Books.WebApi/Books.WebApi.csproj
@@ -1,7 +1,7 @@
- net9.0
+ net10.0
enable
enable
13
@@ -16,7 +16,7 @@
-
+
diff --git a/samples/BooksWebApi/Books.WebApi/Program.cs b/samples/BooksWebApi/Books.WebApi/Program.cs
index ea63902..1217942 100644
--- a/samples/BooksWebApi/Books.WebApi/Program.cs
+++ b/samples/BooksWebApi/Books.WebApi/Program.cs
@@ -2,9 +2,9 @@
using Books.Application;
using Books.Infrastructure;
using Books.WebApi;
-using Microsoft.OpenApi.Models;
using Books.WebApi.Endpoints;
using Books.WebApi.Middlewares;
+using Microsoft.OpenApi;
var builder = WebApplication.CreateBuilder(args);
diff --git a/samples/SimpleConsoleApp/Program.cs b/samples/SimpleConsoleApp/Program.cs
index 0edd12b..0b83ad9 100644
--- a/samples/SimpleConsoleApp/Program.cs
+++ b/samples/SimpleConsoleApp/Program.cs
@@ -1,12 +1,11 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using MitMediator;
-using MitMediator.InMemoryCache;
var services = new ServiceCollection();
services
- .AddMitMediator(typeof(PingRequestHandler).Assembly)
- .AddRequestsInMemoryCache(new MemoryCacheOptions {SizeLimit = 100}, typeof(PingRequestHandler).Assembly);
+ .AddMitMediator()
+ .AddMitMediatorInMemoryCache(new MemoryCacheOptions {SizeLimit = 100});
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService();
@@ -19,7 +18,7 @@
result = await mediator.SendAsync(new PingRequest(), CancellationToken.None);
Console.WriteLine(result); //Pong result
-[CacheResponse]
+[CacheResponseInMemory]
public class PingRequest : IRequest;
public class PingRequestHandler : IRequestHandler
diff --git a/samples/SimpleConsoleApp/SimpleConsoleApp.csproj b/samples/SimpleConsoleApp/SimpleConsoleApp.csproj
index a23a9e4..b9cc825 100644
--- a/samples/SimpleConsoleApp/SimpleConsoleApp.csproj
+++ b/samples/SimpleConsoleApp/SimpleConsoleApp.csproj
@@ -2,7 +2,7 @@
Exe
- net9.0
+ net10.0
enable
enable
diff --git a/src/MitMediator.InMemoryCache/CacheResponseAttribute.cs b/src/MitMediator.InMemoryCache.Abstractions/CacheResponseInMemoryAttribute.cs
similarity index 89%
rename from src/MitMediator.InMemoryCache/CacheResponseAttribute.cs
rename to src/MitMediator.InMemoryCache.Abstractions/CacheResponseInMemoryAttribute.cs
index c19ff36..dd39f28 100644
--- a/src/MitMediator.InMemoryCache/CacheResponseAttribute.cs
+++ b/src/MitMediator.InMemoryCache.Abstractions/CacheResponseInMemoryAttribute.cs
@@ -1,11 +1,11 @@
-namespace MitMediator.InMemoryCache;
+namespace MitMediator;
///
/// Specifies caching behavior for request handlers, allowing responses to be cached
/// indefinitely, for a fixed duration, or until triggered by other request types.
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
-public class CacheResponseAttribute : Attribute
+public class CacheResponseInMemoryAttribute : Attribute
{
public int? AbsoluteExpirationRelativeToNowSeconds { get; private set; }
@@ -17,7 +17,7 @@ public class CacheResponseAttribute : Attribute
/// Caches the response until one of the specified request types is received.
///
/// Types of requests that will trigger cache invalidation.
- public CacheResponseAttribute(params Type[]? requestsToClearCache)
+ public CacheResponseInMemoryAttribute(params Type[]? requestsToClearCache)
{
if (requestsToClearCache is not null && requestsToClearCache.Length > 0)
{
@@ -31,7 +31,7 @@ public CacheResponseAttribute(params Type[]? requestsToClearCache)
/// Absolute expiration time in seconds, relative to the current moment. Set null for indefinitely
/// The size of the cache entry item. For collections, size is calculated per element.
/// Types of requests that will trigger cache invalidation
- public CacheResponseAttribute(
+ public CacheResponseInMemoryAttribute(
int expirationSeconds = -1,
int entrySize = 1,
params Type[] requestsToClearCache)
diff --git a/src/MitMediator.InMemoryCache.Abstractions/MitMediator.InMemoryCache.Abstractions.csproj b/src/MitMediator.InMemoryCache.Abstractions/MitMediator.InMemoryCache.Abstractions.csproj
new file mode 100644
index 0000000..14f60b3
--- /dev/null
+++ b/src/MitMediator.InMemoryCache.Abstractions/MitMediator.InMemoryCache.Abstractions.csproj
@@ -0,0 +1,34 @@
+
+
+
+ net10.0
+ MitMediator.InMemoryCache.Abstractions
+ enable
+ enable
+ 10.0.0-alfa
+ MitMediator.InMemoryCache.Abstractions
+ An attribute-driven in-memory caching extension for the MitMediator
+ https://github.com/dzmprt/MitMediator.InMemoryCache
+ mediator;request;response;queries;commands;mitmediator;cqrs;memorycache;inmemorycache
+ v10.0.0-alfa
+Change attribute name to [CacheResponseInMemory]. Add abstractions package.
+ LICENSE
+ README.md
+ logo.png
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/CacheEntryKeyUtill.cs b/src/MitMediator.InMemoryCache/CacheEntryKeyUtill.cs
deleted file mode 100644
index 9befb56..0000000
--- a/src/MitMediator.InMemoryCache/CacheEntryKeyUtill.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System.Text.Json;
-
-namespace MitMediator.InMemoryCache;
-
-internal static class ObjectGetCacheEntryKeyExtensions
-{
- public static string GetCacheEntryKey(this object obj)
- {
- // DON'T USE GetHashCode()
- return $"{obj.GetType().Name}_{JsonSerializer.Serialize(obj)}";
- }
-}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/CacheInvalidationService.cs b/src/MitMediator.InMemoryCache/CacheInvalidationService.cs
new file mode 100644
index 0000000..5828758
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/CacheInvalidationService.cs
@@ -0,0 +1,59 @@
+using System.Collections.Concurrent;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
+using static MitMediator.InMemoryCache.ObjectGetCacheEntryKeyExtensions;
+
+namespace MitMediator.InMemoryCache;
+
+internal sealed class CacheInvalidationService(MemoryCache cache, IServiceProvider serviceProvider)
+{
+ private readonly ConcurrentDictionary _closedGenericRequestTypes = new();
+
+ public void RegisterRequestType(Type requestType)
+ {
+ if (requestType.IsConstructedGenericType)
+ {
+ _closedGenericRequestTypes.TryAdd(requestType, 0);
+ }
+ }
+
+ public async ValueTask InvalidateAsync(Type requestType, CancellationToken cancellationToken, string? key = null)
+ {
+ if (requestType.IsGenericTypeDefinition)
+ {
+ var closedTypes = _closedGenericRequestTypes.Keys
+ .Where(type => type.GetGenericTypeDefinition() == requestType)
+ .ToHashSet();
+ var prefix = $"{TypePrefix}{requestType.FullName}[";
+ foreach (var cacheKey in cache.Keys.OfType())
+ {
+ if (!cacheKey.StartsWith(prefix, StringComparison.Ordinal))
+ {
+ continue;
+ }
+
+ var separatorIndex = cacheKey.IndexOf(SerializedRequestSeparator, StringComparison.Ordinal);
+ if (separatorIndex < 0)
+ {
+ continue;
+ }
+
+ var typeName = cacheKey[TypePrefix.Length..separatorIndex];
+ closedTypes.Add(requestType.Assembly.GetType(typeName, throwOnError: true)!);
+ }
+ foreach (var closedType in closedTypes)
+ {
+ await InvalidateAsync(closedType, cancellationToken);
+ }
+ return;
+ }
+
+ await GetRequestTypeLocks(requestType).InvalidateAsync(cache, key, cancellationToken);
+ }
+
+ public ICacheRequestTypeLocksStriping GetRequestTypeLocks(Type requestType)
+ {
+ var locksType = typeof(CacheRequestTypeLocksStriping<>).MakeGenericType(requestType);
+ return (ICacheRequestTypeLocksStriping)serviceProvider.GetRequiredService(locksType);
+ }
+}
diff --git a/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStriping.cs b/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStriping.cs
new file mode 100644
index 0000000..ac88d4f
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStriping.cs
@@ -0,0 +1,76 @@
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Options;
+
+namespace MitMediator.InMemoryCache;
+
+internal interface ICacheRequestTypeLocksStriping
+{
+ SemaphoreSlim ForRequest(string key);
+
+ ValueTask InvalidateAsync(MemoryCache cache, string? key, CancellationToken cancellationToken);
+}
+
+public class CacheRequestTypeLocksStriping : ICacheRequestTypeLocksStriping, IDisposable
+{
+ private readonly SemaphoreSlim[] _gates;
+
+ public CacheRequestTypeLocksStriping(IOptions options)
+ {
+ _gates = Enumerable.Range(0, options.Value.LockStripingGatesCount).Select(_ => new SemaphoreSlim(1, 1)).ToArray();
+ }
+
+ public SemaphoreSlim ForRequest(string key)
+ {
+ ArgumentNullException.ThrowIfNull(key);
+
+ var hash = unchecked((uint)StringComparer.Ordinal.GetHashCode(key));
+ var index = (int)(hash % (uint)_gates.Length);
+
+ return _gates[index];
+ }
+
+ async ValueTask ICacheRequestTypeLocksStriping.InvalidateAsync(
+ MemoryCache cache, string? key, CancellationToken cancellationToken)
+ {
+ var gates = key is null ? _gates : [ForRequest(key)];
+ var acquired = 0;
+ try
+ {
+ // fixed deadlocking
+ for (; acquired < gates.Length; acquired++)
+ {
+ await gates[acquired].WaitAsync(cancellationToken);
+ }
+
+ if (key is not null)
+ {
+ cache.Remove(key);
+ return;
+ }
+
+ var prefix = typeof(RequestType).GetCacheEntryKeyPrefix();
+ foreach (var cacheKey in cache.Keys.OfType())
+ {
+ if (cacheKey.StartsWith(prefix, StringComparison.Ordinal))
+ {
+ cache.Remove(cacheKey);
+ }
+ }
+ }
+ finally
+ {
+ for (var i = acquired - 1; i >= 0; i--)
+ {
+ gates[i].Release();
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ foreach (var gate in _gates)
+ {
+ gate.Dispose();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStripingOptions.cs b/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStripingOptions.cs
new file mode 100644
index 0000000..56f36cd
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/CacheRequestTypeLocksStripingOptions.cs
@@ -0,0 +1,6 @@
+namespace MitMediator.InMemoryCache;
+
+public sealed class CacheRequestTypeLocksStripingOptions
+{
+ public int LockStripingGatesCount { get; set; } = 64;
+}
diff --git a/src/MitMediator.InMemoryCache/ClearCacheMatrix.cs b/src/MitMediator.InMemoryCache/ClearCacheMatrix.cs
index 2de2cba..af89f24 100644
--- a/src/MitMediator.InMemoryCache/ClearCacheMatrix.cs
+++ b/src/MitMediator.InMemoryCache/ClearCacheMatrix.cs
@@ -1,9 +1,6 @@
-using System.Collections.Concurrent;
-
namespace MitMediator.InMemoryCache;
-internal static class ClearCacheMatrix
+internal sealed class ClearCacheMatrix(IReadOnlyDictionary clearCacheMatrixDictionary)
{
- public static IReadOnlyDictionary ClearCacheMatrixDictionary { get; set; } =
- new Dictionary();
+ public IReadOnlyDictionary ClearCacheMatrixDictionary { get; } = clearCacheMatrixDictionary;
}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/DependencyInjection.cs b/src/MitMediator.InMemoryCache/DependencyInjection.cs
index 0f7731a..66fa5e2 100644
--- a/src/MitMediator.InMemoryCache/DependencyInjection.cs
+++ b/src/MitMediator.InMemoryCache/DependencyInjection.cs
@@ -2,20 +2,34 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using MitMediator.InMemoryCache.Notifications;
+using MitMediator.InMemoryCache;
-namespace MitMediator.InMemoryCache;
+namespace MitMediator;
public static class DependencyInjection
{
///
/// Inject InMemoryCacheBehavior for cache requests responses.
///
+ ///
+ /// Only the first call for a service collection registers services and configures caching.
+ /// Subsequent calls leave the collection unchanged and ignore all supplied parameters.
+ ///
/// .
/// .
+ /// Count of lock cache write and invalidate SemaphoresSlim for each request type.
/// .
/// .
- public static IServiceCollection AddRequestsInMemoryCache(this IServiceCollection services, MemoryCacheOptions? memoryCacheOptions = null, params Assembly[]? assemblies)
+ public static IServiceCollection AddMitMediatorInMemoryCache(
+ this IServiceCollection services,
+ MemoryCacheOptions? memoryCacheOptions = null,
+ int lockStripingGatesCount = 64,
+ params Assembly[]? assemblies)
{
+ if (services.Any(descriptor => descriptor.ServiceType == typeof(InMemoryCacheRegistration)))
+ {
+ return services;
+ }
if (assemblies is null || assemblies.Length == 0)
{
@@ -25,7 +39,7 @@ public static IServiceCollection AddRequestsInMemoryCache(this IServiceCollectio
var types = assemblies.SelectMany(a => a.GetTypes().Where(t => !t.IsAbstract)).ToArray();
foreach (var type in types)
{
- var cacheAttribute = type.GetCustomAttribute();
+ var cacheAttribute = type.GetCustomAttribute();
if (cacheAttribute == null || cacheAttribute.RequestsToClearCache is null)
{
continue;
@@ -41,14 +55,10 @@ public static IServiceCollection AddRequestsInMemoryCache(this IServiceCollectio
}
}
- if (clearCacheMatrix.Count != 0)
- {
- ClearCacheMatrix.ClearCacheMatrixDictionary = clearCacheMatrix
- .Select(c =>
- new KeyValuePair(c.Key, c.Value.ToArray()))
- .ToDictionary();
- }
+ services.AddSingleton(_ => new ClearCacheMatrix(
+ clearCacheMatrix.ToDictionary(c => c.Key, c => c.Value.ToArray())));
+ services.AddTransient(typeof(IPipelineBehavior<,>), typeof(InMemoryClearCacheByMatrixBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(InMemoryCacheBehavior<,>));
if (memoryCacheOptions is not null)
{
@@ -66,10 +76,18 @@ public static IServiceCollection AddRequestsInMemoryCache(this IServiceCollectio
{
services.AddMemoryCache();
}
+ services.AddOptions()
+ .Configure(options => options.LockStripingGatesCount = lockStripingGatesCount)
+ .Validate(options => options.LockStripingGatesCount > 0, "LockStripingGatesCount must be greater than zero.");
+ services.AddSingleton(typeof(CacheRequestTypeLocksStriping<>), typeof(CacheRequestTypeLocksStriping<>));
services.AddSingleton();
+ services.AddSingleton();
services
.AddScoped, ClearAllResponsesCacheNotificationHandler>()
.AddScoped, ClearResponseCacheForRequestHandler>();
+ services.AddSingleton(new InMemoryCacheRegistration());
return services;
}
+
+ private sealed class InMemoryCacheRegistration;
}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/InMemoryCacheBehavior.cs b/src/MitMediator.InMemoryCache/InMemoryCacheBehavior.cs
index 394662a..e6d7f68 100644
--- a/src/MitMediator.InMemoryCache/InMemoryCacheBehavior.cs
+++ b/src/MitMediator.InMemoryCache/InMemoryCacheBehavior.cs
@@ -4,43 +4,69 @@
namespace MitMediator.InMemoryCache;
-public class InMemoryCacheBehavior(MemoryCache memoryCache)
+internal class InMemoryCacheBehavior(
+ MemoryCache memoryCache,
+ CacheRequestTypeLocksStriping cacheRequestTypeLocksStriping,
+ CacheInvalidationService invalidationService)
: IPipelineBehavior
where TRequest : IRequest
{
- public async ValueTask HandleAsync(TRequest request, IRequestHandlerNext nextPipe,
+ public async ValueTask HandleAsync(
+ TRequest request,
+ IRequestHandlerNext nextPipe,
CancellationToken cancellationToken)
{
- TResponse result;
var requestType = typeof(TRequest);
- var cacheAttribute = requestType.GetCustomAttribute();
-
+ var cacheAttribute = requestType.GetCustomAttribute();
if (cacheAttribute is null)
{
- result = await nextPipe.InvokeAsync(request, cancellationToken);
- ClearCacheByMatrix(requestType);
- return result;
+ return await nextPipe.InvokeAsync(request, cancellationToken);
}
-
- var key = request.GetCacheEntryKey();
- if (memoryCache.TryGetValue(key, out var value))
+ var runtimeRequestType = request.GetType();
+ invalidationService.RegisterRequestType(runtimeRequestType);
+ var key = request.GetCacheEntryKey();
+ if (memoryCache.TryGetValue(key, out var responseFromCache))
{
- ClearCacheByMatrix(requestType);
- return (TResponse)value;
+ return (TResponse)responseFromCache!;
}
- result = await nextPipe.InvokeAsync(request, cancellationToken);
- ClearCacheByMatrix(requestType);
-
- if (result is null)
+ var gate = runtimeRequestType == requestType
+ ? cacheRequestTypeLocksStriping.ForRequest(key)
+ : invalidationService.GetRequestTypeLocks(runtimeRequestType).ForRequest(key);
+ await gate.WaitAsync(cancellationToken);
+ try
{
+ if (memoryCache.TryGetValue(key, out responseFromCache))
+ {
+ return (TResponse)responseFromCache!;
+ }
+
+ var result = await nextPipe.InvokeAsync(request, cancellationToken);
+
+ if (result == null)
+ {
+ return result;
+ }
+
+ SetMemoryCacheValue(result, key, cacheAttribute);
return result;
}
+ finally
+ {
+ gate.Release();
+ }
+ }
- var entrySize = cacheAttribute.EntrySize;
- if (result is ICollection collection)
+ private void SetMemoryCacheValue(
+ TResponse response,
+ string key,
+ CacheResponseInMemoryAttribute
+ cacheResponseInMemoryAttribute)
+ {
+ long entrySize = cacheResponseInMemoryAttribute.EntrySize;
+ if (response is ICollection collection)
{
entrySize = entrySize * collection.Count;
}
@@ -49,43 +75,13 @@ public async ValueTask HandleAsync(TRequest request, IRequestHandlerN
{
Size = entrySize
};
-
- // TODO: check by benchmarks
- // if (!cacheAttribute.AbsoluteExpirationRelativeToNowSeconds.HasValue &&
- // cacheAttribute.RequestsToClearCache is null)
- // {
- // memoryCacheEntryOptions.Priority = CacheItemPriority.NeverRemove;
- // }
- if (cacheAttribute.AbsoluteExpirationRelativeToNowSeconds.HasValue)
+ if (cacheResponseInMemoryAttribute.AbsoluteExpirationRelativeToNowSeconds.HasValue)
{
memoryCacheEntryOptions.AbsoluteExpirationRelativeToNow =
- new TimeSpan(0, 0, cacheAttribute.AbsoluteExpirationRelativeToNowSeconds.Value);
+ new TimeSpan(0, 0, cacheResponseInMemoryAttribute.AbsoluteExpirationRelativeToNowSeconds.Value);
}
- memoryCache.Set(key, result, memoryCacheEntryOptions);
-
- return result;
- }
-
- // TODO: background task?
- private void ClearCacheByMatrix(Type request)
- {
- if (ClearCacheMatrix.ClearCacheMatrixDictionary.TryGetValue(request, out var typesToClearCache))
- {
- var requestsTypeName = typesToClearCache.Select(c => c.Name).ToArray();
- foreach (var requestTypeName in requestsTypeName)
- {
- var keys = memoryCache
- .Keys
- .Where(k => k is string && k.ToString()?.StartsWith(requestTypeName) == true)
- .Select(k => k.ToString()).ToArray();
-
- foreach (var key in keys)
- {
- memoryCache.Remove(key!);
- }
- }
- }
+ memoryCache.Set(key, response, memoryCacheEntryOptions);
}
}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/InMemoryClearCacheByMatrixBehavior.cs b/src/MitMediator.InMemoryCache/InMemoryClearCacheByMatrixBehavior.cs
new file mode 100644
index 0000000..4b0dd36
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/InMemoryClearCacheByMatrixBehavior.cs
@@ -0,0 +1,24 @@
+namespace MitMediator.InMemoryCache;
+
+internal class InMemoryClearCacheByMatrixBehavior(
+ CacheInvalidationService invalidationService,
+ ClearCacheMatrix clearCacheMatrix)
+ : IPipelineBehavior
+ where TRequest : IRequest
+{
+ public async ValueTask HandleAsync(TRequest request, IRequestHandlerNext nextPipe,
+ CancellationToken cancellationToken)
+ {
+ var result = await nextPipe.InvokeAsync(request, cancellationToken);
+ var requestType = typeof(TRequest);
+
+ if (clearCacheMatrix.ClearCacheMatrixDictionary.TryGetValue(requestType, out var typesToClearCache))
+ {
+ foreach (var typeToClearCache in typesToClearCache)
+ {
+ await invalidationService.InvalidateAsync(typeToClearCache, CancellationToken.None);
+ }
+ }
+ return result;
+ }
+}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/MediatorExtensions.cs b/src/MitMediator.InMemoryCache/MediatorExtensions.cs
index e498586..abdd220 100644
--- a/src/MitMediator.InMemoryCache/MediatorExtensions.cs
+++ b/src/MitMediator.InMemoryCache/MediatorExtensions.cs
@@ -1,6 +1,6 @@
using MitMediator.InMemoryCache.Notifications;
-namespace MitMediator.InMemoryCache;
+namespace MitMediator;
public static class MediatorExtensions
{
diff --git a/src/MitMediator.InMemoryCache/MitMediator.InMemoryCache.csproj b/src/MitMediator.InMemoryCache/MitMediator.InMemoryCache.csproj
index 6f3ad6e..d1dfd78 100644
--- a/src/MitMediator.InMemoryCache/MitMediator.InMemoryCache.csproj
+++ b/src/MitMediator.InMemoryCache/MitMediator.InMemoryCache.csproj
@@ -1,17 +1,17 @@
- net9.0
+ net10.0
MitMediator.InMemoryCache
enable
enable
- 9.0.0-alfa-2
+ 10.0.0-alfa
MitMediator.InMemoryCache
An attribute-driven in-memory caching extension for the MitMediator
https://github.com/dzmprt/MitMediator.InMemoryCache
mediator;request;response;queries;commands;mitmediator;cqrs;memorycache;inmemorycache
- v9.0.0-alfa-2
-Added extensions methods for IMediator. Unified all cache-related attributes into a single [CacheResponse] attribute
+ v10.0.0-alfa
+Change attribute name to [CacheResponseInMemory]. Add abstractions package.
LICENSE
README.md
logo.png
@@ -31,12 +31,16 @@ Added extensions methods for IMediator. Unified all cache-related attributes int
-
-
+
+
-
+
+
+
+
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/Notifications/ClearAllResponsesCacheNotification.cs b/src/MitMediator.InMemoryCache/Notifications/ClearAllResponsesCacheNotification.cs
index b568138..d43cdbc 100644
--- a/src/MitMediator.InMemoryCache/Notifications/ClearAllResponsesCacheNotification.cs
+++ b/src/MitMediator.InMemoryCache/Notifications/ClearAllResponsesCacheNotification.cs
@@ -1,5 +1,3 @@
-using Microsoft.Extensions.Caching.Memory;
-
namespace MitMediator.InMemoryCache.Notifications;
internal class ClearAllResponsesCacheNotification(Type requestType) : INotification
@@ -7,18 +5,11 @@ internal class ClearAllResponsesCacheNotification(Type requestType) : INotificat
public Type RequestType { get; private set; } = requestType;
}
-internal class ClearAllResponsesCacheNotificationHandler(MemoryCache memoryCache)
+internal class ClearAllResponsesCacheNotificationHandler(CacheInvalidationService invalidationService)
: INotificationHandler
{
public ValueTask HandleAsync(ClearAllResponsesCacheNotification cacheNotification, CancellationToken cancellationToken)
{
- var keyStart = cacheNotification.RequestType.Name;
- var entriesKeys = memoryCache.Keys
- .Where(k => k is string && k.ToString()?.StartsWith(keyStart) == true);
- foreach (var entriesKey in entriesKeys)
- {
- memoryCache.Remove(entriesKey);
- }
- return ValueTask.CompletedTask;
+ return invalidationService.InvalidateAsync(cacheNotification.RequestType, cancellationToken);
}
}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/Notifications/ClearResponseCacheForRequestNotification.cs b/src/MitMediator.InMemoryCache/Notifications/ClearResponseCacheForRequestNotification.cs
index 0ae8d5a..4cfeae9 100644
--- a/src/MitMediator.InMemoryCache/Notifications/ClearResponseCacheForRequestNotification.cs
+++ b/src/MitMediator.InMemoryCache/Notifications/ClearResponseCacheForRequestNotification.cs
@@ -1,12 +1,8 @@
-using Microsoft.Extensions.Caching.Memory;
-
namespace MitMediator.InMemoryCache.Notifications;
internal class ClearResponseCacheForRequestNotification : INotification
{
public object Request { get; private set; }
-
- // public Type RequestType { get; private set; } = requestType;
public ClearResponseCacheForRequestNotification(object request)
{
@@ -18,12 +14,11 @@ public ClearResponseCacheForRequestNotification(object request)
}
}
-internal class ClearResponseCacheForRequestHandler(MemoryCache memoryCache) : INotificationHandler
+internal class ClearResponseCacheForRequestHandler(CacheInvalidationService invalidationService) : INotificationHandler
{
public ValueTask HandleAsync(ClearResponseCacheForRequestNotification notification, CancellationToken cancellationToken)
{
var key = notification.Request!.GetCacheEntryKey();
- memoryCache.Remove(key);
- return ValueTask.CompletedTask;
+ return invalidationService.InvalidateAsync(notification.Request.GetType(), cancellationToken, key);
}
}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/ObjectGetCacheEntryKeyExtensions.cs b/src/MitMediator.InMemoryCache/ObjectGetCacheEntryKeyExtensions.cs
new file mode 100644
index 0000000..ec0afc7
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/ObjectGetCacheEntryKeyExtensions.cs
@@ -0,0 +1,17 @@
+using System.Text.Json;
+
+namespace MitMediator.InMemoryCache;
+
+internal static class ObjectGetCacheEntryKeyExtensions
+{
+ internal const string TypePrefix = "Type:";
+ internal const string SerializedRequestSeparator = "_SerializedRequest:";
+
+ public static string GetCacheEntryKey(this object obj)
+ {
+ return $"{GetCacheEntryKeyPrefix(obj.GetType())}{JsonSerializer.Serialize(obj)}";
+ }
+
+ public static string GetCacheEntryKeyPrefix(this Type requestType) =>
+ $"{TypePrefix}{requestType.FullName}{SerializedRequestSeparator}";
+}
\ No newline at end of file
diff --git a/src/MitMediator.InMemoryCache/SemaphoreSlimWithUsers.cs b/src/MitMediator.InMemoryCache/SemaphoreSlimWithUsers.cs
new file mode 100644
index 0000000..6ed42ca
--- /dev/null
+++ b/src/MitMediator.InMemoryCache/SemaphoreSlimWithUsers.cs
@@ -0,0 +1,51 @@
+namespace MitMediator.InMemoryCache;
+
+public class SemaphoreSlimWithUsers : IDisposable
+{
+ public string Name { get; private set; }
+
+ public int UsersCount { get => _usersCount; }
+
+ private int _usersCount;
+
+ private SemaphoreSlim _semaphoreSlim;
+
+ public SemaphoreSlimWithUsers(int initialCount, int maxCount, string name)
+ {
+ _semaphoreSlim = new SemaphoreSlim(initialCount, maxCount);
+ Name = name;
+ }
+
+ public async Task WaitAsync(CancellationToken cancellationToken)
+ {
+ Task waitTask;
+ lock (Name)
+ {
+ waitTask = _semaphoreSlim.WaitAsync(cancellationToken);
+ _usersCount++;
+ }
+ try
+ {
+ await waitTask;
+ }
+ catch
+ {
+ Interlocked.Decrement(ref _usersCount);
+ throw;
+ }
+ }
+
+ public void Release()
+ {
+ lock (Name)
+ {
+ _semaphoreSlim.Release();
+ _usersCount--;
+ }
+ }
+
+ public void Dispose()
+ {
+ _semaphoreSlim.Dispose();
+ }
+}
\ No newline at end of file
diff --git a/tests/MitMediator.InMemoryCache.Tests/AttributesAndDI_Tests.cs b/tests/MitMediator.InMemoryCache.Tests/AttributesAndDI_Tests.cs
index c7e55c0..2192bd6 100644
--- a/tests/MitMediator.InMemoryCache.Tests/AttributesAndDI_Tests.cs
+++ b/tests/MitMediator.InMemoryCache.Tests/AttributesAndDI_Tests.cs
@@ -1,6 +1,3 @@
-using Microsoft.Extensions.Caching.Memory;
-using Microsoft.Extensions.DependencyInjection;
-
namespace MitMediator.InMemoryCache.Tests;
public class AttributeTests
@@ -8,7 +5,7 @@ public class AttributeTests
[Fact]
public void CacheResponseAttribute_CacheForever_ShouldHaveEntrySize1ByDefaultAndOtherDataNull()
{
- var attr = new CacheResponseAttribute();
+ var attr = new CacheResponseInMemoryAttribute();
Assert.Equal(1, attr.EntrySize);
Assert.Null(attr.AbsoluteExpirationRelativeToNowSeconds);
Assert.Null(attr.RequestsToClearCache);
@@ -18,7 +15,7 @@ public void CacheResponseAttribute_CacheForever_ShouldHaveEntrySize1ByDefaultAnd
[Fact]
public void CacheResponseAttribute_CacheForTime_ShouldSetCacheTimeAndSize()
{
- var attr = new CacheResponseAttribute(42, 2);
+ var attr = new CacheResponseInMemoryAttribute(42, 2);
Assert.Equal(42, attr.AbsoluteExpirationRelativeToNowSeconds);
Assert.Equal(2, attr.EntrySize);
@@ -28,7 +25,67 @@ public void CacheResponseAttribute_CacheForTime_ShouldSetCacheTimeAndSize()
public void CacheResponseAttribute_CacheUntilTrigger_ShouldSetTriggers()
{
var types = new[] { typeof(string), typeof(int) };
- var attr = new CacheResponseAttribute(types);
+ var attr = new CacheResponseInMemoryAttribute(types);
Assert.Equal(types, attr.RequestsToClearCache);
}
+
+ [Fact]
+ public void CacheResponseInMemoryAttribute_NullTriggers_ShouldKeepDefaults()
+ {
+ var attribute = new CacheResponseInMemoryAttribute((Type[]?)null);
+
+ Assert.Null(attribute.RequestsToClearCache);
+ Assert.Null(attribute.AbsoluteExpirationRelativeToNowSeconds);
+ Assert.Equal(1, attribute.EntrySize);
+ }
+
+ [Fact]
+ public void CacheResponseInMemoryAttribute_EmptyTriggers_ShouldKeepDefaults()
+ {
+ var attribute = new CacheResponseInMemoryAttribute(Array.Empty());
+
+ Assert.Null(attribute.RequestsToClearCache);
+ Assert.Null(attribute.AbsoluteExpirationRelativeToNowSeconds);
+ Assert.Equal(1, attribute.EntrySize);
+ }
+
+ [Fact]
+ public void CacheResponseInMemoryAttribute_IndefiniteLifetime_ShouldPreserveSizeAndTriggers()
+ {
+ var triggers = new[] { typeof(string), typeof(int) };
+ var attribute = new CacheResponseInMemoryAttribute(-1, 4, triggers);
+
+ Assert.Null(attribute.AbsoluteExpirationRelativeToNowSeconds);
+ Assert.Equal(4, attribute.EntrySize);
+ Assert.Same(triggers, attribute.RequestsToClearCache);
+ }
+
+ [Fact]
+ public void CacheResponseInMemoryAttribute_FixedLifetime_ShouldPreserveTriggers()
+ {
+ var triggers = new[] { typeof(string) };
+ var attribute = new CacheResponseInMemoryAttribute(30, 2, triggers);
+
+ Assert.Equal(30, attribute.AbsoluteExpirationRelativeToNowSeconds);
+ Assert.Equal(2, attribute.EntrySize);
+ Assert.Same(triggers, attribute.RequestsToClearCache);
+ }
+
+ [Fact]
+ public void CacheResponseInMemoryAttribute_ShouldNotBeInherited()
+ {
+ var usage = Assert.Single(typeof(CacheResponseInMemoryAttribute)
+ .GetCustomAttributes(typeof(AttributeUsageAttribute), false));
+ var attributeUsage = Assert.IsType(usage);
+
+ Assert.Equal(AttributeTargets.Class | AttributeTargets.Struct, attributeUsage.ValidOn);
+ Assert.False(attributeUsage.AllowMultiple);
+ Assert.False(attributeUsage.Inherited);
+ Assert.Empty(typeof(DerivedRequest).GetCustomAttributes(typeof(CacheResponseInMemoryAttribute), true));
+ }
+
+ [CacheResponseInMemory]
+ private class BaseRequest;
+
+ private class DerivedRequest : BaseRequest;
}
\ No newline at end of file
diff --git a/tests/MitMediator.InMemoryCache.Tests/ClearCacheMatrixTests.cs b/tests/MitMediator.InMemoryCache.Tests/ClearCacheMatrixTests.cs
index a0a009a..36da29d 100644
--- a/tests/MitMediator.InMemoryCache.Tests/ClearCacheMatrixTests.cs
+++ b/tests/MitMediator.InMemoryCache.Tests/ClearCacheMatrixTests.cs
@@ -1,21 +1,23 @@
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using Microsoft.Extensions.DependencyInjection;
-using MitMediator.InMemoryCache;
-using Xunit;
-
namespace MitMediator.InMemoryCache.Tests;
public class ClearCacheMatrixTests
{
[Fact]
- public void ClearCacheMatrixDictionary_SetAndGet_WorksCorrectly()
+ public void Constructor_ShouldStoreRules()
{
var dict = new Dictionary { { typeof(string), new[] { typeof(int), typeof(double) } } };
- ClearCacheMatrix.ClearCacheMatrixDictionary = dict;
- Assert.NotNull(ClearCacheMatrix.ClearCacheMatrixDictionary);
- Assert.True(ClearCacheMatrix.ClearCacheMatrixDictionary.ContainsKey(typeof(string)));
- Assert.Equal(new[] { typeof(int), typeof(double) }, ClearCacheMatrix.ClearCacheMatrixDictionary[typeof(string)]);
+ var matrix = new ClearCacheMatrix(dict);
+
+ Assert.Equal(new[] { typeof(int), typeof(double) }, matrix.ClearCacheMatrixDictionary[typeof(string)]);
+ }
+
+ [Fact]
+ public void Instances_ShouldHaveIndependentRules()
+ {
+ var first = new ClearCacheMatrix(new Dictionary { [typeof(string)] = [typeof(int)] });
+ var second = new ClearCacheMatrix(new Dictionary());
+
+ Assert.Equal(new[] { typeof(int) }, first.ClearCacheMatrixDictionary[typeof(string)]);
+ Assert.Empty(second.ClearCacheMatrixDictionary);
}
}
diff --git a/tests/MitMediator.InMemoryCache.Tests/DependencyInjectionTests.cs b/tests/MitMediator.InMemoryCache.Tests/DependencyInjectionTests.cs
index 7e482e2..128f56b 100644
--- a/tests/MitMediator.InMemoryCache.Tests/DependencyInjectionTests.cs
+++ b/tests/MitMediator.InMemoryCache.Tests/DependencyInjectionTests.cs
@@ -1,16 +1,180 @@
+using System.Reflection;
+using System.Reflection.Emit;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Options;
+using MitMediator.InMemoryCache.Notifications;
namespace MitMediator.InMemoryCache.Tests;
public class DependencyInjectionTests
{
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public void AddMitMediatorInMemoryCache_RepeatedCalls_ShouldLeaveServicesAndConfigurationUnchanged(bool differentArguments)
+ {
+ var services = new ServiceCollection();
+ var options = new MemoryCacheOptions
+ {
+ Clock = new TestClock(),
+ SizeLimit = 123,
+ CompactionPercentage = 0.4,
+ ExpirationScanFrequency = TimeSpan.FromSeconds(17),
+ TrackLinkedCacheEntries = true,
+ TrackStatistics = true
+ };
+ var assemblies = new[] { typeof(DependencyInjectionTests).Assembly };
+ services.AddMitMediatorInMemoryCache(options, lockStripingGatesCount: 32, assemblies: assemblies);
+ var descriptors = services.ToArray();
+
+ var result = differentArguments
+ ? services.AddMitMediatorInMemoryCache(
+ new MemoryCacheOptions { SizeLimit = 999 },
+ lockStripingGatesCount: 128,
+ assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly])
+ : services.AddMitMediatorInMemoryCache(options, lockStripingGatesCount: 32, assemblies: assemblies);
+
+ Assert.Same(services, result);
+ Assert.Equal(descriptors, services.ToArray());
+ using var provider = services.BuildServiceProvider();
+ var registeredOptions = provider.GetRequiredService>().Value;
+ Assert.Same(options.Clock, registeredOptions.Clock);
+ Assert.Equal(options.SizeLimit, registeredOptions.SizeLimit);
+ Assert.Equal(options.CompactionPercentage, registeredOptions.CompactionPercentage);
+ Assert.Equal(options.ExpirationScanFrequency, registeredOptions.ExpirationScanFrequency);
+ Assert.Equal(options.TrackLinkedCacheEntries, registeredOptions.TrackLinkedCacheEntries);
+ Assert.Equal(options.TrackStatistics, registeredOptions.TrackStatistics);
+ Assert.Equal(32,
+ provider.GetRequiredService>().Value.LockStripingGatesCount);
+ Assert.Contains(typeof(FirstCachedRequest),
+ provider.GetRequiredService().ClearCacheMatrixDictionary[typeof(TriggerRequest)]);
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_RepeatedCall_ShouldPreserveDefaultOptionsAndEmptyMatrix()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ var descriptors = services.ToArray();
+
+ services.AddMitMediatorInMemoryCache(
+ new MemoryCacheOptions { SizeLimit = 123 },
+ lockStripingGatesCount: 0,
+ assemblies: [typeof(DependencyInjectionTests).Assembly]);
+
+ Assert.Equal(descriptors, services.ToArray());
+ using var provider = services.BuildServiceProvider();
+ Assert.Null(provider.GetRequiredService>().Value.SizeLimit);
+ Assert.Equal(64,
+ provider.GetRequiredService>().Value.LockStripingGatesCount);
+ Assert.Empty(provider.GetRequiredService().ClearCacheMatrixDictionary);
+ }
+
+ [Fact]
+ public async Task RegisteredPipeline_RepeatedRegistration_ShouldCacheAndInvalidateWithoutDeadlock()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediator(typeof(IMediator).Assembly);
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ var queryCalls = 0;
+ var triggerCalls = 0;
+ services.AddSingleton>(
+ new Handler((_, _) => ValueTask.FromResult($"response {++queryCalls}")));
+ services.AddSingleton>(
+ new Handler((_, _) =>
+ {
+ triggerCalls++;
+ return ValueTask.FromResult("updated");
+ }));
+ using var provider = services.BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ using var source = new CancellationTokenSource(TimeSpan.FromSeconds(5));
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var query = new FirstCachedRequest();
+
+ Assert.Collection(scope.ServiceProvider.GetServices>(),
+ behavior => Assert.IsType>(behavior),
+ behavior => Assert.IsType>(behavior));
+ Assert.Single(scope.ServiceProvider.GetServices>());
+ Assert.Single(scope.ServiceProvider.GetServices>());
+
+ Assert.Equal("response 1", await mediator.SendAsync(query, source.Token));
+ Assert.Equal("response 1", await mediator.SendAsync(query, source.Token));
+ Assert.Equal(1, queryCalls);
+
+ Assert.Equal("updated", await mediator.SendAsync(new UncachedRequest(), source.Token));
+ Assert.Equal(1, triggerCalls);
+ Assert.Equal("response 2", await mediator.SendAsync(query, source.Token));
+
+ await mediator.ClearResponseCacheAsync(query, source.Token);
+ Assert.Equal("response 3", await mediator.SendAsync(query, source.Token));
+
+ await mediator.ClearAllResponseCacheAsync(source.Token);
+ Assert.Equal("response 4", await mediator.SendAsync(query, source.Token));
+ Assert.Equal(4, queryCalls);
+ }
+
+ [Theory]
+ [InlineData(64)]
+ [InlineData(128)]
+ public void AddMitMediatorInMemoryCache_ShouldConfigureLocksStriping(int gatesCount)
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(
+ lockStripingGatesCount: gatesCount,
+ assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ using var provider = services.BuildServiceProvider();
+ using var scope = provider.CreateScope();
+
+ Assert.Equal(gatesCount,
+ provider.GetRequiredService>().Value.LockStripingGatesCount);
+ var locks = provider.GetRequiredService>();
+ Assert.Same(locks, scope.ServiceProvider.GetRequiredService>());
+ Assert.NotSame((object)locks, provider.GetRequiredService>());
+
+ var gatesField = typeof(CacheRequestTypeLocksStriping)
+ .GetField("_gates", BindingFlags.Instance | BindingFlags.NonPublic)!;
+ var gates = Assert.IsType(gatesField.GetValue(locks));
+ Assert.Equal(gatesCount, gates.Length);
+ Assert.All(gates, gate => Assert.Equal(1, gate.CurrentCount));
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_ShouldConfigureDefaultLocksStriping()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ using var provider = services.BuildServiceProvider();
+
+ Assert.Equal(64,
+ provider.GetRequiredService>().Value.LockStripingGatesCount);
+ Assert.NotNull(provider.GetRequiredService>());
+ }
+
+ [Theory]
+ [InlineData(0)]
+ [InlineData(-1)]
+ public void AddMitMediatorInMemoryCache_InvalidGatesCount_ShouldRejectLocksStriping(int gatesCount)
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(
+ lockStripingGatesCount: gatesCount,
+ assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ using var provider = services.BuildServiceProvider();
+
+ Assert.Throws(
+ () => provider.GetRequiredService>());
+ }
+
[Fact]
public void AddRequestsInMemoryCache_ShouldRegisterServices()
{
var services = new ServiceCollection();
- services.AddRequestsInMemoryCache();
- var provider = services.BuildServiceProvider();
+ services.AddMitMediatorInMemoryCache();
+ using var provider = services.BuildServiceProvider();
Assert.NotNull(provider.GetService());
}
@@ -19,9 +183,270 @@ public void AddRequestsInMemoryCache_WithOptions_ShouldRegisterWithOptions()
{
var services = new ServiceCollection();
var options = new MemoryCacheOptions { SizeLimit = 123 };
- services.AddRequestsInMemoryCache(options);
- var provider = services.BuildServiceProvider();
+ services.AddMitMediatorInMemoryCache(options);
+ using var provider = services.BuildServiceProvider();
var cache = provider.GetService()!;
Assert.NotNull(cache);
}
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_ShouldCopyAllSupportedOptions()
+ {
+ var services = new ServiceCollection();
+ var options = new MemoryCacheOptions
+ {
+ Clock = new TestClock(),
+ SizeLimit = 123,
+ CompactionPercentage = 0.4,
+ ExpirationScanFrequency = TimeSpan.FromSeconds(17),
+ TrackLinkedCacheEntries = true,
+ TrackStatistics = true
+ };
+
+ Assert.Same(services, services.AddMitMediatorInMemoryCache(options, assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]));
+ using var provider = services.BuildServiceProvider();
+ var registeredOptions = provider.GetRequiredService>().Value;
+
+ Assert.Same(options.Clock, registeredOptions.Clock);
+ Assert.Equal(options.SizeLimit, registeredOptions.SizeLimit);
+ Assert.Equal(options.CompactionPercentage, registeredOptions.CompactionPercentage);
+ Assert.Equal(options.ExpirationScanFrequency, registeredOptions.ExpirationScanFrequency);
+ Assert.Equal(options.TrackLinkedCacheEntries, registeredOptions.TrackLinkedCacheEntries);
+ Assert.Equal(options.TrackStatistics, registeredOptions.TrackStatistics);
+ Assert.NotNull(provider.GetRequiredService());
+ Assert.Same(provider.GetRequiredService(), provider.GetRequiredService());
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_ShouldRegisterBehaviorAndNotificationHandlers()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ using var provider = services.BuildServiceProvider();
+ using var scope = provider.CreateScope();
+
+ Assert.Collection(scope.ServiceProvider.GetServices>(),
+ behavior => Assert.IsType>(behavior),
+ behavior => Assert.IsType>(behavior));
+ Assert.IsType(
+ scope.ServiceProvider.GetRequiredService>());
+ Assert.IsType(
+ scope.ServiceProvider.GetRequiredService>());
+ Assert.Collection(services.Where(descriptor => descriptor.ServiceType == typeof(IPipelineBehavior<,>)),
+ descriptor =>
+ {
+ Assert.Equal(typeof(InMemoryClearCacheByMatrixBehavior<,>), descriptor.ImplementationType);
+ Assert.Equal(ServiceLifetime.Transient, descriptor.Lifetime);
+ },
+ descriptor =>
+ {
+ Assert.Equal(typeof(InMemoryCacheBehavior<,>), descriptor.ImplementationType);
+ Assert.Equal(ServiceLifetime.Transient, descriptor.Lifetime);
+ });
+ Assert.Contains(services, descriptor =>
+ descriptor.ServiceType == typeof(INotificationHandler) &&
+ descriptor.Lifetime == ServiceLifetime.Scoped);
+ Assert.Contains(services, descriptor =>
+ descriptor.ServiceType == typeof(INotificationHandler) &&
+ descriptor.Lifetime == ServiceLifetime.Scoped);
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_NullAssemblies_ShouldScanLoadedAssemblies()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(assemblies: null);
+ using var provider = services.BuildServiceProvider();
+ var matrix = provider.GetRequiredService();
+
+ Assert.Contains(typeof(FirstCachedRequest), matrix.ClearCacheMatrixDictionary[typeof(TriggerRequest)]);
+ Assert.Contains(typeof(SecondCachedRequest), matrix.ClearCacheMatrixDictionary[typeof(TriggerRequest)]);
+ }
+
+ [Theory]
+ [InlineData(false, false)]
+ [InlineData(true, false)]
+ [InlineData(true, true)]
+ public async Task RegisteredPipeline_ShouldCacheQueriesAndInvalidateOnTriggers(bool cachedTrigger, bool cacheHit)
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediator(typeof(IMediator).Assembly);
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ var queryCalls = 0;
+ var triggerCalls = 0;
+ services.AddSingleton>(
+ new Handler((_, _) => ValueTask.FromResult($"response {++queryCalls}")));
+ services.AddSingleton>(
+ new Handler((_, _) =>
+ {
+ triggerCalls++;
+ return ValueTask.FromResult("updated");
+ }));
+ services.AddSingleton>(
+ new Handler((_, _) =>
+ {
+ triggerCalls++;
+ return ValueTask.FromResult("updated");
+ }));
+ using var provider = services.BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var query = new FirstCachedRequest();
+ if (cacheHit)
+ {
+ cache.Set(new CachedWithoutTriggers().GetCacheEntryKey(), "cached trigger");
+ }
+
+ Assert.Equal("response 1", await mediator.SendAsync(query, CancellationToken.None));
+ Assert.Equal("response 1", await mediator.SendAsync(query, CancellationToken.None));
+ Assert.Equal(1, queryCalls);
+
+ var result = cachedTrigger
+ ? await mediator.SendAsync(new CachedWithoutTriggers(), CancellationToken.None)
+ : await mediator.SendAsync(new UncachedRequest(), CancellationToken.None);
+
+ Assert.Equal(cacheHit ? "cached trigger" : "updated", result);
+ Assert.Equal(cacheHit ? 0 : 1, triggerCalls);
+ Assert.False(cache.TryGetValue(query.GetCacheEntryKey(), out _));
+ Assert.Equal("response 2", await mediator.SendAsync(query, CancellationToken.None));
+ Assert.Equal(2, queryCalls);
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_ExplicitAssembly_ShouldAggregateTriggersAndSkipAbstractTypes()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ using var provider = services.BuildServiceProvider();
+ var matrix = provider.GetRequiredService();
+
+ var targets = matrix.ClearCacheMatrixDictionary[typeof(TriggerRequest)];
+ Assert.Equal(2, targets.Length);
+ Assert.Contains(typeof(FirstCachedRequest), targets);
+ Assert.Contains(typeof(SecondCachedRequest), targets);
+ Assert.Equal(new[] { typeof(FirstCachedRequest) },
+ matrix.ClearCacheMatrixDictionary[typeof(OtherTriggerRequest)]);
+ Assert.DoesNotContain(typeof(AbstractCachedRequest), targets);
+ Assert.DoesNotContain(typeof(UncachedRequest), targets);
+ Assert.DoesNotContain(typeof(CachedWithoutTriggers), targets);
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_AssemblyWithoutTriggers_ShouldRegisterEmptyMatrix()
+ {
+ var services = new ServiceCollection();
+
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(CacheResponseInMemoryAttribute).Assembly]);
+ using var provider = services.BuildServiceProvider();
+
+ Assert.Empty(provider.GetRequiredService().ClearCacheMatrixDictionary);
+ }
+
+ [Fact]
+ public void AddMitMediatorInMemoryCache_ShouldRegisterMatrixAsSingletonPerProvider()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ using var firstProvider = services.BuildServiceProvider();
+ using var secondProvider = services.BuildServiceProvider();
+ using var scope = firstProvider.CreateScope();
+ var matrix = firstProvider.GetRequiredService();
+ var invalidation = firstProvider.GetRequiredService();
+
+ Assert.Same(matrix, scope.ServiceProvider.GetRequiredService());
+ Assert.NotSame(matrix, secondProvider.GetRequiredService());
+ Assert.Same(invalidation, scope.ServiceProvider.GetRequiredService());
+ Assert.NotSame(invalidation, secondProvider.GetRequiredService());
+ Assert.Contains(services, descriptor =>
+ descriptor.ServiceType == typeof(CacheInvalidationService) &&
+ descriptor.Lifetime == ServiceLifetime.Singleton);
+ Assert.Contains(services, descriptor =>
+ descriptor.ServiceType == typeof(ClearCacheMatrix) &&
+ descriptor.Lifetime == ServiceLifetime.Singleton);
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task RegisteredPipeline_AnotherContainer_ShouldNotChangeInvalidationRules(bool otherHasRules)
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediator(typeof(IMediator).Assembly);
+ services.AddMitMediatorInMemoryCache(null, assemblies: [typeof(DependencyInjectionTests).Assembly]);
+ services.AddSingleton>(
+ new Handler((_, _) => ValueTask.FromResult("query")));
+ services.AddSingleton>(
+ new Handler((_, _) => ValueTask.FromResult("updated")));
+ using var provider = services.BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var query = new FirstCachedRequest();
+ await mediator.SendAsync(query, CancellationToken.None);
+ Assert.True(cache.TryGetValue(query.GetCacheEntryKey(), out _));
+
+ Assembly otherAssembly = typeof(CacheResponseInMemoryAttribute).Assembly;
+ Type? otherQuery = null;
+ if (otherHasRules)
+ {
+ var assembly = AssemblyBuilder.DefineDynamicAssembly(
+ new AssemblyName($"OtherCacheRules_{Guid.NewGuid():N}"), AssemblyBuilderAccess.RunAndCollect);
+ var type = assembly.DefineDynamicModule("Rules").DefineType("OtherCachedRequest", TypeAttributes.Public);
+ type.AddInterfaceImplementation(typeof(IRequest));
+ var constructor = typeof(CacheResponseInMemoryAttribute).GetConstructor([typeof(Type[])])!;
+ type.SetCustomAttribute(new CustomAttributeBuilder(constructor, [new[] { typeof(UncachedRequest) }]));
+ otherQuery = type.CreateType()!;
+ otherAssembly = assembly;
+ }
+
+ var otherServices = new ServiceCollection();
+ otherServices.AddMitMediatorInMemoryCache(null, assemblies: [otherAssembly]);
+ using var otherProvider = otherServices.BuildServiceProvider();
+ var otherMatrix = otherProvider.GetRequiredService().ClearCacheMatrixDictionary;
+ if (otherHasRules)
+ {
+ Assert.Equal(new[] { otherQuery! }, otherMatrix[typeof(UncachedRequest)]);
+ }
+ else
+ {
+ Assert.Empty(otherMatrix);
+ }
+
+ await mediator.SendAsync(new UncachedRequest(), CancellationToken.None);
+
+ Assert.False(cache.TryGetValue(query.GetCacheEntryKey(), out _));
+ Assert.Contains(typeof(FirstCachedRequest),
+ provider.GetRequiredService().ClearCacheMatrixDictionary[typeof(UncachedRequest)]);
+ }
+
+ private sealed class TestClock : Microsoft.Extensions.Internal.ISystemClock
+ {
+ public DateTimeOffset UtcNow => DateTimeOffset.UnixEpoch;
+ }
+
+ private class TriggerRequest : IRequest;
+ private class OtherTriggerRequest : IRequest;
+ private class UncachedRequest : IRequest;
+
+ [CacheResponseInMemory]
+ private class CachedWithoutTriggers : IRequest;
+
+ [CacheResponseInMemory(typeof(TriggerRequest), typeof(OtherTriggerRequest),
+ typeof(UncachedRequest), typeof(CachedWithoutTriggers))]
+ private class FirstCachedRequest : IRequest;
+
+ [CacheResponseInMemory(typeof(TriggerRequest))]
+ private class SecondCachedRequest : IRequest;
+
+ [CacheResponseInMemory(typeof(TriggerRequest))]
+ private abstract class AbstractCachedRequest : IRequest;
+
+ private sealed class Handler(Func> invoke)
+ : IRequestHandler
+ where TRequest : IRequest
+ {
+ public ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) =>
+ invoke(request, cancellationToken);
+ }
}
\ No newline at end of file
diff --git a/tests/MitMediator.InMemoryCache.Tests/GenericInvalidationTests.cs b/tests/MitMediator.InMemoryCache.Tests/GenericInvalidationTests.cs
new file mode 100644
index 0000000..2445080
--- /dev/null
+++ b/tests/MitMediator.InMemoryCache.Tests/GenericInvalidationTests.cs
@@ -0,0 +1,249 @@
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace MitMediator.InMemoryCache.Tests;
+
+public class GenericInvalidationTests
+{
+ private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5);
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task Trigger_ShouldInvalidateDirectEntriesOfUnusedClosedVariants(bool usedVariant)
+ {
+ using var provider = CreateServices().BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var first = new Query { Id = 1 };
+ var second = new Query { Id = 2 };
+ var text = new Query();
+ var nested = new Query>();
+ var unrelated = new OtherQuery();
+ if (usedVariant)
+ {
+ await mediator.SendAsync, string>(first, default);
+ }
+ cache.Set(first.GetCacheEntryKey(), "first");
+ cache.Set(second.GetCacheEntryKey(), "second");
+ cache.Set(text.GetCacheEntryKey(), "text");
+ cache.Set(nested.GetCacheEntryKey(), "nested");
+ cache.Set(unrelated.GetCacheEntryKey(), "keep");
+ cache.Set(42, "non-string key");
+
+ await mediator.SendAsync(new Update(), default);
+
+ Assert.False(cache.TryGetValue(first.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(second.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(text.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(nested.GetCacheEntryKey(), out _));
+ Assert.Equal("keep", cache.Get(unrelated.GetCacheEntryKey()));
+ Assert.Equal("non-string key", cache.Get(42));
+ }
+
+ [Fact]
+ public async Task Trigger_WithDirectGenericEntry_ShouldWaitForItsWriteStripe()
+ {
+ using var provider = CreateServices().BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var request = new Query();
+ var key = request.GetCacheEntryKey();
+ cache.Set(key, "old");
+ var gate = provider.GetRequiredService>>().ForRequest(key);
+ await gate.WaitAsync();
+ var invalidation = mediator.SendAsync(new Update(), default).AsTask();
+
+ try
+ {
+ Assert.False(invalidation.IsCompleted);
+ }
+ finally
+ {
+ gate.Release();
+ await invalidation.WaitAsync(Timeout);
+ }
+
+ Assert.False(cache.TryGetValue(key, out _));
+ }
+
+ [Fact]
+ public async Task Trigger_BeforeAnyClosedVariantIsUsed_ShouldSucceed()
+ {
+ using var provider = CreateServices().BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+
+ Assert.Equal("updated", await mediator.SendAsync(new Update(), default));
+ Assert.Equal("int", await mediator.SendAsync, string>(new Query(), default));
+ }
+
+ [Fact]
+ public async Task Trigger_ShouldInvalidateAllClosedVariantsAndParameterValuesAcrossScopes()
+ {
+ var services = CreateServices();
+ var calls = 0;
+ services.AddSingleton, string>>(
+ new Handler>((_, _) => ValueTask.FromResult($"int {++calls}")));
+ using var provider = services.BuildServiceProvider();
+ using var firstScope = provider.CreateScope();
+ using var secondScope = provider.CreateScope();
+ var firstMediator = firstScope.ServiceProvider.GetRequiredService();
+ var secondMediator = secondScope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var first = new Query { Id = 1 };
+ var second = new Query { Id = 2 };
+ var text = new Query();
+ var unrelated = new OtherQuery();
+
+ Assert.Equal("int 1", await firstMediator.SendAsync, string>(first, default));
+ Assert.Equal("int 1", await secondMediator.SendAsync, string>(first, default));
+ Assert.Equal("int 2", await secondMediator.SendAsync, string>(second, default));
+ Assert.Equal("string", await firstMediator.SendAsync, string>(text, default));
+ Assert.Equal("other", await firstMediator.SendAsync, string>(unrelated, default));
+ Assert.True(cache.TryGetValue(text.GetCacheEntryKey(), out _));
+
+ Assert.Equal("updated", await secondMediator.SendAsync(new Update(), default));
+
+ Assert.False(cache.TryGetValue(first.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(second.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(text.GetCacheEntryKey(), out _));
+ Assert.Equal("other", cache.Get(unrelated.GetCacheEntryKey()));
+ Assert.Equal("int 3", await firstMediator.SendAsync, string>(first, default));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task ExplicitInvalidation_ShouldPreserveOtherClosedVariants(bool singleEntry)
+ {
+ using var provider = CreateServices().BuildServiceProvider();
+ using var scope = provider.CreateScope();
+ var mediator = scope.ServiceProvider.GetRequiredService();
+ var cache = provider.GetRequiredService();
+ var first = new Query { Id = 1 };
+ var second = new Query { Id = 2 };
+ var text = new Query();
+ await mediator.SendAsync, string>(first, default);
+ await mediator.SendAsync, string>(second, default);
+ await mediator.SendAsync, string>(text, default);
+
+ if (singleEntry)
+ {
+ await mediator.ClearResponseCacheAsync(first, default);
+ }
+ else
+ {
+ await mediator.ClearAllResponseCacheAsync>(default);
+ }
+
+ Assert.False(cache.TryGetValue(first.GetCacheEntryKey(), out _));
+ Assert.Equal(singleEntry, cache.TryGetValue(second.GetCacheEntryKey(), out _));
+ Assert.Equal("string", cache.Get(text.GetCacheEntryKey()));
+ }
+
+ [Fact]
+ public async Task Trigger_ShouldWaitForInFlightWritesOfAllClosedVariants()
+ {
+ var services = CreateServices();
+ var intStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var stringStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ services.AddSingleton, string>>(
+ new Handler>(async (_, _) =>
+ {
+ intStarted.SetResult();
+ await release.Task.WaitAsync(Timeout);
+ return "old int";
+ }));
+ services.AddSingleton, string>>(
+ new Handler>(async (_, _) =>
+ {
+ stringStarted.SetResult();
+ await release.Task.WaitAsync(Timeout);
+ return "old string";
+ }));
+ using var provider = services.BuildServiceProvider();
+ using var queryScope = provider.CreateScope();
+ using var updateScope = provider.CreateScope();
+ var queryMediator = queryScope.ServiceProvider.GetRequiredService();
+ var updateMediator = updateScope.ServiceProvider.GetRequiredService();
+ var first = new Query();
+ var second = new Query();
+ var intQuery = queryMediator.SendAsync, string>(first, default).AsTask();
+ var stringQuery = queryMediator.SendAsync, string>(second, default).AsTask();
+ await Task.WhenAll(intStarted.Task, stringStarted.Task).WaitAsync(Timeout);
+ var update = updateMediator.SendAsync(new Update(), default).AsTask();
+
+ try
+ {
+ Assert.False(update.IsCompleted);
+ }
+ finally
+ {
+ release.TrySetResult();
+ await Task.WhenAll(intQuery, stringQuery, update).WaitAsync(Timeout);
+ }
+
+ Assert.Equal("updated", await update);
+ var cache = provider.GetRequiredService();
+ Assert.False(cache.TryGetValue(first.GetCacheEntryKey(), out _));
+ Assert.False(cache.TryGetValue(second.GetCacheEntryKey(), out _));
+ }
+
+ [Fact]
+ public async Task Trigger_ShouldNotInvalidateAnotherProvidersClosedVariants()
+ {
+ var services = CreateServices();
+ using var firstProvider = services.BuildServiceProvider();
+ using var secondProvider = services.BuildServiceProvider();
+ using var firstScope = firstProvider.CreateScope();
+ using var secondScope = secondProvider.CreateScope();
+ var firstMediator = firstScope.ServiceProvider.GetRequiredService();
+ var secondMediator = secondScope.ServiceProvider.GetRequiredService();
+ var query = new Query();
+ await firstMediator.SendAsync, string>(query, default);
+ await secondMediator.SendAsync, string>(query, default);
+
+ await secondMediator.SendAsync(new Update(), default);
+
+ Assert.Equal("int", firstProvider.GetRequiredService().Get(query.GetCacheEntryKey()));
+ Assert.False(secondProvider.GetRequiredService().TryGetValue(query.GetCacheEntryKey(), out _));
+ }
+
+ private static ServiceCollection CreateServices()
+ {
+ var services = new ServiceCollection();
+ services.AddMitMediator(typeof(IMediator).Assembly);
+ services.AddMitMediatorInMemoryCache(assemblies: [typeof(GenericInvalidationTests).Assembly]);
+ services.AddSingleton, string>>(
+ new Handler>((_, _) => ValueTask.FromResult("int")));
+ services.AddSingleton, string>>(
+ new Handler>((_, _) => ValueTask.FromResult("string")));
+ services.AddSingleton, string>>(
+ new Handler>((_, _) => ValueTask.FromResult("other")));
+ services.AddSingleton>(
+ new Handler((_, _) => ValueTask.FromResult("updated")));
+ return services;
+ }
+
+ [CacheResponseInMemory(typeof(Update))]
+ private sealed class Query : IRequest
+ {
+ public int Id { get; init; }
+ }
+
+ [CacheResponseInMemory]
+ private sealed class OtherQuery : IRequest;
+
+ private sealed class Update : IRequest;
+
+ private sealed class Handler(Func> invoke)
+ : IRequestHandler where TRequest : IRequest
+ {
+ public ValueTask HandleAsync(TRequest request, CancellationToken cancellationToken) =>
+ invoke(request, cancellationToken);
+ }
+}
diff --git a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorConcurrencyTests.cs b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorConcurrencyTests.cs
new file mode 100644
index 0000000..fa52b3a
--- /dev/null
+++ b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorConcurrencyTests.cs
@@ -0,0 +1,219 @@
+using Microsoft.Extensions.Caching.Memory;
+
+namespace MitMediator.InMemoryCache.Tests;
+
+public class InMemoryCacheBehaviorConcurrencyTests
+{
+ private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5);
+
+ [Fact]
+ public async Task HandleAsync_DifferentKeysOnSameStripe_ShouldSerializeAndKeepSeparateEntries()
+ {
+ using var cache = new MemoryCache(new MemoryCacheOptions());
+ using var services = new TestCacheServices(cache, gatesCount: 1);
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var secondStarted = false;
+ var firstRequest = new Query { Id = 1 };
+ var secondRequest = new Query { Id = 2 };
+ var first = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation)
+ .HandleAsync(firstRequest, new Next(async (_, _) =>
+ {
+ started.SetResult();
+ await release.Task.WaitAsync(Timeout);
+ return "first";
+ }), default).AsTask();
+ await started.Task.WaitAsync(Timeout);
+ var second = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation)
+ .HandleAsync(secondRequest, new Next((_, _) =>
+ {
+ secondStarted = true;
+ return ValueTask.FromResult("second");
+ }), default).AsTask();
+
+ try
+ {
+ Assert.False(secondStarted);
+ Assert.False(second.IsCompleted);
+ }
+ finally
+ {
+ release.TrySetResult();
+ await Task.WhenAll(first, second).WaitAsync(Timeout);
+ }
+
+ Assert.Equal("first", cache.Get(firstRequest.GetCacheEntryKey()));
+ Assert.Equal("second", cache.Get(secondRequest.GetCacheEntryKey()));
+ }
+
+ [Fact]
+ public async Task HandleAsync_ConcurrentMisses_ShouldExecuteHandlerOnceAcrossBehaviorInstances()
+ {
+ using var cache = new MemoryCache(new MemoryCacheOptions());
+ using var services = new TestCacheServices(cache);
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var calls = 0;
+ var next = new Next(async (_, _) =>
+ {
+ Interlocked.Increment(ref calls);
+ await release.Task.WaitAsync(Timeout);
+ return "response";
+ });
+ var request = new Query();
+ var tasks = Enumerable.Range(0, 10)
+ .Select(_ => new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation)
+ .HandleAsync(request, next, CancellationToken.None).AsTask())
+ .ToArray();
+
+ release.SetResult();
+ var responses = await Task.WhenAll(tasks).WaitAsync(Timeout);
+
+ Assert.All(responses, response => Assert.Equal("response", response));
+ Assert.Equal(1, calls);
+ }
+
+ [Fact]
+ public async Task HandleAsync_DifferentKeys_ShouldNotBlockEachOther()
+ {
+ using var cache = new MemoryCache(new MemoryCacheOptions());
+ using var services = new TestCacheServices(cache);
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var firstRequest = new Query { Id = 1 };
+ var first = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation).HandleAsync(
+ firstRequest, new Next(async (_, _) =>
+ {
+ await release.Task.WaitAsync(Timeout);
+ return "first";
+ }), CancellationToken.None).AsTask();
+
+ try
+ {
+ var request = Enumerable.Range(2, 1000).Select(id => new Query { Id = id })
+ .First(candidate => services.Locks().ForRequest(candidate.GetCacheEntryKey()) !=
+ services.Locks().ForRequest(firstRequest.GetCacheEntryKey()));
+ var second = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation).HandleAsync(
+ request, new Next((_, _) => ValueTask.FromResult("second")), CancellationToken.None);
+
+ Assert.Equal("second", await second.AsTask().WaitAsync(Timeout));
+ }
+ finally
+ {
+ release.TrySetResult();
+ await first.WaitAsync(Timeout);
+ }
+ }
+
+ [Fact]
+ public async Task HandleAsync_NullResult_ShouldKeepSameLockWhileWaitersRemain()
+ {
+ using var cache = new MemoryCache(new MemoryCacheOptions());
+ using var services = new TestCacheServices(cache);
+ var releaseFirst = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var releaseSecond = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var secondStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var calls = 0;
+ var next = new Next(async (_, _) =>
+ {
+ var call = Interlocked.Increment(ref calls);
+ if (call == 1)
+ {
+ await releaseFirst.Task.WaitAsync(Timeout);
+ return null!;
+ }
+
+ if (call == 2)
+ {
+ secondStarted.SetResult();
+ await releaseSecond.Task.WaitAsync(Timeout);
+ }
+
+ return "response";
+ });
+ var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation);
+ var request = new Query();
+ var first = behavior.HandleAsync(request, next, CancellationToken.None).AsTask();
+ var second = behavior.HandleAsync(request, next, CancellationToken.None).AsTask();
+ Task? third = null;
+
+ try
+ {
+ releaseFirst.SetResult();
+ Assert.Null(await first.WaitAsync(Timeout));
+ await secondStarted.Task.WaitAsync(Timeout);
+
+ third = behavior.HandleAsync(request, next, CancellationToken.None).AsTask();
+ Assert.Equal(2, Volatile.Read(ref calls));
+
+ releaseSecond.SetResult();
+ Assert.Equal("response", await second.WaitAsync(Timeout));
+ Assert.Equal("response", await third.WaitAsync(Timeout));
+ Assert.Equal(2, calls);
+ }
+ finally
+ {
+ releaseFirst.TrySetResult();
+ releaseSecond.TrySetResult();
+ await Task.WhenAll(first, second, third ?? Task.FromResult("")).WaitAsync(Timeout);
+ }
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task HandleAsync_CanceledWaiter_ShouldNotExecuteOrReleaseActiveLock(bool alreadyCanceled)
+ {
+ using var cache = new MemoryCache(new MemoryCacheOptions());
+ using var services = new TestCacheServices(cache);
+ using var source = new CancellationTokenSource();
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var calls = 0;
+ var next = new Next(async (_, _) =>
+ {
+ Interlocked.Increment(ref calls);
+ await release.Task.WaitAsync(Timeout);
+ return "response";
+ });
+ var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation);
+ var request = new Query();
+ var first = behavior.HandleAsync(request, next, CancellationToken.None).AsTask();
+ Task? third = null;
+
+ try
+ {
+ if (alreadyCanceled)
+ {
+ source.Cancel();
+ }
+
+ var canceled = behavior.HandleAsync(request, next, source.Token).AsTask();
+ source.Cancel();
+ await Assert.ThrowsAnyAsync(() => canceled.WaitAsync(Timeout));
+
+ third = behavior.HandleAsync(request, next, CancellationToken.None).AsTask();
+ Assert.Equal(1, Volatile.Read(ref calls));
+
+ release.SetResult();
+ Assert.Equal("response", await first.WaitAsync(Timeout));
+ Assert.Equal("response", await third.WaitAsync(Timeout));
+ Assert.Equal(1, calls);
+ }
+ finally
+ {
+ release.TrySetResult();
+ await Task.WhenAll(first, third ?? Task.FromResult("")).WaitAsync(Timeout);
+ }
+ }
+
+ [CacheResponseInMemory]
+ private sealed class Query : IRequest
+ {
+ public int Id { get; init; }
+ }
+
+ private sealed class Next(Func> invoke)
+ : IRequestHandlerNext
+ {
+ public ValueTask InvokeAsync(Query request, CancellationToken cancellationToken) =>
+ invoke(request, cancellationToken);
+ }
+}
diff --git a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorEdgeCaseTests.cs b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorEdgeCaseTests.cs
new file mode 100644
index 0000000..1f9abdc
--- /dev/null
+++ b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorEdgeCaseTests.cs
@@ -0,0 +1,411 @@
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.Internal;
+using MitMediator.InMemoryCache.Notifications;
+
+namespace MitMediator.InMemoryCache.Tests;
+
+public class InMemoryCacheBehaviorEdgeCaseTests : IDisposable
+{
+ private readonly MemoryCache _cache = new(new MemoryCacheOptions());
+ private readonly TestCacheServices _services;
+ private ClearCacheMatrix _matrix = new(new Dictionary());
+
+ public InMemoryCacheBehaviorEdgeCaseTests() => _services = new TestCacheServices(_cache);
+
+ public void Dispose()
+ {
+ _services.Dispose();
+ _cache.Dispose();
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task HandleAsync_Trigger_ShouldInvalidateOnCacheMissAndHit(bool cacheHit)
+ {
+ _matrix = new ClearCacheMatrix(new Dictionary
+ {
+ [typeof(CachedTrigger)] = [typeof(CachedQuery), typeof(ExpiringQuery)]
+ });
+ var first = new CachedQuery { Id = 1 };
+ var second = new CachedQuery { Id = 2 };
+ var expiring = new ExpiringQuery();
+ var trigger = new CachedTrigger();
+ _cache.Set(first.GetCacheEntryKey(), "first");
+ _cache.Set(second.GetCacheEntryKey(), "second");
+ _cache.Set(expiring.GetCacheEntryKey(), "expiring");
+ _cache.Set("unrelated", "keep");
+ _cache.Set(42, "non-string key");
+ if (cacheHit)
+ {
+ _cache.Set(trigger.GetCacheEntryKey(), "cached trigger");
+ }
+ var calls = 0;
+ var next = new Next((_, _) =>
+ {
+ calls++;
+ return ValueTask.FromResult("handler result");
+ });
+
+ var result = await HandleWithInvalidation(trigger, next, CancellationToken.None);
+
+ Assert.Equal(cacheHit ? "cached trigger" : "handler result", result);
+ Assert.Equal(cacheHit ? 0 : 1, calls);
+ Assert.False(_cache.TryGetValue(first.GetCacheEntryKey(), out _));
+ Assert.False(_cache.TryGetValue(second.GetCacheEntryKey(), out _));
+ Assert.False(_cache.TryGetValue(expiring.GetCacheEntryKey(), out _));
+ Assert.Equal("keep", _cache.Get("unrelated"));
+ Assert.Equal("non-string key", _cache.Get(42));
+ Assert.True(_cache.TryGetValue(trigger.GetCacheEntryKey(), out _));
+ }
+
+ [Fact]
+ public async Task HandleAsync_DistinctRequestValues_ShouldHaveIndependentEntries()
+ {
+ var behavior = new InMemoryCacheBehavior(_cache, _services.Locks(), _services.Invalidation);
+ var calls = 0;
+ var next = new Next((request, _) =>
+ {
+ calls++;
+ return ValueTask.FromResult($"response {request.Id}");
+ });
+
+ Assert.Equal("response 1", await behavior.HandleAsync(new CachedQuery { Id = 1 }, next, CancellationToken.None));
+ Assert.Equal("response 2", await behavior.HandleAsync(new CachedQuery { Id = 2 }, next, CancellationToken.None));
+ Assert.Equal("response 1", await behavior.HandleAsync(new CachedQuery { Id = 1 }, next, CancellationToken.None));
+ Assert.Equal(2, calls);
+ }
+
+ [Fact]
+ public async Task HandleAsync_AbsoluteExpiration_ShouldNotBeExtendedByCacheHits()
+ {
+ var clock = new TestClock();
+ using var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock });
+ using var services = new TestCacheServices(cache);
+ var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation);
+ var request = new ExpiringQuery();
+ var calls = 0;
+ var next = new Next((_, _) => ValueTask.FromResult($"response {++calls}"));
+
+ Assert.Equal("response 1", await behavior.HandleAsync(request, next, CancellationToken.None));
+ clock.UtcNow += TimeSpan.FromSeconds(9);
+ Assert.Equal("response 1", await behavior.HandleAsync(request, next, CancellationToken.None));
+ clock.UtcNow += TimeSpan.FromSeconds(1);
+ Assert.Equal("response 2", await behavior.HandleAsync(request, next, CancellationToken.None));
+ Assert.Equal(2, calls);
+ }
+
+ [Fact]
+ public async Task HandleAsync_NoExpiration_ShouldRetainEntryAfterTimeAdvances()
+ {
+ var clock = new TestClock();
+ using var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock });
+ using var services = new TestCacheServices(cache);
+ var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation);
+ var request = new CachedQuery();
+ var calls = 0;
+ var next = new Next((_, _) => ValueTask.FromResult($"response {++calls}"));
+
+ Assert.Equal("response 1", await behavior.HandleAsync(request, next, CancellationToken.None));
+ clock.UtcNow += TimeSpan.FromDays(365);
+ Assert.Equal("response 1", await behavior.HandleAsync(request, next, CancellationToken.None));
+ Assert.Equal(1, calls);
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task HandleAsync_DownstreamException_ShouldNotCacheOrInvalidate(bool cancellation)
+ {
+ _matrix = new ClearCacheMatrix(new Dictionary
+ {
+ [typeof(CachedTrigger)] = [typeof(CachedQuery)]
+ });
+ var query = new CachedQuery();
+ _cache.Set(query.GetCacheEntryKey(), "keep");
+ var request = new CachedTrigger();
+ using var source = new CancellationTokenSource();
+ Exception expected = cancellation
+ ? new OperationCanceledException(source.Token)
+ : new InvalidOperationException("handler failed");
+ var calls = 0;
+ var next = new Next((actualRequest, token) =>
+ {
+ Assert.Same(request, actualRequest);
+ Assert.Equal(source.Token, token);
+ calls++;
+ return ValueTask.FromException(expected);
+ });
+ for (var i = 0; i < 2; i++)
+ {
+ var exception = await Assert.ThrowsAnyAsync(
+ () => HandleWithInvalidation(request, next, source.Token).AsTask());
+ Assert.Same(expected, exception);
+ }
+
+ Assert.Equal(2, calls);
+ Assert.False(_cache.TryGetValue(request.GetCacheEntryKey(), out _));
+ Assert.Equal("keep", _cache.Get(query.GetCacheEntryKey()));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task HandleAsync_UncachedTriggerException_ShouldNotInvalidate(bool cancellation)
+ {
+ _matrix = new ClearCacheMatrix(new Dictionary
+ {
+ [typeof(UncachedTrigger)] = [typeof(CachedQuery)]
+ });
+ var key = new CachedQuery().GetCacheEntryKey();
+ _cache.Set(key, "keep");
+ Exception expected = cancellation
+ ? new OperationCanceledException()
+ : new InvalidOperationException("handler failed");
+ var next = new Next((_, _) => ValueTask.FromException(expected));
+
+ var exception = await Assert.ThrowsAnyAsync(() =>
+ HandleWithInvalidation(new UncachedTrigger(), next, CancellationToken.None).AsTask());
+
+ Assert.Same(expected, exception);
+ Assert.Equal("keep", _cache.Get(key));
+ }
+
+ [Fact]
+ public async Task HandleAsync_CanceledCacheMiss_ShouldNotExecuteOrInvalidateAndAllowRetry()
+ {
+ _matrix = new ClearCacheMatrix(new Dictionary
+ {
+ [typeof(CachedTrigger)] = [typeof(CachedQuery)]
+ });
+ var key = new CachedQuery().GetCacheEntryKey();
+ _cache.Set(key, "keep");
+ using var source = new CancellationTokenSource();
+ source.Cancel();
+ var calls = 0;
+ var next = new Next((_, _) =>
+ {
+ calls++;
+ return ValueTask.FromResult("response");
+ });
+ var request = new CachedTrigger();
+
+ await Assert.ThrowsAnyAsync(() =>
+ HandleWithInvalidation(request, next, source.Token).AsTask());
+
+ Assert.Equal(0, calls);
+ Assert.Equal("keep", _cache.Get(key));
+ Assert.Equal("response", await HandleWithInvalidation(request, next, CancellationToken.None));
+ Assert.Equal(1, calls);
+ Assert.False(_cache.TryGetValue(key, out _));
+ }
+
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task HandleAsync_Trigger_ShouldInvalidateOnlyAfterSuccessfulHandler(bool cachedTrigger)
+ {
+ var triggerType = cachedTrigger ? typeof(CachedTrigger) : typeof(UncachedTrigger);
+ _matrix = new ClearCacheMatrix(new Dictionary