Skip to content

Commit 19afe75

Browse files
authored
Cache Enablement check changes (#2012)
## Why make this change? - Ensure cache is used when enabled and add additional checks to make this flow as expected. ## What is this change? - Change cache check to ensure dbpolicies are NOT resolved for request in order to use cache - Add check to ensure entity cache is enabled. ## How was this tested? - TBD
1 parent 126737d commit 19afe75

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Core/Resolvers/SqlQueryEngine.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,16 @@ public async Task<IActionResult> ExecuteAsync(StoredProcedureRequestContext cont
211211
// Open connection and execute query using _queryExecutor
212212
string queryString = queryBuilder.Build(structure);
213213

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

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

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

0 commit comments

Comments
 (0)