Skip to content

Inherit default values for EntityCacheOption.Enabled and EntityCacheOption.Level from RuntimeCacheOptions#3155

Open
aaronburtle wants to merge 7 commits intomainfrom
dev/aaronburtle/InheritEntityCacheEnabldedAndLevelFromRuntimeSetting
Open

Inherit default values for EntityCacheOption.Enabled and EntityCacheOption.Level from RuntimeCacheOptions#3155
aaronburtle wants to merge 7 commits intomainfrom
dev/aaronburtle/InheritEntityCacheEnabldedAndLevelFromRuntimeSetting

Conversation

@aaronburtle
Copy link
Contributor

Why make this change?

Closes #3149

What is this change?

Instead of having static default values for the EntityCacheOptions.Enabled and EntityCacheOptions.Level, we inherit these default values from the RuntimeCacheOptions.

How was this tested?

We add a test that validates the new behavior in CachingConfigProcessingTests

Sample Request(s)

  • Example REST and/or GraphQL request to demonstrate modifications
  • Example of CLI usage to demonstrate modifications

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GetEntityCacheEntryTtl and GetEntityCacheEntryLevel methods 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

Copy link
Contributor

@JerryNixon JerryNixon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Copy link
Contributor

@RubenCerna2079 RubenCerna2079 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just need to resolve comments by copilot.

@aaronburtle
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

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?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont we need the "UserProvidedxyz" logic to Enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Collaborator

@Aniruddh25 Aniruddh25 Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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": {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Collaborator

@Aniruddh25 Aniruddh25 Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.")]
Copy link
Collaborator

@Aniruddh25 Aniruddh25 Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the duplicate tests with similar DataRows.

@Aniruddh25 Aniruddh25 added the 2.0 label Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change default for Entity.cache.enabled and Entity.cache.level to inherit the Runtime.Cache values.

5 participants