-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ions` (#2058) * EnableContentHashing not being considered from appsettings * Adding CacheOptionsCreator, Injected IRegionCreator as Singleton. Should still add some acceptance tests that are definitely missing! * Adding caching global configuration since we messed up, ignoring an important breaking change with EnableContentHashing set to false by default * Adding some further acceptance tests, validating EnableContentHashing, validating global config too. * removing some debug content * TtlSeconds must be set * updating documentation * Update docs/features/caching.rst Co-authored-by: Raman Maksimchuk <[email protected]> * Update docs/features/caching.rst Co-authored-by: Raman Maksimchuk <[email protected]> * Removing RegionCreator, moving service collection extension method to dependencyInjection\Features etc. * adding unit tests for FileCacheOptions * some more null tests... * slight refactoring, updating ICacheOptionsCreator signature * some more design refactoring * Update src/Ocelot/Configuration/Creator/CacheOptionsCreator.cs Co-authored-by: Raman Maksimchuk <[email protected]> * Code review by @raman-m * Rename `FileCacheOptions` -> `CacheOptions` * Subtly transition to `CacheOptions`, ensuring compatibility with `FileCacheOptions` to avoid a breaking change * Not obsolete --------- Co-authored-by: Guillaume Gnaegi <[email protected]> Co-authored-by: Raman Maksimchuk <[email protected]>
- Loading branch information
1 parent
aef3e6b
commit 6e9a975
Showing
24 changed files
with
490 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,42 @@ | ||
using Ocelot.Request.Middleware; | ||
|
||
namespace Ocelot.Configuration | ||
{ | ||
public class CacheOptions | ||
{ | ||
internal CacheOptions() { } | ||
namespace Ocelot.Configuration; | ||
|
||
public CacheOptions(int ttlSeconds, string region, string header) | ||
{ | ||
TtlSeconds = ttlSeconds; | ||
Region = region; | ||
Header = header; | ||
} | ||
|
||
public CacheOptions(int ttlSeconds, string region, string header, bool enableContentHashing) | ||
{ | ||
TtlSeconds = ttlSeconds; | ||
Region = region; | ||
Header = header; | ||
EnableContentHashing = enableContentHashing; | ||
} | ||
public class CacheOptions | ||
{ | ||
internal CacheOptions() { } | ||
|
||
public int TtlSeconds { get; } | ||
public string Region { get; } | ||
public string Header { get; } | ||
|
||
/// <summary> | ||
/// Enables MD5 hash calculation of the <see cref="HttpRequestMessage.Content"/> of the <see cref="DownstreamRequest.Request"/> object. | ||
/// </summary> | ||
/// <remarks> | ||
/// Default value is <see langword="false"/>. No hashing by default. | ||
/// </remarks> | ||
/// <value> | ||
/// <see langword="true"/> if hashing is enabled, otherwise it is <see langword="false"/>. | ||
/// </value> | ||
public bool EnableContentHashing { get; } | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CacheOptions"/> class. | ||
/// </summary> | ||
/// <remarks> | ||
/// Internal defaults: | ||
/// <list type="bullet"> | ||
/// <item>The default value for <see cref="EnableContentHashing"/> is <see langword="false"/>, but it is set to null for route-level configuration to allow global configuration usage.</item> | ||
/// <item>The default value for <see cref="TtlSeconds"/> is 0.</item> | ||
/// </list> | ||
/// </remarks> | ||
/// <param name="ttlSeconds">Time-to-live seconds. If not speciefied, zero value is used by default.</param> | ||
/// <param name="region">The region of caching.</param> | ||
/// <param name="header">The header name to control cached value.</param> | ||
/// <param name="enableContentHashing">The switcher for content hashing. If not speciefied, false value is used by default.</param> | ||
public CacheOptions(int? ttlSeconds, string region, string header, bool? enableContentHashing) | ||
{ | ||
TtlSeconds = ttlSeconds ?? 0; | ||
Region = region; | ||
Header = header; | ||
EnableContentHashing = enableContentHashing ?? false; | ||
} | ||
} | ||
|
||
/// <summary>Time-to-live seconds.</summary> | ||
/// <remarks>Default value is 0. No caching by default.</remarks> | ||
/// <value>An <see cref="int"/> value of seconds.</value> | ||
public int TtlSeconds { get; } | ||
public string Region { get; } | ||
public string Header { get; } | ||
|
||
/// <summary>Enables MD5 hash calculation of the <see cref="HttpRequestMessage.Content"/> of the <see cref="DownstreamRequest.Request"/> object.</summary> | ||
/// <remarks>Default value is <see langword="false"/>. No hashing by default.</remarks> | ||
/// <value><see langword="true"/> if hashing is enabled, otherwise it is <see langword="false"/>.</value> | ||
public bool EnableContentHashing { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator; | ||
|
||
public class CacheOptionsCreator : ICacheOptionsCreator | ||
{ | ||
public CacheOptions Create(FileCacheOptions options, FileGlobalConfiguration global, string upstreamPathTemplate, IList<string> upstreamHttpMethods) | ||
{ | ||
var region = GetRegion(options.Region ?? global?.CacheOptions.Region, upstreamPathTemplate, upstreamHttpMethods); | ||
var header = options.Header ?? global?.CacheOptions.Header; | ||
var ttlSeconds = options.TtlSeconds ?? global?.CacheOptions.TtlSeconds; | ||
var enableContentHashing = options.EnableContentHashing ?? global?.CacheOptions.EnableContentHashing; | ||
|
||
return new CacheOptions(ttlSeconds, region, header, enableContentHashing); | ||
} | ||
|
||
protected virtual string GetRegion(string region, string upstreamPathTemplate, IList<string> upstreamHttpMethod) | ||
{ | ||
if (!string.IsNullOrEmpty(region)) | ||
{ | ||
return region; | ||
} | ||
|
||
var methods = string.Join(string.Empty, upstreamHttpMethod); | ||
return $"{methods}{upstreamPathTemplate.Replace("/", string.Empty)}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator; | ||
|
||
/// <summary> | ||
/// This interface is used to create cache options. | ||
/// </summary> | ||
public interface ICacheOptionsCreator | ||
{ | ||
/// <summary> | ||
/// Creates cache options based on the file cache options, upstream path template and upstream HTTP methods.</summary> | ||
/// <remarks>Upstream path template and upstream HTTP methods are used to get the region name.</remarks> | ||
/// <param name="options">The file cache options.</param> | ||
/// <param name="global">The global configuration.</param> | ||
/// <param name="upstreamPathTemplate">The upstream path template as string.</param> | ||
/// <param name="upstreamHttpMethods">The upstream http methods as a list of strings.</param> | ||
/// <returns>The generated cache options.</returns> | ||
CacheOptions Create(FileCacheOptions options, FileGlobalConfiguration global, string upstreamPathTemplate, IList<string> upstreamHttpMethods); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
namespace Ocelot.Configuration.File | ||
{ | ||
public class FileCacheOptions | ||
{ | ||
public FileCacheOptions() | ||
{ | ||
Region = string.Empty; | ||
TtlSeconds = 0; | ||
} | ||
namespace Ocelot.Configuration.File; | ||
|
||
public FileCacheOptions(FileCacheOptions from) | ||
{ | ||
Region = from.Region; | ||
TtlSeconds = from.TtlSeconds; | ||
} | ||
public class FileCacheOptions | ||
{ | ||
public FileCacheOptions() { } | ||
|
||
public int TtlSeconds { get; set; } | ||
public string Region { get; set; } | ||
public string Header { get; set; } | ||
} | ||
public FileCacheOptions(FileCacheOptions from) | ||
{ | ||
Region = from.Region; | ||
TtlSeconds = from.TtlSeconds; | ||
Header = from.Header; | ||
EnableContentHashing = from.EnableContentHashing; | ||
} | ||
|
||
/// <summary>Using <see cref="Nullable{T}"/> where T is <see cref="int"/> to have <see langword="null"/> as default value and allowing global configuration usage.</summary> | ||
/// <remarks>If <see langword="null"/> then use global configuration with 0 by default.</remarks> | ||
/// <value>The time to live seconds, with 0 by default.</value> | ||
public int? TtlSeconds { get; set; } | ||
public string Region { get; set; } | ||
public string Header { get; set; } | ||
|
||
/// <summary>Using <see cref="Nullable{T}"/> where T is <see cref="bool"/> to have <see langword="null"/> as default value and allowing global configuration usage.</summary> | ||
/// <remarks>If <see langword="null"/> then use global configuration with <see langword="false"/> by default.</remarks> | ||
/// <value><see langword="true"/> if content hashing is enabled; otherwise, <see langword="false"/>.</value> | ||
public bool? EnableContentHashing { get; set; } | ||
} |
Oops, something went wrong.