From 9b94979c8b99aa2f763915e59eadc42352cec32b Mon Sep 17 00:00:00 2001 From: Dzmitry Pratsko Date: Fri, 23 Oct 2026 18:28:43 +0300 Subject: [PATCH] v10.0.0-alfa --- .github/workflows/dotnet.yml | 72 ++- .github/workflows/release-abstractions.yml | 41 ++ ...{release.yml => release-inmemorycache.yml} | 30 +- Directory.Packages.props | 18 + MitMediator.InMemoryCache.sln | 70 --- MitMediator.InMemoryCache.slnx | 23 + README.md | 163 ++++--- .../Books.Application.csproj | 6 +- .../Books.Application/DependencyInjection.cs | 3 +- .../Queries/GetAuthor/GetAuthorQuery.cs | 3 +- .../GetAuthorsByFilterQuery.cs | 3 +- .../Books/Queries/GetBook/GetBookQuery.cs | 3 +- .../GetBooksByFilter/GetBooksByFilterQuery.cs | 5 +- .../Queries/GetGenres/GetGenresQuery.cs | 2 +- .../Books.Domain/Books.Domain.csproj | 2 +- .../Books.Infrastructure.csproj | 6 +- .../Books.WebApi/Books.WebApi.csproj | 4 +- samples/BooksWebApi/Books.WebApi/Program.cs | 2 +- samples/SimpleConsoleApp/Program.cs | 7 +- .../SimpleConsoleApp/SimpleConsoleApp.csproj | 2 +- .../CacheResponseInMemoryAttribute.cs} | 8 +- ...Mediator.InMemoryCache.Abstractions.csproj | 34 ++ .../CacheEntryKeyUtill.cs | 12 - .../CacheInvalidationService.cs | 59 +++ .../CacheRequestTypeLocksStriping.cs | 76 +++ .../CacheRequestTypeLocksStripingOptions.cs | 6 + .../ClearCacheMatrix.cs | 7 +- .../DependencyInjection.cs | 38 +- .../InMemoryCacheBehavior.cs | 100 ++-- .../InMemoryClearCacheByMatrixBehavior.cs | 24 + .../MediatorExtensions.cs | 2 +- .../MitMediator.InMemoryCache.csproj | 18 +- .../ClearAllResponsesCacheNotification.cs | 13 +- ...learResponseCacheForRequestNotification.cs | 9 +- .../ObjectGetCacheEntryKeyExtensions.cs | 17 + .../SemaphoreSlimWithUsers.cs | 51 +++ .../AttributesAndDI_Tests.cs | 69 ++- .../ClearCacheMatrixTests.cs | 26 +- .../DependencyInjectionTests.cs | 433 +++++++++++++++++- .../GenericInvalidationTests.cs | 249 ++++++++++ .../InMemoryCacheBehaviorConcurrencyTests.cs | 219 +++++++++ .../InMemoryCacheBehaviorEdgeCaseTests.cs | 411 +++++++++++++++++ .../InMemoryCacheBehaviorSizeTests.cs | 19 +- .../InMemoryCacheBehaviorTests.cs | 35 +- .../MitMediator.InMemoryCache.Tests.csproj | 12 +- .../NotificationsTests.cs | 30 +- .../ObjectGetCacheEntryKeyExtensionsTests.cs | 24 +- .../StripedInvalidationTests.cs | 345 ++++++++++++++ .../TestCacheServices.cs | 27 ++ 49 files changed, 2441 insertions(+), 397 deletions(-) create mode 100644 .github/workflows/release-abstractions.yml rename .github/workflows/{release.yml => release-inmemorycache.yml} (52%) create mode 100644 Directory.Packages.props delete mode 100644 MitMediator.InMemoryCache.sln create mode 100644 MitMediator.InMemoryCache.slnx rename src/{MitMediator.InMemoryCache/CacheResponseAttribute.cs => MitMediator.InMemoryCache.Abstractions/CacheResponseInMemoryAttribute.cs} (89%) create mode 100644 src/MitMediator.InMemoryCache.Abstractions/MitMediator.InMemoryCache.Abstractions.csproj delete mode 100644 src/MitMediator.InMemoryCache/CacheEntryKeyUtill.cs create mode 100644 src/MitMediator.InMemoryCache/CacheInvalidationService.cs create mode 100644 src/MitMediator.InMemoryCache/CacheRequestTypeLocksStriping.cs create mode 100644 src/MitMediator.InMemoryCache/CacheRequestTypeLocksStripingOptions.cs create mode 100644 src/MitMediator.InMemoryCache/InMemoryClearCacheByMatrixBehavior.cs create mode 100644 src/MitMediator.InMemoryCache/ObjectGetCacheEntryKeyExtensions.cs create mode 100644 src/MitMediator.InMemoryCache/SemaphoreSlimWithUsers.cs create mode 100644 tests/MitMediator.InMemoryCache.Tests/GenericInvalidationTests.cs create mode 100644 tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorConcurrencyTests.cs create mode 100644 tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorEdgeCaseTests.cs create mode 100644 tests/MitMediator.InMemoryCache.Tests/StripedInvalidationTests.cs create mode 100644 tests/MitMediator.InMemoryCache.Tests/TestCacheServices.cs 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) [![Build and Test](https://github.com/dzmprt/MitMediator.InMemoryCache/actions/workflows/dotnet.yml/badge.svg)](https://github.com/dzmprt/MitMediator.InMemoryCache/actions/workflows/dotnet.yml) ![NuGet](https://img.shields.io/nuget/v/MitMediator.InMemoryCache) -![.NET 9.0](https://img.shields.io/badge/Version-.NET%209.0-informational?style=flat&logo=dotnet) -![NuGet Downloads](https://img.shields.io/nuget/dt/MitMediator.InMemoryCache) +![.NET 10.0](https://img.shields.io/badge/.NET-10.0-informational?style=flat&logo=dotnet) ![License](https://img.shields.io/github/license/dzmprt/MitMediator.InMemoryCache) -## 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 + { + [triggerType] = [typeof(CachedQuery)] + }); + var query = new CachedQuery(); + var key = query.GetCacheEntryKey(); + _cache.Set(key, "before"); + + ValueTask Handle() + { + Assert.Equal("before", _cache.Get(key)); + _cache.Set(key, "read during update"); + return ValueTask.FromResult("updated"); + } + + if (cachedTrigger) + { + await HandleWithInvalidation( + new CachedTrigger(), new Next((_, _) => Handle()), CancellationToken.None); + } + else + { + await HandleWithInvalidation( + new UncachedTrigger(), new Next((_, _) => Handle()), CancellationToken.None); + } + + Assert.False(_cache.TryGetValue(key, out _)); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task HandleAsync_CacheBehaviorAlone_ShouldNotInvalidate(bool cacheHit) + { + _matrix = new ClearCacheMatrix(new Dictionary + { + [typeof(CachedTrigger)] = [typeof(CachedQuery)] + }); + var key = new CachedQuery().GetCacheEntryKey(); + _cache.Set(key, "keep"); + var trigger = new CachedTrigger(); + if (cacheHit) + { + _cache.Set(trigger.GetCacheEntryKey(), "cached"); + } + + var result = await new InMemoryCacheBehavior(_cache, _services.Locks(), _services.Invalidation).HandleAsync( + trigger, new Next((_, _) => ValueTask.FromResult("handled")), + CancellationToken.None); + + Assert.Equal(cacheHit ? "cached" : "handled", result); + Assert.Equal("keep", _cache.Get(key)); + } + + [Fact] + public async Task HandleAsync_NullTriggerResult_ShouldInvalidateWithoutCachingNull() + { + _matrix = new ClearCacheMatrix(new Dictionary + { + [typeof(CachedTrigger)] = [typeof(CachedQuery)] + }); + var key = new CachedQuery().GetCacheEntryKey(); + _cache.Set(key, "stale"); + var trigger = new CachedTrigger(); + var calls = 0; + var next = new Next((_, _) => + { + calls++; + return ValueTask.FromResult(null!); + }); + + Assert.Null(await HandleWithInvalidation(trigger, next, CancellationToken.None)); + Assert.False(_cache.TryGetValue(key, out _)); + Assert.False(_cache.TryGetValue(trigger.GetCacheEntryKey(), out _)); + Assert.Null(await HandleWithInvalidation(trigger, next, CancellationToken.None)); + Assert.Equal(2, calls); + } + + [Theory] + [InlineData(0)] + [InlineData(3)] + public async Task HandleAsync_Collection_ShouldMultiplyCustomEntrySize(int count) + { + using var cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 100, TrackStatistics = true }); + using var services = new TestCacheServices(cache); + var response = Enumerable.Range(0, count).ToArray(); + var request = new CollectionQuery(); + var next = new Next((_, _) => ValueTask.FromResult(response)); + + var result = await new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation) + .HandleAsync(request, next, CancellationToken.None); + + Assert.Same(response, result); + Assert.Same(response, cache.Get(request.GetCacheEntryKey())); + Assert.Equal(count * 4, cache.GetCurrentStatistics()!.CurrentEstimatedSize); + } + + [Fact] + public async Task HandleAsync_EntryExceedingSizeLimit_ShouldNotBeCached() + { + using var cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 3 }); + using var services = new TestCacheServices(cache); + var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation); + var request = new CollectionQuery(); + var response = new[] { 1 }; + var calls = 0; + var next = new Next((_, _) => + { + calls++; + return ValueTask.FromResult(response); + }); + + Assert.Same(response, await behavior.HandleAsync(request, next, CancellationToken.None)); + Assert.Same(response, await behavior.HandleAsync(request, next, CancellationToken.None)); + Assert.False(cache.TryGetValue(request.GetCacheEntryKey(), out _)); + Assert.Equal(2, calls); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task TypeInvalidation_ShouldPreserveOtherTypesWithSimilarNames(bool explicitInvalidation) + { + _matrix = new ClearCacheMatrix(new Dictionary + { + [typeof(UncachedTrigger)] = [typeof(FirstGroup.Query)] + }); + var key = new FirstGroup.Query().GetCacheEntryKey(); + var similarKey = new FirstGroup.QueryDetails().GetCacheEntryKey(); + var otherKey = new SecondGroup.Query().GetCacheEntryKey(); + _cache.Set(key, "remove"); + _cache.Set(similarKey, "similar name"); + _cache.Set(otherKey, "other type"); + + if (explicitInvalidation) + { + await new ClearAllResponsesCacheNotificationHandler(_services.Invalidation).HandleAsync( + new ClearAllResponsesCacheNotification(typeof(FirstGroup.Query)), CancellationToken.None); + } + else + { + await HandleWithInvalidation(new UncachedTrigger(), + new Next((_, _) => ValueTask.FromResult("updated")), CancellationToken.None); + } + + Assert.False(_cache.TryGetValue(key, out _)); + Assert.Equal("similar name", _cache.Get(similarKey)); + Assert.Equal("other type", _cache.Get(otherKey)); + } + + private static class FirstGroup + { + public sealed class Query : IRequest; + public sealed class QueryDetails : IRequest; + } + + private static class SecondGroup + { + public sealed class Query : IRequest; + } + + private ValueTask HandleWithInvalidation( + TRequest request, IRequestHandlerNext next, CancellationToken cancellationToken) + where TRequest : IRequest + { + var cacheBehavior = new InMemoryCacheBehavior(_cache, _services.Locks(), _services.Invalidation); + var cacheNext = new Next((actualRequest, token) => + cacheBehavior.HandleAsync(actualRequest, next, token)); + + return new InMemoryClearCacheByMatrixBehavior(_services.Invalidation, _matrix) + .HandleAsync(request, cacheNext, cancellationToken); + } + + [CacheResponseInMemory] + private class CachedQuery : IRequest + { + public int Id { get; init; } + } + + [CacheResponseInMemory] + private class CachedTrigger : IRequest; + + private class UncachedTrigger : IRequest; + + [CacheResponseInMemory(10)] + private class ExpiringQuery : IRequest; + + [CacheResponseInMemory(30, 4)] + private class CollectionQuery : IRequest; + + private sealed class TestClock : ISystemClock + { + public DateTimeOffset UtcNow { get; set; } = DateTimeOffset.UnixEpoch; + } + + private sealed class Next( + Func> invoke) : IRequestHandlerNext + where TRequest : IRequest + { + public ValueTask InvokeAsync(TRequest request, CancellationToken cancellationToken) => + invoke(request, cancellationToken); + } +} diff --git a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorSizeTests.cs b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorSizeTests.cs index cb0b6c1..b73822a 100644 --- a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorSizeTests.cs +++ b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorSizeTests.cs @@ -8,14 +8,23 @@ namespace MitMediator.InMemoryCache.Tests; -public class InMemoryCacheBehaviorSizeTests +public class InMemoryCacheBehaviorSizeTests : IDisposable { private readonly MemoryCache _memoryCache = new(new MemoryCacheOptions { SizeLimit = 1000, TrackStatistics = true}); + private readonly TestCacheServices _services; + + public InMemoryCacheBehaviorSizeTests() => _services = new TestCacheServices(_memoryCache); + + public void Dispose() + { + _services.Dispose(); + _memoryCache.Dispose(); + } [Fact] public async Task CacheEntry_Size_Is_One_For_Single_Response() { - var behavior = new InMemoryCacheBehavior(_memoryCache); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new SingleRequest(); var next = new SingleHandlerNext(); await behavior.HandleAsync(request, next, CancellationToken.None); @@ -26,7 +35,7 @@ public async Task CacheEntry_Size_Is_One_For_Single_Response() [Fact] public async Task CacheEntry_Size_Equals_Collection_Count() { - var behavior = new InMemoryCacheBehavior>(_memoryCache); + var behavior = new InMemoryCacheBehavior>(_memoryCache, _services.Locks(), _services.Invalidation); var request = new CollectionRequest(); var next = new CollectionHandlerNext(); @@ -36,10 +45,10 @@ public async Task CacheEntry_Size_Equals_Collection_Count() } - [CacheResponse] + [CacheResponseInMemory] private class SingleRequest : IRequest { } - [CacheResponse] + [CacheResponseInMemory] private class CollectionRequest : IRequest> { } private class SingleHandlerNext : IRequestHandlerNext { diff --git a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorTests.cs b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorTests.cs index a0d6ca5..b803e45 100644 --- a/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorTests.cs +++ b/tests/MitMediator.InMemoryCache.Tests/InMemoryCacheBehaviorTests.cs @@ -2,14 +2,23 @@ namespace MitMediator.InMemoryCache.Tests; -public class InMemoryCacheBehaviorTests +public class InMemoryCacheBehaviorTests : IDisposable { private readonly MemoryCache _memoryCache = new(new MemoryCacheOptions()); + private readonly TestCacheServices _services; + + public InMemoryCacheBehaviorTests() => _services = new TestCacheServices(_memoryCache); + + public void Dispose() + { + _services.Dispose(); + _memoryCache.Dispose(); + } [Fact] public async Task HandleAsync_ShouldCacheResult_WhenCacheForeverAttributeIsPresent() { - var behavior = new InMemoryCacheBehavior(_memoryCache); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new TestRequest { Value = "test" }; var nextCalled = 0; var next = new TestRequestHandlerNext(() => @@ -29,7 +38,7 @@ public async Task HandleAsync_ShouldCacheResult_WhenCacheForeverAttributeIsPrese [Fact] public async Task HandleAsync_ShouldNotCache_WhenNoCacheAttribute() { - var behavior = new InMemoryCacheBehavior(_memoryCache); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new NoCacheRequest { Value = "test" }; var nextCalled = 0; var next = new TestRequestHandlerNext(() => @@ -49,7 +58,7 @@ public async Task HandleAsync_ShouldNotCache_WhenNoCacheAttribute() [Fact] public async Task HandleAsync_ShouldCacheForSeconds() { - var behavior = new InMemoryCacheBehavior(_memoryCache); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new CacheForSecondsRequest { Value = "test" }; var nextCalled = 0; var next = new TestRequestHandlerNext(() => @@ -70,11 +79,11 @@ public async Task HandleAsync_ShouldCacheForSeconds() public async Task HandleAsync_ShouldCacheAndClearCacheByMatrix() { var type = typeof(CachedRequest); - ClearCacheMatrix.ClearCacheMatrixDictionary = new Dictionary + var matrix = new ClearCacheMatrix(new Dictionary { { typeof(ClearCacheRequest), [type] } - }; - var behavior = new InMemoryCacheBehavior(_memoryCache); + }); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new CachedRequest { TestData = "test" @@ -83,17 +92,17 @@ public async Task HandleAsync_ShouldCacheAndClearCacheByMatrix() await behavior.HandleAsync(request, next, CancellationToken.None); - var key = $"{type.Name}_" + System.Text.Json.JsonSerializer.Serialize(request); + var key = request.GetCacheEntryKey(); Assert.True(_memoryCache.TryGetValue(key, out _)); var clearCacheRequest = new ClearCacheRequest(); - var behaviorForClear = new InMemoryCacheBehavior(_memoryCache); + var behaviorForClear = new InMemoryClearCacheByMatrixBehavior(_services.Invalidation, matrix); await behaviorForClear.HandleAsync(clearCacheRequest, next, CancellationToken.None); Assert.False(_memoryCache.TryGetValue(key, out _)); } - [CacheResponse(typeof(ClearCacheRequest))] + [CacheResponseInMemory(typeof(ClearCacheRequest))] internal class CachedRequest : IRequest { public string TestData { get; set; } @@ -101,7 +110,7 @@ internal class CachedRequest : IRequest internal class ClearCacheRequest : IRequest; - [CacheResponse] + [CacheResponseInMemory] internal class TestRequest : IRequest { public string Value { get; set; } = string.Empty; @@ -112,7 +121,7 @@ internal class NoCacheRequest : IRequest public string Value { get; set; } = string.Empty; } - [CacheResponse(10)] + [CacheResponseInMemory(10)] internal class CacheForSecondsRequest : IRequest { public string Value { get; set; } = string.Empty; @@ -121,7 +130,7 @@ internal class CacheForSecondsRequest : IRequest [Fact] public async Task HandleAsync_ShouldNotCacheNullResult() { - var behavior = new InMemoryCacheBehavior(_memoryCache); + var behavior = new InMemoryCacheBehavior(_memoryCache, _services.Locks(), _services.Invalidation); var request = new TestRequest { Value = "test-null" }; int nextCalled = 0; var next = new TestRequestHandlerNext(() => { nextCalled++; return Task.FromResult(null); }); diff --git a/tests/MitMediator.InMemoryCache.Tests/MitMediator.InMemoryCache.Tests.csproj b/tests/MitMediator.InMemoryCache.Tests/MitMediator.InMemoryCache.Tests.csproj index 035fc0b..303c3e7 100644 --- a/tests/MitMediator.InMemoryCache.Tests/MitMediator.InMemoryCache.Tests.csproj +++ b/tests/MitMediator.InMemoryCache.Tests/MitMediator.InMemoryCache.Tests.csproj @@ -1,18 +1,18 @@  - net9.0 + net10.0 enable enable false - - - - - + + + + + diff --git a/tests/MitMediator.InMemoryCache.Tests/NotificationsTests.cs b/tests/MitMediator.InMemoryCache.Tests/NotificationsTests.cs index 3b70097..bae0d61 100644 --- a/tests/MitMediator.InMemoryCache.Tests/NotificationsTests.cs +++ b/tests/MitMediator.InMemoryCache.Tests/NotificationsTests.cs @@ -16,16 +16,25 @@ public void ClearAllResponsesCacheNotification_SetsRequestType() [Fact] public async Task ClearAllResponsesCacheNotificationHandler_RemovesAllMatchingKeys() { - var memoryCache = new MemoryCache(new MemoryCacheOptions()); - memoryCache.Set("String_key1", "value1"); - memoryCache.Set("String_key2", "value2"); - memoryCache.Set("Int32_key3", "value3"); - var handler = new ClearAllResponsesCacheNotificationHandler(memoryCache); + using var memoryCache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(memoryCache); + memoryCache.Set("Type:System.String_SerializedRequest:key1", "value1"); + memoryCache.Set("Type:System.String_SerializedRequest:key2", "value2"); + memoryCache.Set("Type:System.Int32_SerializedRequest:key3", "value3"); + memoryCache.Set("Type:System.StringBuilder_SerializedRequest:key4", "prefix match"); + memoryCache.Set("Type:Other.String_SerializedRequest:key5", "other namespace"); + memoryCache.Set("Type:system.string_SerializedRequest:key6", "different case"); + memoryCache.Set(42, "non-string key"); + var handler = new ClearAllResponsesCacheNotificationHandler(services.Invalidation); var notification = new ClearAllResponsesCacheNotification(typeof(string)); await handler.HandleAsync(notification, CancellationToken.None); - Assert.False(memoryCache.TryGetValue("String_key1", out _)); - Assert.False(memoryCache.TryGetValue("String_key2", out _)); - Assert.True(memoryCache.TryGetValue("Int32_key3", out _)); + Assert.False(memoryCache.TryGetValue("Type:System.String_SerializedRequest:key1", out _)); + Assert.False(memoryCache.TryGetValue("Type:System.String_SerializedRequest:key2", out _)); + Assert.True(memoryCache.TryGetValue("Type:System.Int32_SerializedRequest:key3", out _)); + Assert.Equal("prefix match", memoryCache.Get("Type:System.StringBuilder_SerializedRequest:key4")); + Assert.Equal("other namespace", memoryCache.Get("Type:Other.String_SerializedRequest:key5")); + Assert.Equal("different case", memoryCache.Get("Type:system.string_SerializedRequest:key6")); + Assert.Equal("non-string key", memoryCache.Get(42)); } [Fact] @@ -37,11 +46,12 @@ public void ClearResponseCacheForRequestNotification_ThrowsOnNull() [Fact] public async Task ClearResponseCacheForRequestHandler_RemovesKey() { - var memoryCache = new MemoryCache(new MemoryCacheOptions()); + using var memoryCache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(memoryCache); var request = "test-request"; var key = request.GetCacheEntryKey(); memoryCache.Set(key, "value"); - var handler = new ClearResponseCacheForRequestHandler(memoryCache); + var handler = new ClearResponseCacheForRequestHandler(services.Invalidation); var notification = new ClearResponseCacheForRequestNotification(request); await handler.HandleAsync(notification, CancellationToken.None); Assert.False(memoryCache.TryGetValue(key, out _)); diff --git a/tests/MitMediator.InMemoryCache.Tests/ObjectGetCacheEntryKeyExtensionsTests.cs b/tests/MitMediator.InMemoryCache.Tests/ObjectGetCacheEntryKeyExtensionsTests.cs index 6879984..4dc98a5 100644 --- a/tests/MitMediator.InMemoryCache.Tests/ObjectGetCacheEntryKeyExtensionsTests.cs +++ b/tests/MitMediator.InMemoryCache.Tests/ObjectGetCacheEntryKeyExtensionsTests.cs @@ -11,7 +11,29 @@ public void GetCacheEntryKey_ReturnsExpectedFormat() { var obj = new TestObj { Id = 1, Name = "Test", InnerObject = new InnerObject() { BoolValue = true } }; var key = obj.GetCacheEntryKey(); - Assert.Equal("TestObj_{\"Id\":1,\"Name\":\"Test\",\"InnerObject\":{\"BoolValue\":true}}", key); + Assert.Equal($"Type:{typeof(TestObj).FullName}_SerializedRequest:{{\"Id\":1,\"Name\":\"Test\",\"InnerObject\":{{\"BoolValue\":true}}}}", key); + } + + [Fact] + public void GetCacheEntryKey_SameSimpleName_ShouldIncludeDeclaringType() + { + var first = new FirstGroup.Query().GetCacheEntryKey(); + var second = new SecondGroup.Query().GetCacheEntryKey(); + + Assert.Equal(typeof(FirstGroup.Query).Name, typeof(SecondGroup.Query).Name); + Assert.Equal($"Type:{typeof(FirstGroup.Query).FullName}_SerializedRequest:{{}}", first); + Assert.Equal($"Type:{typeof(SecondGroup.Query).FullName}_SerializedRequest:{{}}", second); + Assert.NotEqual(first, second); + } + + private static class FirstGroup + { + public sealed class Query; + } + + private static class SecondGroup + { + public sealed class Query; } private class TestObj diff --git a/tests/MitMediator.InMemoryCache.Tests/StripedInvalidationTests.cs b/tests/MitMediator.InMemoryCache.Tests/StripedInvalidationTests.cs new file mode 100644 index 0000000..9c7d1f6 --- /dev/null +++ b/tests/MitMediator.InMemoryCache.Tests/StripedInvalidationTests.cs @@ -0,0 +1,345 @@ +using System.Reflection; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using MitMediator.InMemoryCache.Notifications; + +namespace MitMediator.InMemoryCache.Tests; + +public class StripedInvalidationTests +{ + private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(5); + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task DerivedRequest_Invalidation_ShouldWaitForBaseTypedWriter(bool singleEntry) + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache); + var request = new UnannotatedDerivedQuery(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var behavior = new InMemoryCacheBehavior( + cache, services.Locks(), services.Invalidation); + var writing = behavior.HandleAsync(request, new Next(async (_, _) => + { + started.SetResult(); + await release.Task.WaitAsync(Timeout); + return "old"; + }), default).AsTask(); + await started.Task.WaitAsync(Timeout); + var invalidation = singleEntry + ? new ClearResponseCacheForRequestHandler(services.Invalidation) + .HandleAsync(new ClearResponseCacheForRequestNotification(request), default).AsTask() + : new ClearAllResponsesCacheNotificationHandler(services.Invalidation) + .HandleAsync(new ClearAllResponsesCacheNotification(typeof(UnannotatedDerivedQuery)), default).AsTask(); + + try + { + Assert.False(invalidation.IsCompleted); + } + finally + { + release.TrySetResult(); + await Task.WhenAll(writing, invalidation).WaitAsync(Timeout); + } + + Assert.False(cache.TryGetValue(request.GetCacheEntryKey(), out _)); + } + + [Fact] + public async Task DerivedRequest_BaseAndDerivedTypedWriters_ShouldShareLocks() + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache); + var request = new DerivedQuery(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var calls = 0; + var baseBehavior = new InMemoryCacheBehavior( + cache, services.Locks(), services.Invalidation); + var derivedBehavior = new InMemoryCacheBehavior( + cache, services.Locks(), services.Invalidation); + var first = baseBehavior.HandleAsync(request, new Next(async (_, _) => + { + calls++; + started.SetResult(); + await release.Task.WaitAsync(Timeout); + return "first"; + }), default).AsTask(); + await started.Task.WaitAsync(Timeout); + var second = derivedBehavior.HandleAsync(request, new Next((_, _) => + { + calls++; + return ValueTask.FromResult("second"); + }), default).AsTask(); + + try + { + Assert.False(second.IsCompleted); + } + finally + { + release.TrySetResult(); + await Task.WhenAll(first, second).WaitAsync(Timeout); + } + + Assert.Equal("first", await second); + Assert.Equal(1, calls); + } + + [Fact] + public async Task CachedTrigger_ShouldReleaseWriteStripeBeforeInvalidatingItsOwnType() + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache, gatesCount: 1); + var request = new CachedUpdate(); + var matrix = new ClearCacheMatrix(new Dictionary + { + [typeof(CachedUpdate)] = [typeof(CachedUpdate)] + }); + var caching = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation); + var clearing = new InMemoryClearCacheByMatrixBehavior(services.Invalidation, matrix); + var result = await clearing.HandleAsync(request, new Next((actual, token) => + caching.HandleAsync(actual, + new Next((_, _) => ValueTask.FromResult("updated")), token)), default) + .AsTask().WaitAsync(Timeout); + + Assert.Equal("updated", result); + Assert.False(cache.TryGetValue(request.GetCacheEntryKey(), out _)); + Assert.Equal(1, services.Locks().ForRequest(request.GetCacheEntryKey()).CurrentCount); + } + + [Theory] + [InlineData(InvalidationKind.Trigger)] + [InlineData(InvalidationKind.CachedTrigger)] + [InlineData(InvalidationKind.Type)] + [InlineData(InvalidationKind.Entry)] + public async Task Invalidation_ShouldWaitForInFlightWriteThenRemoveIt(InvalidationKind kind) + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache); + var behavior = new InMemoryCacheBehavior(cache, services.Locks(), services.Invalidation); + var request = new Query { Id = 1 }; + var other = new Query { Id = 2 }; + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + cache.Set(other.GetCacheEntryKey(), "other"); + var first = behavior.HandleAsync(request, new Next(async (_, _) => + { + started.SetResult(); + await release.Task.WaitAsync(Timeout); + return "old"; + }), default).AsTask(); + await started.Task.WaitAsync(Timeout); + var invalidation = Invalidate(kind, services, cache, request); + + try + { + Assert.False(invalidation.IsCompleted); + } + finally + { + release.TrySetResult(); + await Task.WhenAll(first, invalidation).WaitAsync(Timeout); + } + + Assert.Equal("old", await first); + Assert.False(cache.TryGetValue(request.GetCacheEntryKey(), out _)); + Assert.Equal(kind == InvalidationKind.Entry, cache.TryGetValue(other.GetCacheEntryKey(), out _)); + Assert.Equal("new", await behavior.HandleAsync(request, + new Next((_, _) => ValueTask.FromResult("new")), default)); + Assert.Equal("new", cache.Get(request.GetCacheEntryKey())); + } + + [Fact] + public async Task TypeInvalidation_CanceledAfterPartialAcquisition_ShouldReleaseOnlyAcquiredGates() + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache, gatesCount: 3); + var locks = services.Locks(); + var field = typeof(CacheRequestTypeLocksStriping) + .GetField("_gates", BindingFlags.Instance | BindingFlags.NonPublic)!; + var gates = Assert.IsType(field.GetValue(locks)); + await gates[2].WaitAsync(); + using var source = new CancellationTokenSource(); + var invalidation = services.Invalidation.InvalidateAsync(typeof(Query), source.Token).AsTask(); + + try + { + Assert.All(gates, gate => Assert.Equal(0, gate.CurrentCount)); + source.Cancel(); + await Assert.ThrowsAnyAsync(() => invalidation.WaitAsync(Timeout)); + Assert.Equal(1, gates[0].CurrentCount); + Assert.Equal(1, gates[1].CurrentCount); + Assert.Equal(0, gates[2].CurrentCount); + } + finally + { + source.Cancel(); + gates[2].Release(); + } + + await services.Invalidation.InvalidateAsync(typeof(Query), default).AsTask().WaitAsync(Timeout); + Assert.All(gates, gate => Assert.Equal(1, gate.CurrentCount)); + } + + [Fact] + public async Task EntryInvalidation_CanceledWaiter_ShouldNotReleaseWriterGate() + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache); + var request = new Query(); + var key = request.GetCacheEntryKey(); + var gate = services.Locks().ForRequest(key); + await gate.WaitAsync(); + using var source = new CancellationTokenSource(); + var invalidation = services.Invalidation.InvalidateAsync(typeof(Query), source.Token, key).AsTask(); + + try + { + source.Cancel(); + await Assert.ThrowsAnyAsync(() => invalidation.WaitAsync(Timeout)); + Assert.Equal(0, gate.CurrentCount); + } + finally + { + gate.Release(); + } + } + + [Fact] + public async Task ConcurrentTypeInvalidations_ShouldCompleteWithoutDeadlock() + { + using var cache = new MemoryCache(new MemoryCacheOptions()); + using var services = new TestCacheServices(cache); + cache.Set(new Query().GetCacheEntryKey(), "old"); + + await Task.WhenAll(Enumerable.Range(0, 32).Select(_ => Task.Run(async () => + await services.Invalidation.InvalidateAsync(typeof(Query), default)))).WaitAsync(Timeout); + + Assert.False(cache.TryGetValue(new Query().GetCacheEntryKey(), out _)); + Assert.Equal(1, services.Locks().ForRequest(new Query().GetCacheEntryKey()).CurrentCount); + } + + [Fact] + public async Task RegisteredPipeline_ShouldShareLocksAcrossScopesAndWaitForWrites() + { + var services = new ServiceCollection(); + services.AddMitMediator(typeof(IMediator).Assembly); + services.AddMitMediatorInMemoryCache(assemblies: [typeof(StripedInvalidationTests).Assembly]); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var value = "old"; + var calls = 0; + services.AddSingleton>(new Handler(async (_, _) => + { + var snapshot = value; + if (++calls == 1) + { + started.SetResult(); + await release.Task.WaitAsync(Timeout); + } + return snapshot; + })); + services.AddSingleton>(new Handler((_, _) => + { + value = "new"; + return ValueTask.FromResult("updated"); + })); + 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 request = new Query(); + var first = firstMediator.SendAsync(request, default).AsTask(); + await started.Task.WaitAsync(Timeout); + var update = secondMediator.SendAsync(new Update(), default).AsTask(); + + try + { + Assert.False(update.IsCompleted); + } + finally + { + release.TrySetResult(); + await Task.WhenAll(first, update).WaitAsync(Timeout); + } + + Assert.Equal("old", await first); + Assert.Equal("new", await secondMediator.SendAsync(request, default)); + Assert.Equal("new", await firstMediator.SendAsync(request, default)); + Assert.Equal(2, calls); + } + + private static Task Invalidate( + InvalidationKind kind, TestCacheServices services, MemoryCache cache, Query request) + { + var matrix = new ClearCacheMatrix(new Dictionary + { + [typeof(Update)] = [typeof(Query)], + [typeof(CachedUpdate)] = [typeof(Query)] + }); + switch (kind) + { + case InvalidationKind.Trigger: + return new InMemoryClearCacheByMatrixBehavior(services.Invalidation, matrix) + .HandleAsync(new Update(), new Next((_, _) => ValueTask.FromResult("updated")), default) + .AsTask(); + case InvalidationKind.CachedTrigger: + var trigger = new CachedUpdate(); + cache.Set(trigger.GetCacheEntryKey(), "cached update"); + var cacheBehavior = new InMemoryCacheBehavior( + cache, services.Locks(), services.Invalidation); + return new InMemoryClearCacheByMatrixBehavior(services.Invalidation, matrix) + .HandleAsync(trigger, new Next((actual, token) => + cacheBehavior.HandleAsync(actual, + new Next((_, _) => throw new InvalidOperationException("Expected cache hit")), + token)), default).AsTask(); + case InvalidationKind.Type: + return new ClearAllResponsesCacheNotificationHandler(services.Invalidation) + .HandleAsync(new ClearAllResponsesCacheNotification(typeof(Query)), default).AsTask(); + case InvalidationKind.Entry: + return new ClearResponseCacheForRequestHandler(services.Invalidation) + .HandleAsync(new ClearResponseCacheForRequestNotification(request), default).AsTask(); + default: + throw new ArgumentOutOfRangeException(nameof(kind)); + } + } + + public enum InvalidationKind { Trigger, CachedTrigger, Type, Entry } + + [CacheResponseInMemory] + private class BaseQuery : IRequest; + + private sealed class UnannotatedDerivedQuery : BaseQuery; + + [CacheResponseInMemory] + private sealed class DerivedQuery : BaseQuery; + + [CacheResponseInMemory(typeof(Update), typeof(CachedUpdate))] + private sealed class Query : IRequest + { + public int Id { get; init; } + } + + private sealed class Update : IRequest; + + [CacheResponseInMemory] + private sealed class CachedUpdate : IRequest; + + private sealed class Next(Func> invoke) + : IRequestHandlerNext where TRequest : IRequest + { + public ValueTask InvokeAsync(TRequest request, CancellationToken cancellationToken) => + invoke(request, cancellationToken); + } + + 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/TestCacheServices.cs b/tests/MitMediator.InMemoryCache.Tests/TestCacheServices.cs new file mode 100644 index 0000000..370377f --- /dev/null +++ b/tests/MitMediator.InMemoryCache.Tests/TestCacheServices.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; + +namespace MitMediator.InMemoryCache.Tests; + +internal sealed class TestCacheServices : IDisposable +{ + private readonly ServiceProvider _provider; + + public TestCacheServices(MemoryCache cache, int gatesCount = 64) + { + var services = new ServiceCollection(); + services.AddSingleton(cache); + services.AddOptions() + .Configure(options => options.LockStripingGatesCount = gatesCount); + services.AddSingleton(typeof(CacheRequestTypeLocksStriping<>), typeof(CacheRequestTypeLocksStriping<>)); + services.AddSingleton(); + _provider = services.BuildServiceProvider(); + } + + public CacheRequestTypeLocksStriping Locks() => + _provider.GetRequiredService>(); + + public CacheInvalidationService Invalidation => _provider.GetRequiredService(); + + public void Dispose() => _provider.Dispose(); +}