Skip to content

Commit

Permalink
[Cherrypick] Cache enablement check and dab-draft.schema.json changes (
Browse files Browse the repository at this point in the history
…#2033)

Cherry picks #2011 and #2012 to main

# 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.

# 2011

## Why this change?

This change follows up on
#1865, which adds caching
support.
VSCode suggesting the new `cache` properties were errors when included
in the config file. That is because the properties needed to be added to
the json schema.

Global `cache` property in the `runtime` section which adjacent to
`rest`, `graphql`, and `host`.

```json
{
  "runtime": {
    "cache": {
      "enabled": false,
      "ttl-seconds": 5
    }
  }
}
```

and per entity alongside `source`, `graphql`, `rest`, and `permissions`

```json
{
  "myEntity": {
    "cache": {
      "enabled": false,
      "ttl-seconds": 5
    }
  }
}
```

---------

Co-authored-by: Aniruddh Munde <anmunde@microsoft.com>
  • Loading branch information
seantleonard and Aniruddh25 committed Feb 13, 2024
1 parent d763a83 commit a64c277
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
32 changes: 32 additions & 0 deletions schemas/dab.draft.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@
}
}
},
"cache": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"description": "Enable caching of responses globally.",
"default": false
},
"ttl-seconds": {
"type": "integer",
"description": "Time to live in seconds",
"default": 5
}
}
},
"telemetry": {
"type": "object",
"description": "Telemetry configuration",
Expand Down Expand Up @@ -493,6 +509,22 @@
]
}
}
},
"cache": {
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {
"type": "boolean",
"description": "Enable caching of responses for this entity.",
"default": false
},
"ttl-seconds": {
"type": "integer",
"description": "Time to live in seconds",
"default": 5
}
}
}
},
"if": {
Expand Down
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

0 comments on commit a64c277

Please sign in to comment.