@@ -577,7 +577,33 @@ public virtual EntityCacheLevel GetEntityCacheEntryLevel(string entityName)
577577 return entityConfig . Cache . Level . Value ;
578578 }
579579
580- return EntityCacheOptions . DEFAULT_LEVEL ;
580+ // GlobalCacheEntryLevel() returns null when runtime cache is not configured.
581+ // Default to L1 to match EntityCacheOptions.DEFAULT_LEVEL.
582+ return GlobalCacheEntryLevel ( ) ?? EntityCacheOptions . DEFAULT_LEVEL ;
583+ }
584+
585+ /// <summary>
586+ /// Returns the ttl-seconds value for the global cache entry.
587+ /// If no value is explicitly set, returns the global default value.
588+ /// </summary>
589+ /// <returns>Number of seconds a cache entry should be valid before cache eviction.</returns>
590+ public virtual int GlobalCacheEntryTtl ( )
591+ {
592+ return Runtime is not null && Runtime . IsCachingEnabled && Runtime . Cache . UserProvidedTtlOptions
593+ ? Runtime . Cache . TtlSeconds . Value
594+ : EntityCacheOptions . DEFAULT_TTL_SECONDS ;
595+ }
596+
597+ /// <summary>
598+ /// Returns the cache level value for the global cache entry.
599+ /// The level is inferred from the runtime cache Level2 configuration:
600+ /// if Level2 is enabled, the level is L1L2; otherwise L1.
601+ /// Returns null when runtime cache is not configured.
602+ /// </summary>
603+ /// <returns>Cache level for a cache entry, or null if runtime cache is not configured.</returns>
604+ public virtual EntityCacheLevel ? GlobalCacheEntryLevel ( )
605+ {
606+ return Runtime ? . Cache ? . InferredLevel ;
581607 }
582608
583609 /// <summary>
@@ -592,18 +618,6 @@ public virtual bool CanUseCache()
592618 return IsCachingEnabled && ! setSessionContextEnabled ;
593619 }
594620
595- /// <summary>
596- /// Returns the ttl-seconds value for the global cache entry.
597- /// If no value is explicitly set, returns the global default value.
598- /// </summary>
599- /// <returns>Number of seconds a cache entry should be valid before cache eviction.</returns>
600- public int GlobalCacheEntryTtl ( )
601- {
602- return Runtime is not null && Runtime . IsCachingEnabled && Runtime . Cache . UserProvidedTtlOptions
603- ? Runtime . Cache . TtlSeconds . Value
604- : EntityCacheOptions . DEFAULT_TTL_SECONDS ;
605- }
606-
607621 private void CheckDataSourceNamePresent ( string dataSourceName )
608622 {
609623 if ( ! _dataSourceNameToDataSource . ContainsKey ( dataSourceName ) )
@@ -794,4 +808,46 @@ public LogLevel GetConfiguredLogLevel(string loggerFilter = "")
794808 /// </summary>
795809 [ JsonIgnore ]
796810 public DmlToolsConfig ? McpDmlTools => Runtime ? . Mcp ? . DmlTools ;
811+
812+ /// <summary>
813+ /// Determines whether caching is enabled for a given entity, resolving inheritance lazily.
814+ /// If the entity explicitly sets cache enabled/disabled, that value wins.
815+ /// If the entity has a cache object but did not explicitly set enabled (UserProvidedEnabledOptions is false),
816+ /// the global runtime cache enabled setting is inherited.
817+ /// If the entity has no cache config at all, the global runtime cache enabled setting is inherited.
818+ /// </summary>
819+ /// <param name="entityName">Name of the entity to check cache configuration.</param>
820+ /// <returns>Whether caching is enabled for the entity.</returns>
821+ /// <exception cref="DataApiBuilderException">Raised when an invalid entity name is provided.</exception>
822+ public virtual bool IsEntityCachingEnabled ( string entityName )
823+ {
824+ if ( ! Entities . TryGetValue ( entityName , out Entity ? entityConfig ) )
825+ {
826+ throw new DataApiBuilderException (
827+ message : $ "{ entityName } is not a valid entity.",
828+ statusCode : HttpStatusCode . BadRequest ,
829+ subStatusCode : DataApiBuilderException . SubStatusCodes . EntityNotFound ) ;
830+ }
831+
832+ return IsEntityCachingEnabled ( entityConfig ) ;
833+ }
834+
835+ /// <summary>
836+ /// Determines whether caching is enabled for a given entity, resolving inheritance lazily.
837+ /// If the entity explicitly sets cache enabled/disabled (UserProvidedEnabledOptions is true), that value wins.
838+ /// Otherwise, inherits the global runtime cache enabled setting.
839+ /// </summary>
840+ /// <param name="entity">The entity to check cache configuration.</param>
841+ /// <returns>Whether caching is enabled for the entity.</returns>
842+ private bool IsEntityCachingEnabled ( Entity entity )
843+ {
844+ // If entity has an explicit cache config with user-provided enabled value, use it.
845+ if ( entity . Cache is not null && entity . Cache . UserProvidedEnabledOptions )
846+ {
847+ return entity . IsCachingEnabled ;
848+ }
849+
850+ // Otherwise, inherit from the global runtime cache setting.
851+ return IsCachingEnabled ;
852+ }
797853}
0 commit comments