Skip to content

Commit

Permalink
Merge pull request #17 from jembi/concept-caching
Browse files Browse the repository at this point in the history
Concept caching OHM-69
  • Loading branch information
rcrichton committed Mar 5, 2015
2 parents 257c7f9 + 0195077 commit 83bade5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
api/.settings/
omod/.settings/
target/
.idea/
*.iml
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
</parent>

<artifactId>shr-contenthandler-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
*/
package org.openmrs.module.shr.contenthandler;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -99,14 +95,42 @@ private Obs createUnstructuredDataObs(Content content) {
return res;
}

private static final Map<String, Integer> conceptCache = Collections.synchronizedMap(new HashMap<String, Integer>());
private static final String GP_CACHE_CONCEPTS_BY_NAME = "shr.contenthandler.cacheConceptsByName";
private static Boolean cacheConceptsByName = null;

private Concept getUnstructuredAttachmentConcept(CodedValue formatCode) {
ConceptService cs = Context.getConceptService();
String conceptName = getUnstructuredAttachmentConceptName(formatCode);
synchronized (this) {
if (cacheConceptsByName == null) {
if (Context.getAdministrationService().getGlobalProperty(GP_CACHE_CONCEPTS_BY_NAME).equalsIgnoreCase("true")) {
cacheConceptsByName = true;
} else {
cacheConceptsByName = false;
}
}
}

ConceptService cs = Context.getConceptService();
String conceptName = getUnstructuredAttachmentConceptName(formatCode);

if (cacheConceptsByName) {
Integer conceptId = conceptCache.get(conceptName);

if (conceptId != null) {
return cs.getConcept(conceptId);
}
}

Concept res = cs.getConceptByName(conceptName);
if (res==null) {
if (res == null) {
res = buildUnstructuredAttachmentConcept(conceptName);
cs.saveConcept(res);
res = cs.saveConcept(res);
}

if (cacheConceptsByName) {
conceptCache.put(conceptName, res.getConceptId());
}

return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class UnstructuredDataHandlerTest extends BaseModuleContextSensitiveTest
public void before() {
//Use our in-memory complex obs handler
Context.getAdministrationService().setGlobalProperty(UnstructuredDataHandler.UNSTRUCTURED_DATA_HANDLER_GLOBAL_PROP, "InMemoryComplexObsHandler");
Context.getAdministrationService().setGlobalProperty("shr.contenthandler.cacheConceptsByName", "false");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
</parent>

<artifactId>shr-contenthandler-omod</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
The complex obs handler to use for unstructured data.
</description>
</globalProperty>
<globalProperty>
<property>shr.contenthandler.cacheConceptsByName</property>
<defaultValue>false</defaultValue>
<description>
Enables unstructured attachment concepts to be cached by name. This greatly improves performance. You MUST restart the module for this to take effect. Note: changes to concept names will only be picked up on server restart.
</description>
</globalProperty>
<!-- / Global props -->

<!-- Internationalization -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.openmrs.module</groupId>
<artifactId>shr-contenthandler</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<packaging>pom</packaging>
<name>SHR Content Handler Module</name>
<description>Provides interfaces and services for dynamically linking content processors with particular content types.</description>
Expand Down

0 comments on commit 83bade5

Please sign in to comment.