Skip to content

Conversation

@seantleonard
Copy link
Contributor

Why make this change?

  • Closes [Enhancement] Cache - Stored Procedure Support #2299
  • Enabling DAB's cache only worked for generic table/view queries (REST: GET, GraphQL: queries). Stored procedure results were always fetched from the database.
  • This change enables caching for stored procedure results when the stored procedure is configured as such:
"entities": {
  "MyStoredProcedureEntity": {
    "source": {
      "type": "stored-procedure",
      "object": "dbo.spExample"
    },
    "graphql": {
      "operation": "query"
    },
    "rest": {
      "methods": [ "GET" ]
    },
    "cache": {
      "enabled": true,
      "ttl-seconds": 5
    },
    "permissions": [
      {
        "role": "anonymous",
        "actions": [
          "execute"
        ]
      }
    ]
  }

What is this change?

  • Updates DAB's SqlQueryEngine function private async Task<JsonDocument?> ExecuteAsync(SqlExecuteStructure structure, string dataSourceName) to utilize DAB's cache service.
  • The code now checks whether caching is enabled globally:
    • RuntimeConfig.Cache.Enabled is true
    • RuntimeConfig.DataSource.Options.SetSessionContext is false
  • The cache service is invoked with type JsonArray : _cache.GetOrSetAsync<JsonArray?>(...)

How was this tested?

  • Integration Tests
  • Unit Tests: tests that cache invocation with type JsonArray succeeds. This covers how stored procedures are executed in the query engine.

Sample Request(s)

Dab config: use example entity at top of this description.
Example stored procedure:

create procedure [dbo].[stp_Dummy]
@mandatoryParam int,
@optionalParam int = 10
as
select 
    @mandatoryParam as mandatoryParam,
    @optionalParam as optionalParam
GO

REST

GET http://localhost:5000/api/Dummy?mandatoryParam=123&optionalParam=2345

GraphQL

query mySP{
  executeDummy {
    mandatoryParam
    optionalParam
  }
}

…ort for using cache. Updated method signature formatting in DabCacheService to have params on new lines.
@abhishekkumams
Copy link
Contributor

how would it work, when caching is disabled globally but enabled at entity level or vice-versa?

@seantleonard
Copy link
Contributor Author

seantleonard commented Sep 9, 2024

how would it work, when caching is disabled globally but enabled at entity level or vice-versa?

@abhishekkumams
For caching:

  1. Global cache must be enabled (with session context turned off)
  2. Entity cache must be turned on + no db policy configured.

image

@seantleonard
Copy link
Contributor Author

/azp run

@Aniruddh25
Copy link
Collaborator

Would this cache results even if the stored proc has a mutation operation in it? If thats the case, in a cache hit, will the stores proc not be executed? So, for cache hits, we will NOT execute the mutation operation.. Is the responsibility to ensure they enable cache only for READ-ONLY stored procs with our customer developers?

…tances of "cacheEntryTtl" variable name to "cacheEntryTtlInSeconds"
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.

Looks good, left a few questions and suggestions

…h checks for invocation > 1 and invocation = 1 to give separate error messages to clearly indicate whether the cache wasn't hit
@seantleonard
Copy link
Contributor Author

@Aniruddh25

Would this cache results even if the stored proc has a mutation operation in it?

No, this code is within the QueryEngine so Stored Procedures configured as mutations would not enter this code path.

@seantleonard seantleonard added this to the 1.3 milestone Sep 9, 2024
@abhishekkumams
Copy link
Contributor

/azp run

@seantleonard seantleonard merged commit b109718 into main Sep 10, 2024
@seantleonard seantleonard deleted the dev/sean/cache_sp_2299 branch September 10, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Enhancement] Cache - Stored Procedure Support

4 participants