Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Core/Resolvers/SqlQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ public async Task<IActionResult> ExecuteAsync(StoredProcedureRequestContext cont
// Open connection and execute query using _queryExecutor
string queryString = queryBuilder.Build(structure);

// Global Cache enablement check
if (runtimeConfig.CanUseCache())
{
// Entity level cache behavior checks
bool dbPolicyConfigured = !string.IsNullOrEmpty(structure.DbPolicyPredicatesForOperations[EntityActionOperation.Read]);
bool entityCacheEnabled = runtimeConfig.Entities[structure.EntityName].IsCachingEnabled;

if (dbPolicyConfigured)
// If a db policy is configured for the read operation in the context of the executing role, skip the cache.
// We want to avoid caching token metadata because token metadata can change frequently and we want to avoid caching it.
if (!dbPolicyConfigured && entityCacheEnabled)
{
DatabaseQueryMetadata queryMetadata = new(queryText: queryString, dataSource: dataSourceName, queryParameters: structure.Parameters);
JsonElement result = await _cache.GetOrSetAsync<JsonElement>(queryExecutor, queryMetadata, cacheEntryTtl: runtimeConfig.GetEntityCacheEntryTtl(entityName: structure.EntityName));
Expand All @@ -225,6 +230,11 @@ public async Task<IActionResult> ExecuteAsync(StoredProcedureRequestContext cont
}
}

// Execute a request normally (skipping cache) when any of the cache usage checks fail:
// 1. Global cache is disabled
// 2. MSSQL datasource set-session-context property is true
// 3. Entity level cache is disabled
// 4. A db policy is resolved for the read operation
JsonDocument? response = await queryExecutor.ExecuteQueryAsync(
sqltext: queryString,
parameters: structure.Parameters,
Expand Down