From 828baa05c888fb8c1d6fc41dea12087d89fd972a Mon Sep 17 00:00:00 2001 From: Scott Arbeit Date: Fri, 3 Nov 2023 23:55:25 -0700 Subject: [PATCH] Added MemoryCache to speed up validations. --- src/Grace.Actors/Grace.Actors.fsproj | 1 + src/Grace.Actors/Services.Actor.fs | 5 + src/Grace.Server/ApplicationContext.Server.fs | 10 +- src/Grace.Server/Branch.Server.fs | 9 +- src/Grace.Server/Directory.Server.fs | 6 +- src/Grace.Server/Grace.Server.fsproj | 9 +- src/Grace.Server/Organization.Server.fs | 29 +-- src/Grace.Server/Owner.Server.fs | 12 +- src/Grace.Server/Program.Server.fs | 5 + src/Grace.Server/Repository.Server.fs | 14 +- src/Grace.Server/Startup.Server.fs | 3 +- src/Grace.Server/Storage.Server.fs | 6 +- src/Grace.Server/Validations.Server.fs | 171 ++++++++++++------ src/Grace.Shared/Constants.Shared.fs | 3 + 14 files changed, 183 insertions(+), 100 deletions(-) diff --git a/src/Grace.Actors/Grace.Actors.fsproj b/src/Grace.Actors/Grace.Actors.fsproj index 3106401..8f58341 100644 --- a/src/Grace.Actors/Grace.Actors.fsproj +++ b/src/Grace.Actors/Grace.Actors.fsproj @@ -44,6 +44,7 @@ + diff --git a/src/Grace.Actors/Services.Actor.fs b/src/Grace.Actors/Services.Actor.fs index deb6986..dc58141 100644 --- a/src/Grace.Actors/Services.Actor.fs +++ b/src/Grace.Actors/Services.Actor.fs @@ -18,6 +18,7 @@ open Grace.Shared.Types open Grace.Shared.Utilities open Microsoft.Azure.Cosmos open Microsoft.Azure.Cosmos.Linq +open Microsoft.Extensions.Caching.Memory open Microsoft.Extensions.Logging open System open System.Collections.Concurrent @@ -72,6 +73,10 @@ module Services = let setLoggerFactory (factory: ILoggerFactory) = loggerFactory <- factory + let mutable internal memoryCache: IMemoryCache = null + let setMemoryCache (cache: IMemoryCache) = + memoryCache <- cache + let linqSerializerOptions = CosmosLinqSerializerOptions(PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase) /// Custom QueryRequestOptions that requests Index Metrics only in DEBUG build. diff --git a/src/Grace.Server/ApplicationContext.Server.fs b/src/Grace.Server/ApplicationContext.Server.fs index cde196b..eae047e 100644 --- a/src/Grace.Server/ApplicationContext.Server.fs +++ b/src/Grace.Server/ApplicationContext.Server.fs @@ -10,6 +10,7 @@ open Grace.Shared open Grace.Shared.Types open Grace.Shared.Utilities open Microsoft.Azure.Cosmos +open Microsoft.Extensions.Caching.Memory open Microsoft.Extensions.Configuration open Microsoft.Extensions.DependencyInjection open Microsoft.Extensions.Logging @@ -31,6 +32,7 @@ module ApplicationContext = let mutable private actorProxyFactory: IActorProxyFactory = null let mutable private actorStateStorageProvider: ActorStateStorageProvider = ActorStateStorageProvider.Unknown let mutable loggerFactory: ILoggerFactory = null + let mutable memoryCache: IMemoryCache = null let ActorProxyFactory() = actorProxyFactory let ActorStateStorageProvider() = actorStateStorageProvider @@ -131,7 +133,11 @@ module ApplicationContext = let! containerResponse = database.CreateContainerIfNotExistsAsync(containerProperties) let cosmosContainer = containerResponse.Container + // Create a MemoryCache instance. + memoryCache <- new MemoryCache(MemoryCacheOptions(), loggerFactory) + // Inject the CosmosClient and CosmosContainer into Actor Services. - Grace.Actors.Services.setCosmosClient (cosmosClient) - Grace.Actors.Services.setCosmosContainer (cosmosContainer) + Grace.Actors.Services.setCosmosClient cosmosClient + Grace.Actors.Services.setCosmosContainer cosmosContainer + Grace.Actors.Services.setMemoryCache memoryCache } :> Task diff --git a/src/Grace.Server/Branch.Server.fs b/src/Grace.Server/Branch.Server.fs index 3a5d61c..2d07cb4 100644 --- a/src/Grace.Server/Branch.Server.fs +++ b/src/Grace.Server/Branch.Server.fs @@ -35,7 +35,7 @@ module Branch = let actorProxyFactory = ApplicationContext.ActorProxyFactory() - let getActorProxy (context: HttpContext) (branchId: BranchId) = + let getActorProxy (context: HttpContext) (branchId: string) = let actorId = ActorId($"{branchId}") actorProxyFactory.CreateActorProxy(actorId, ActorName.Branch) @@ -53,7 +53,7 @@ module Branch = parameters.RepositoryId <- repositoryId match! resolveBranchId repositoryId parameters.BranchId parameters.BranchName with | Some branchId -> - let actorProxy = getActorProxy context (Guid.Parse(branchId)) + let actorProxy = getActorProxy context branchId let! cmd = command parameters match! actorProxy.Handle cmd (Services.createMetadata context) with | Ok graceReturn -> return! context |> result200Ok graceReturn @@ -83,11 +83,12 @@ module Branch = if validationsPassed then match! resolveBranchId parameters.RepositoryId parameters.BranchId parameters.BranchName with | Some branchId -> - let actorProxy = getActorProxy context (Guid.Parse(branchId)) + let actorProxy = getActorProxy context branchId let! queryResult = query context maxCount actorProxy let! returnValue = context |> result200Ok (GraceReturnValue.Create queryResult (getCorrelationId context)) return returnValue - | None -> return! context |> result400BadRequest (GraceError.Create (BranchError.getErrorMessage BranchDoesNotExist) (getCorrelationId context)) + | None -> + return! context |> result400BadRequest (GraceError.Create (BranchError.getErrorMessage BranchDoesNotExist) (getCorrelationId context)) else let! error = validationResults |> getFirstError let graceError = GraceError.Create (BranchError.getErrorMessage error) (getCorrelationId context) diff --git a/src/Grace.Server/Directory.Server.fs b/src/Grace.Server/Directory.Server.fs index 9384fef..64f8214 100644 --- a/src/Grace.Server/Directory.Server.fs +++ b/src/Grace.Server/Directory.Server.fs @@ -35,8 +35,8 @@ module DirectoryVersion = let actorProxyFactory = ApplicationContext.ActorProxyFactory() - let getActorProxy (context: HttpContext) (directoryId: DirectoryId) = - let actorId = GetActorId directoryId + let getActorProxy (context: HttpContext) (directoryId: string) = + let actorId = ActorId(directoryId) actorProxyFactory.CreateActorProxy(actorId, ActorName.DirectoryVersion) let processCommand<'T when 'T :> DirectoryParameters> (context: HttpContext) (validations: Validations<'T>) (command: 'T -> HttpContext -> Task>) = @@ -70,7 +70,7 @@ module DirectoryVersion = let validationResults = validations parameters context let! validationsPassed = validationResults |> allPass if validationsPassed then - let actorProxy = getActorProxy context (Guid.Parse(parameters.DirectoryId)) + let actorProxy = getActorProxy context parameters.DirectoryId let! queryResult = query context maxCount actorProxy let! returnValue = context |> result200Ok (GraceReturnValue.Create queryResult (getCorrelationId context)) return returnValue diff --git a/src/Grace.Server/Grace.Server.fsproj b/src/Grace.Server/Grace.Server.fsproj index f8f1899..7017c04 100644 --- a/src/Grace.Server/Grace.Server.fsproj +++ b/src/Grace.Server/Grace.Server.fsproj @@ -21,10 +21,10 @@ https://github.com/ScottArbeit/Grace - - - - + + + + @@ -63,6 +63,7 @@ +