Inherit default values for EntityCacheOption.Enabled and EntityCacheOption.Level from RuntimeCacheOptions#3155
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request implements inheritance of default values for entity-level cache configuration from runtime-level cache configuration. When entities don't explicitly configure cache.enabled or cache.level, they now inherit these values from the global runtime cache settings, addressing issue #3149.
Changes:
- Entity cache enabled state inherits from runtime cache enabled state when not explicitly configured
- Entity cache level inherits from runtime cache Level2 configuration (L1L2 if Level2 enabled, otherwise L1) when not explicitly configured
- Removed exceptions from
GetEntityCacheEntryTtlandGetEntityCacheEntryLevelmethods when caching is disabled, as these methods can now be safely called to retrieve inherited defaults
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Service.Tests/Caching/CachingConfigProcessingTests.cs | Added comprehensive test coverage for entity cache inheritance behavior from runtime cache settings |
| src/Core/Resolvers/SqlQueryEngine.cs | Updated to use new IsEntityCachingEnabled() method instead of entity property for inheritance-aware caching checks |
| src/Config/ObjectModel/RuntimeConfig.cs | Added IsEntityCachingEnabled() and GlobalCacheEntryLevel() methods; updated GetEntityCacheEntryTtl/Level() to support inheritance |
| src/Config/ObjectModel/RuntimeCacheOptions.cs | Added InferredLevel property to compute cache level from Level2 configuration |
| src/Config/ObjectModel/EntityCacheOptions.cs | Added UserProvidedEnabledOptions flag and updated constructor to track explicit vs inherited enabled state |
JerryNixon
left a comment
There was a problem hiding this comment.
Same as the other PR: If the old Entity.IsCachingEnabled incorporated additional logic (for example: entity absent cache block, entity defaults, or other gating like unsupported entity types), this change could enable caching where it previously wouldn’t, or vice-versa.
RubenCerna2079
left a comment
There was a problem hiding this comment.
LGTM! Just need to resolve comments by copilot.
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
| [JsonConstructor] | ||
| public EntityCacheOptions(bool? Enabled = null, int? TtlSeconds = null, EntityCacheLevel? Level = null) | ||
| { | ||
| // TODO: shouldn't we apply the same "UserProvidedXyz" logic to Enabled, too? |
There was a problem hiding this comment.
Dont we need the "UserProvidedxyz" logic to Enabled?
There was a problem hiding this comment.
I don't think so, because in the case that it is not user provided the value will be null, that let's us distinguish between if its user set or not.
| { | ||
| return Runtime?.Cache is not null | ||
| ? Runtime.Cache.InferredLevel | ||
| : EntityCacheOptions.DEFAULT_LEVEL; |
There was a problem hiding this comment.
The Default cache level when level 2 Cache section is NOT specified in runtime is L1 (as is evident from the InferredCacheLevel). But for EntityCacheOptions.DEFAULT_LEVEL it seems to be L1L2. Is that accurate and what we want?
There was a problem hiding this comment.
This is misleading because for us to get to the default level here, caching would be turned off at the runtime level. I will update so it isn't confusing.
| if (reader.TokenType is JsonTokenType.StartObject) | ||
| { | ||
| bool? enabled = false; | ||
| // Default to null (unset) so that an empty cache object ("cache": {}) |
There was a problem hiding this comment.
Fix PR description. The following should be deleted - its remnant from the template.
Sample Request(s)
Example REST and/or GraphQL request to demonstrate modifications
Example of CLI usage to demonstrate modifications
| /// <param name="entityName">Name of the entity to check cache configuration.</param> | ||
| /// <returns>Whether caching is enabled for the entity.</returns> | ||
| /// <exception cref="DataApiBuilderException">Raised when an invalid entity name is provided.</exception> | ||
| public virtual bool IsEntityCachingEnabled(string entityName) |
There was a problem hiding this comment.
What happened to the function IsCachingEnabled defined in Entity.cs? Are we deleting that? If not, where would that be called now?
If we are deleting, this function should be defined in Entity.cs itself or simply modify the existing IsCachingEnabled function. We do not need to introduce this new function.
| [DataRow(@",""cache"": { ""enabled"": true }", EntityCacheLevel.L1, DisplayName = "Global cache enabled, no Level2: inferred level is L1.")] | ||
| [DataRow(@",""cache"": { ""enabled"": true, ""level-2"": { ""enabled"": true } }", EntityCacheLevel.L1L2, DisplayName = "Global cache enabled, Level2 enabled: inferred level is L1L2.")] | ||
| [DataRow(@",""cache"": { ""enabled"": true, ""level-2"": { ""enabled"": false } }", EntityCacheLevel.L1, DisplayName = "Global cache enabled, Level2 disabled: inferred level is L1.")] | ||
| [DataRow(@"", EntityCacheLevel.L1L2, DisplayName = "No global cache: default level is L1L2.")] |
There was a problem hiding this comment.
default level when no global cache should be L1. Similar to the case above when Global cache is enabled but L2 is not specified, L1 is preferred.
| /// <param name="entityCacheConfig">Entity cache configuration JSON fragment.</param> | ||
| /// <param name="expectedLevel">Expected cache level returned by GetEntityCacheEntryLevel.</param> | ||
| [DataRow(@",""cache"": { ""enabled"": true, ""level-2"": { ""enabled"": true } }", @",""cache"": { ""enabled"": true }", EntityCacheLevel.L1L2, DisplayName = "Level2 enabled, entity has no level: inherits L1L2 from runtime.")] | ||
| [DataRow(@",""cache"": { ""enabled"": true }", @",""cache"": { ""enabled"": true }", EntityCacheLevel.L1, DisplayName = "No Level2, entity has no level: inherits L1 from runtime.")] |
There was a problem hiding this comment.
This DataRow
[DataRow(@",""cache"": { ""enabled"": true }", @",""cache"": { ""enabled"": true }"
has the same effect as the one in the above test:
[DataRow(@",""cache"": { ""enabled"": true }", EntityCacheLevel.L1, DisplayName = "Global cache enabled, no Level2: inferred level is L1.")]
Please go through each DataRow in this test and above to ensure we only have unique cases. The two test methods can be combined into a single method with multiple DataRows to check for level.
Aniruddh25
left a comment
There was a problem hiding this comment.
Fix the duplicate tests with similar DataRows.
Why make this change?
Closes #3149
What is this change?
Instead of having static default values for the
EntityCacheOptions.EnabledandEntityCacheOptions.Level, we inherit these default values from theRuntimeCacheOptions.How was this tested?
We add a test that validates the new behavior in
CachingConfigProcessingTestsSample Request(s)