generated from dailydevops/template-dotnet
-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Distributed cache interceptor for queries (ICacheableQuery<TResponse>)
#152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a27b3a
feat: add distributed cache interceptor for queries (ICacheableQuery)
Copilot 1764b00
docs: update README files with distributed query caching feature
Copilot ba0fd52
feat: add QueryCachingOptions with configurable JsonSerializerOptions…
Copilot ff62f91
chore: Refactor cache entry options creation into helper method
samtrion fa777b0
feat: add DefaultExpiry fallback to QueryCachingOptions
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| namespace NetEvolve.Pulse; | ||
| namespace NetEvolve.Pulse; | ||
|
|
||
| using System.Text.Json; | ||
| using Dapr.Client; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| namespace NetEvolve.Pulse; | ||
| namespace NetEvolve.Pulse; | ||
|
|
||
| using NetEvolve.Pulse.Extensibility; | ||
|
|
||
|
|
||
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.EntityFramework/Configurations/MySqlOutboxMessageConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.EntityFramework/EntityFrameworkEventOutbox.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.EntityFramework/EntityFrameworkMediatorConfiguratorExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.EntityFramework/EntityFrameworkOutboxRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.EntityFramework/EntityFrameworkOutboxTransactionScope.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace NetEvolve.Pulse.Extensibility; | ||
|
|
||
| /// <summary> | ||
| /// Specifies how cached entries created by the distributed cache interceptor should expire. | ||
| /// </summary> | ||
| /// <seealso cref="QueryCachingOptions"/> | ||
| /// <seealso cref="ICacheableQuery{TResponse}"/> | ||
| public enum CacheExpirationMode | ||
| { | ||
| /// <summary> | ||
| /// The cache entry expires at an absolute point in time calculated as | ||
| /// the current instant plus <see cref="ICacheableQuery{TResponse}.Expiry"/>. | ||
| /// This is the default mode. | ||
| /// </summary> | ||
| Absolute = 0, | ||
|
|
||
| /// <summary> | ||
| /// The cache entry expiry window is reset each time the entry is accessed. | ||
| /// The entry expires after <see cref="ICacheableQuery{TResponse}.Expiry"/> elapses | ||
| /// since the last access. | ||
| /// </summary> | ||
| Sliding = 1, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace NetEvolve.Pulse.Extensibility; | ||
|
|
||
| /// <summary> | ||
| /// Represents a query that supports distributed caching of its response. | ||
| /// Queries implementing this interface are eligible for transparent caching via | ||
| /// <c>DistributedCacheQueryInterceptor</c>. | ||
| /// </summary> | ||
| /// <typeparam name="TResponse">The type of data returned by the query.</typeparam> | ||
| /// <remarks> | ||
| /// <para><strong>Opt-In Caching:</strong></para> | ||
| /// Caching is opt-in per query type. Only queries that implement <see cref="ICacheableQuery{TResponse}"/> | ||
| /// will be cached by the interceptor. Queries that do not implement this interface always reach the handler. | ||
| /// <para><strong>Cache Key:</strong></para> | ||
| /// The <see cref="CacheKey"/> property uniquely identifies the cached entry. Ensure it is deterministic | ||
| /// and unique per logical query result (e.g., include query parameters in the key). | ||
| /// <para><strong>Expiry:</strong></para> | ||
| /// When <see cref="Expiry"/> is <see langword="null"/>, the entry is stored without an explicit expiry, | ||
| /// relying on the cache's default eviction policy. Provide a <see cref="TimeSpan"/> value to set an | ||
| /// absolute expiration relative to the time of caching. | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code> | ||
| /// public record GetCustomerByIdQuery(string CustomerId) | ||
| /// : ICacheableQuery<CustomerDetailsDto> | ||
| /// { | ||
| /// public string? CorrelationId { get; set; } | ||
| /// public string CacheKey => $"customer:{CustomerId}"; | ||
| /// public TimeSpan? Expiry => TimeSpan.FromMinutes(5); | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| /// <seealso cref="IQuery{TResponse}"/> | ||
| public interface ICacheableQuery<TResponse> : IQuery<TResponse> | ||
| { | ||
| /// <summary> | ||
| /// Gets the key used to store and retrieve the response from the distributed cache. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The key must be unique for each distinct query result. Include relevant query parameters | ||
| /// to avoid cache collisions between different query instances. | ||
| /// </remarks> | ||
| string CacheKey { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the duration after which the cached entry expires, or <see langword="null"/> to use the cache's default. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When <see langword="null"/>, the entry is stored without an explicit absolute expiration. | ||
| /// When a <see cref="TimeSpan"/> value is provided, it is used as the absolute expiration | ||
| /// relative to the time of caching. | ||
| /// </remarks> | ||
| TimeSpan? Expiry { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| namespace NetEvolve.Pulse.Extensibility; | ||
|
|
||
| using System.Text.Json; | ||
|
|
||
| /// <summary> | ||
| /// Configuration options for the distributed query caching interceptor. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Configure these options when calling <see cref="IMediatorConfigurator.AddQueryCaching"/>. | ||
| /// </remarks> | ||
| /// <seealso cref="IMediatorConfigurator.AddQueryCaching"/> | ||
| /// <seealso cref="ICacheableQuery{TResponse}"/> | ||
| public sealed class QueryCachingOptions | ||
samtrion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Gets or sets the <see cref="System.Text.Json.JsonSerializerOptions"/> used when | ||
| /// serializing responses to the cache and deserializing them back. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Defaults to <see cref="JsonSerializerOptions.Default"/>. | ||
| /// Provide custom options when your response types require non-default converters, | ||
| /// naming policies, or other serialization settings. | ||
| /// </remarks> | ||
| public JsonSerializerOptions JsonSerializerOptions { get; set; } = JsonSerializerOptions.Default; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets how the <see cref="ICacheableQuery{TResponse}.Expiry"/> value is interpreted | ||
| /// when storing entries in the distributed cache. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <list type="bullet"> | ||
| /// <item> | ||
| /// <description> | ||
| /// <see cref="CacheExpirationMode.Absolute"/> (default) — sets | ||
| /// <c>DistributedCacheEntryOptions.AbsoluteExpirationRelativeToNow</c>. | ||
| /// </description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <description> | ||
| /// <see cref="CacheExpirationMode.Sliding"/> — sets | ||
| /// <c>DistributedCacheEntryOptions.SlidingExpiration</c>, resetting the | ||
| /// expiry window on each cache access. | ||
| /// </description> | ||
| /// </item> | ||
| /// </list> | ||
| /// When <see cref="ICacheableQuery{TResponse}.Expiry"/> is <see langword="null"/>, | ||
| /// <see cref="DefaultExpiry"/> is used if set; otherwise no expiry is applied and the | ||
| /// cache's default eviction policy is used. | ||
| /// </remarks> | ||
| public CacheExpirationMode ExpirationMode { get; set; } = CacheExpirationMode.Absolute; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the fallback expiry duration used when | ||
| /// <see cref="ICacheableQuery{TResponse}.Expiry"/> returns <see langword="null"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When <see langword="null"/> (default), entries whose query returns a <see langword="null"/> | ||
| /// <see cref="ICacheableQuery{TResponse}.Expiry"/> are stored without an explicit expiration, | ||
| /// relying on the cache's default eviction policy. | ||
| /// When a <see cref="TimeSpan"/> value is provided, it is applied according to | ||
| /// <see cref="ExpirationMode"/> in the same way as a per-query expiry would be. | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code> | ||
| /// services.AddPulse(config => config.AddQueryCaching(options => | ||
| /// { | ||
| /// // All queries without an explicit Expiry get a 10-minute TTL | ||
| /// options.DefaultExpiry = TimeSpan.FromMinutes(10); | ||
| /// })); | ||
| /// </code> | ||
| /// </example> | ||
| public TimeSpan? DefaultExpiry { get; set; } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/NetEvolve.Pulse.Polly/Interceptors/PollyEventInterceptor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.