Skip to content

Commit

Permalink
added source list caching.
Browse files Browse the repository at this point in the history
Changed editor config to tabs.
Made cache endpoints auth restricted.
  • Loading branch information
chrisknoll committed Dec 6, 2024
1 parent da68972 commit 230dcda
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
root = true

[*]
indent_style = space
indent_style = tab
indent_size = 2
end_of_line = crlf
charset = utf-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ protected FilterChainBuilder setupProtectedPaths(FilterChainBuilder filterChainB
return filterChainBuilder
// version info
.addRestPath("/info")
.addRestPath("/cache/*") // move this to protected path before PR
// DDL service
.addRestPath("/ddl/results")
.addRestPath("/ddl/cemresults")
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/ohdsi/webapi/source/SourceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.springframework.cache.annotation.CacheEvict;

@Path("/source/")
@Component
Expand Down Expand Up @@ -157,6 +157,7 @@ public SourceDetails getSourceDetails(@PathParam("sourceId") Integer sourceId) {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@CacheEvict(cacheNames = SourceService.CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public SourceInfo createSource(@FormDataParam("keyfile") InputStream file, @FormDataParam("keyfile") FormDataContentDisposition fileDetail, @FormDataParam("source") SourceRequest request) throws Exception {
if (!securityEnabled) {
throw new NotAuthorizedException(SECURE_MODE_ERROR);
Expand Down Expand Up @@ -219,6 +220,7 @@ public SourceInfo createSource(@FormDataParam("keyfile") InputStream file, @Form
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@CacheEvict(cacheNames = SourceService.CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public SourceInfo updateSource(@PathParam("sourceId") Integer sourceId, @FormDataParam("keyfile") InputStream file, @FormDataParam("keyfile") FormDataContentDisposition fileDetail, @FormDataParam("source") SourceRequest request) throws IOException {
if (!securityEnabled) {
throw new NotAuthorizedException(SECURE_MODE_ERROR);
Expand Down Expand Up @@ -318,6 +320,7 @@ private void setKeyfileData(Source updated, Source source, InputStream file) thr
@Path("{sourceId}")
@DELETE
@Transactional
@CacheEvict(cacheNames = SourceService.CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public Response delete(@PathParam("sourceId") Integer sourceId) throws Exception {
if (!securityEnabled){
return getInsecureModeResponse();
Expand Down Expand Up @@ -384,6 +387,7 @@ public Map<SourceDaimon.DaimonType, SourceInfo> getPriorityDaimons() {
@Path("{sourceKey}/daimons/{daimonType}/set-priority")
@POST
@Produces(MediaType.APPLICATION_JSON)
@CacheEvict(cacheNames = SourceService.CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public Response updateSourcePriority(
@PathParam("sourceKey") final String sourceKey,
@PathParam("daimonType") final String daimonTypeName
Expand Down
46 changes: 32 additions & 14 deletions src/main/java/org/ohdsi/webapi/source/SourceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
import javax.cache.CacheManager;
import javax.cache.configuration.MutableConfiguration;
import org.ohdsi.webapi.util.CacheHelper;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Service
public class SourceService extends AbstractDaoService {

private static Collection<Source> cachedSources = null;

@Component
public static class CachingSetup implements JCacheManagerCustomizer {

public static final String SOURCE_LIST_CACHE = "sourceList";

@Override
public void customize(CacheManager cacheManager) {
// Evict when a cohort definition is created or updated, or permissions, or tags
if (!CacheHelper.getCacheNames(cacheManager).contains(SOURCE_LIST_CACHE)) {
cacheManager.createCache(SOURCE_LIST_CACHE, new MutableConfiguration<Object, List<Source>>()
.setTypes(Object.class, (Class<List<Source>>) (Class<?>) List.class)
.setStoreByValue(false)
.setStatisticsEnabled(true));
}
}
}
@Value("${jasypt.encryptor.enabled}")
private boolean encryptorEnabled;

Expand Down Expand Up @@ -77,15 +98,13 @@ protected void doInTransactionWithoutResult(TransactionStatus transactionStatus)
}
}

public Collection<Source> getSources() {
@Cacheable(cacheNames = CachingSetup.SOURCE_LIST_CACHE)
public Collection<Source> getSources() {

if (cachedSources == null) {
List<Source> sources = sourceRepository.findAll();
Collections.sort(sources, new SortByKey());
cachedSources = sources;
}
return cachedSources;
}
List<Source> sources = sourceRepository.findAll();
Collections.sort(sources, new SortByKey());
return sources;
}

public Source findBySourceKey(final String sourceKey) {

Expand Down Expand Up @@ -160,10 +179,9 @@ public SourceInfo getPriorityVocabularySourceInfo() {
return new SourceInfo(source);
}

public void invalidateCache() {

this.cachedSources = null;
}
@CacheEvict(cacheNames = CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public void invalidateCache() {
}

private boolean checkConnectionSafe(Source source) {

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/appCache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<jsr107:cache name="conceptDetail" template="medium-heap"/>
<jsr107:cache name="conceptRelated" template="medium-heap"/>
<jsr107:cache name="conceptHierarchy" template="medium-heap"/>
<jsr107:cache name="sourceList" template="small-heap"/>
</jsr107:defaults>
</service>
<cache-template name="small-heap">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO ${ohdsiSchema}.sec_permission (id, value, description)
SELECT nextval('${ohdsiSchema}.sec_permission_id_seq'),
'cache:get',
'fetch WebAPI cache information';

INSERT INTO ${ohdsiSchema}.sec_role_permission (role_id, permission_id)
SELECT sr.id, sp.id
FROM ${ohdsiSchema}.sec_permission sp,
${ohdsiSchema}.sec_role sr
WHERE sp.value in
(
'cache:get'
)
AND sr.name IN ('admin');

0 comments on commit 230dcda

Please sign in to comment.