Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache cds-service definitions #348

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.JsonArray;

import org.apache.commons.dbcp2.BasicDataSource;
import org.cqframework.cql.cql2elm.model.Model;
Expand Down Expand Up @@ -199,4 +201,31 @@ Map<VersionedIdentifier, Model> globalModelCache() {
Map<org.cqframework.cql.elm.execution.VersionedIdentifier, Library> globalLibraryCache() {
return new ConcurrentHashMap<org.cqframework.cql.elm.execution.VersionedIdentifier, Library>();
}

@Bean(name="globalCdsServiceCache")
AtomicReference<JsonArray> cdsServiceCache(){
return new AtomicReference<>();
}

@Bean
public IResourceChangeListener PlanDefinitionChangeListener(IResourceChangeListenerRegistry resourceChangeListenerRegistry, AtomicReference<JsonArray> cdsServiceCache) {
IResourceChangeListener listener = new IResourceChangeListener(){

@Override
public void handleInit(Collection<IIdType> theResourceIds) {
// Intentionally empty
}

// TODO: Selectively clear by url. Requires a lookup on the resource
@Override
public void handleChange(IResourceChangeEvent theResourceChangeEvent) {
cdsServiceCache.set(null);
}
};

resourceChangeListenerRegistry.registerResourceResourceChangeListener("PlanDefinition", SearchParameterMap.newSynchronous(), listener, 1000);

return listener;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand Down Expand Up @@ -78,6 +79,8 @@ public class CdsHooksServlet extends HttpServlet {

private LibraryHelper libraryHelper;

private AtomicReference<JsonArray> services;

@SuppressWarnings("unchecked")
@Override
public void init() {
Expand All @@ -92,6 +95,7 @@ public void init() {
this.serverTerminologyProvider = appCtx.getBean(TerminologyProvider.class);
this.modelResolver = appCtx.getBean("dstu3ModelResolver", ModelResolver.class);
this.libraryHelper = appCtx.getBean(LibraryHelper.class);
this.services = appCtx.getBean("globalCdsServiceCache", AtomicReference.class);
}

protected ProviderConfiguration getProviderConfiguration() {
Expand Down Expand Up @@ -277,8 +281,18 @@ private void printStackTrack(Exception e, HttpServletResponse response) throws I
response.getWriter().println(exceptionAsString);
}

private JsonArray getServicesArray() {
JsonArray cachedServices = this.services.get();
if (cachedServices == null || cachedServices.size() == 0) {
cachedServices = getServices().get("services").getAsJsonArray();
services.set(cachedServices);
}

return cachedServices;
}

private JsonObject getService(String service) {
JsonArray services = getServices().get("services").getAsJsonArray();
JsonArray services = getServicesArray();
List<String> ids = new ArrayList<>();
for (JsonElement element : services) {
if (element.isJsonObject() && element.getAsJsonObject().has("id")) {
Expand Down
27 changes: 1 addition & 26 deletions r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,6 @@ public org.opencds.cqf.cql.engine.execution.LibraryLoader createLibraryLoader(
TranslatingLibraryLoader translatingLibraryLoader = new TranslatingLibraryLoader(modelManager, contentProviders,
translatorOptions);

return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache) {
@Override
protected Boolean translatorOptionsMatch(Library library) {
return true;
}
};
}

@Override
public org.opencds.cqf.cql.engine.execution.LibraryLoader createLibraryLoader(
org.cqframework.cql.cql2elm.LibrarySourceProvider provider) {
ModelManager modelManager = new CacheAwareModelManager(this.modelCache);
LibraryManager libraryManager = new LibraryManager(modelManager);
libraryManager.getLibrarySourceLoader().clearProviders();

libraryManager.getLibrarySourceLoader().registerProvider(provider);

TranslatingLibraryLoader translatingLibraryLoader = new TranslatingLibraryLoader(modelManager, null,
translatorOptions);

return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache) {
@Override
protected Boolean translatorOptionsMatch(Library library) {
return true;
}
};
return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache);
}
}
16 changes: 15 additions & 1 deletion r4/src/main/java/org/opencds/cqf/r4/servlet/CdsHooksServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand Down Expand Up @@ -78,6 +79,8 @@ public class CdsHooksServlet extends HttpServlet {

private LibraryHelper libraryHelper;

private AtomicReference<JsonArray> services;

@SuppressWarnings("unchecked")
@Override
public void init() {
Expand All @@ -92,6 +95,7 @@ public void init() {
this.serverTerminologyProvider = appCtx.getBean(TerminologyProvider.class);
this.modelResolver = appCtx.getBean("r4ModelResolver", ModelResolver.class);
this.libraryHelper = appCtx.getBean(LibraryHelper.class);
this.services = appCtx.getBean("globalCdsServiceCache", AtomicReference.class);
}

protected ProviderConfiguration getProviderConfiguration() {
Expand Down Expand Up @@ -284,7 +288,7 @@ private void printStackTrack(Exception e, HttpServletResponse response) throws I
}

private JsonObject getService(String service) {
JsonArray services = getServices().get("services").getAsJsonArray();
JsonArray services = getServicesArray();
List<String> ids = new ArrayList<>();
for (JsonElement element : services) {
if (element.isJsonObject() && element.getAsJsonObject().has("id")) {
Expand All @@ -298,6 +302,16 @@ private JsonObject getService(String service) {
"Cannot resolve service: " + service + "\nAvailable services: " + ids.toString());
}

private JsonArray getServicesArray() {
JsonArray cachedServices = this.services.get();
if (cachedServices == null || cachedServices.size() == 0) {
cachedServices = getServices().get("services").getAsJsonArray();
services.set(cachedServices);
}

return cachedServices;
}

private JsonObject getServices() {
DiscoveryResolutionR4 discoveryResolutionR4 = new DiscoveryResolutionR4(
FhirContext.forCached(FhirVersionEnum.R4).newRestfulGenericClient(HapiProperties.getServerAddress()));
Expand Down