131131import java .util .Optional ;
132132import java .util .Set ;
133133import java .util .stream .Collectors ;
134+ import javax .cache .CacheManager ;
135+ import javax .cache .configuration .MutableConfiguration ;
134136import javax .ws .rs .core .Response .ResponseBuilder ;
135137
136138import static org .ohdsi .webapi .Constants .Params .COHORT_DEFINITION_ID ;
137139import static org .ohdsi .webapi .Constants .Params .JOB_NAME ;
138140import static org .ohdsi .webapi .Constants .Params .SOURCE_ID ;
139141import org .ohdsi .webapi .source .SourceService ;
140142import static org .ohdsi .webapi .util .SecurityUtils .whitelist ;
143+ import org .springframework .boot .autoconfigure .cache .JCacheManagerCustomizer ;
144+ import org .springframework .cache .annotation .CacheEvict ;
145+ import org .springframework .cache .annotation .Cacheable ;
141146
142147/**
143148 * Provides REST services for working with cohort definitions.
149154@ Component
150155public class CohortDefinitionService extends AbstractDaoService implements HasTags <Integer > {
151156
157+ //create cache
158+ @ Component
159+ public static class CachingSetup implements JCacheManagerCustomizer {
160+
161+ public static final String COHORT_DEFINITION_LIST_CACHE = "cohortDefinitionList" ;
162+
163+ @ Override
164+ public void customize (CacheManager cacheManager ) {
165+ // Evict when a cohort definition is created or updated, or permissions, or tags
166+ if (!CacheHelper .getCacheNames (cacheManager ).contains (COHORT_DEFINITION_LIST_CACHE )) {
167+ cacheManager .createCache (COHORT_DEFINITION_LIST_CACHE , new MutableConfiguration <String , List <CohortMetadataDTO >>()
168+ .setTypes (String .class , (Class <List <CohortMetadataDTO >>) (Class <?>) List .class )
169+ .setStoreByValue (false )
170+ .setStatisticsEnabled (true ));
171+ }
172+ }
173+ }
174+
152175 private static final CohortExpressionQueryBuilder queryBuilder = new CohortExpressionQueryBuilder ();
153176
154177 @ Autowired
@@ -205,7 +228,7 @@ public class CohortDefinitionService extends AbstractDaoService implements HasTa
205228 @ Autowired
206229 private VersionService <CohortVersion > versionService ;
207230
208- @ Value ("${security.defaultGlobalReadPermissions}" )
231+ @ Value ("${security.defaultGlobalReadPermissions}" )
209232 private boolean defaultGlobalReadPermissions ;
210233
211234 private final MarkdownRender markdownPF = new MarkdownRender ();
@@ -408,6 +431,7 @@ public GenerateSqlResult generateSql(GenerateSqlRequest request) {
408431 @ Path ("/" )
409432 @ Produces (MediaType .APPLICATION_JSON )
410433 @ Transactional
434+ @ Cacheable (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , key = "@permissionService.getSubjectCacheKey()" )
411435 public List <CohortMetadataDTO > getCohortDefinitionList () {
412436 List <CohortDefinition > definitions = cohortDefinitionRepository .list ();
413437 return definitions .stream ()
@@ -436,6 +460,7 @@ public List<CohortMetadataDTO> getCohortDefinitionList() {
436460 @ Transactional
437461 @ Produces (MediaType .APPLICATION_JSON )
438462 @ Consumes (MediaType .APPLICATION_JSON )
463+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
439464 public CohortDTO createCohortDefinition (CohortDTO dto ) {
440465
441466 Date currentTime = Calendar .getInstance ().getTime ();
@@ -538,6 +563,7 @@ public int getCountCDefWithSameName(@PathParam("id") @DefaultValue("0") final in
538563 @ Produces (MediaType .APPLICATION_JSON )
539564 @ Consumes (MediaType .APPLICATION_JSON )
540565 @ Transactional
566+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
541567 public CohortDTO saveCohortDefinition (@ PathParam ("id" ) final int id , CohortDTO def ) {
542568 Date currentTime = Calendar .getInstance ().getTime ();
543569
@@ -670,6 +696,7 @@ public List<CohortGenerationInfoDTO> getInfo(@PathParam("id") final int id) {
670696 @ Produces (MediaType .APPLICATION_JSON )
671697 @ Path ("/{id}/copy" )
672698 @ Transactional
699+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
673700 public CohortDTO copy (@ PathParam ("id" ) final int id ) {
674701 CohortDTO sourceDef = getCohortDefinition (id );
675702 sourceDef .setId (null ); // clear the ID
@@ -954,6 +981,7 @@ private Response printFrindly(String markdown, String format) {
954981 @ POST
955982 @ Produces (MediaType .APPLICATION_JSON )
956983 @ Path ("/{id}/tag/" )
984+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
957985 @ Transactional
958986 public void assignTag (@ PathParam ("id" ) final Integer id , final int tagId ) {
959987 CohortDefinition entity = cohortDefinitionRepository .findOne (id );
@@ -971,6 +999,7 @@ public void assignTag(@PathParam("id") final Integer id, final int tagId) {
971999 @ DELETE
9721000 @ Produces (MediaType .APPLICATION_JSON )
9731001 @ Path ("/{id}/tag/{tagId}" )
1002+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
9741003 @ Transactional
9751004 public void unassignTag (@ PathParam ("id" ) final Integer id , @ PathParam ("tagId" ) final int tagId ) {
9761005 CohortDefinition entity = cohortDefinitionRepository .findOne (id );
@@ -1106,6 +1135,7 @@ public void deleteVersion(@PathParam("id") final int id, @PathParam("version") f
11061135 @ Produces (MediaType .APPLICATION_JSON )
11071136 @ Path ("/{id}/version/{version}/createAsset" )
11081137 @ Transactional
1138+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
11091139 public CohortDTO copyAssetFromVersion (@ PathParam ("id" ) final int id , @ PathParam ("version" ) final int version ) {
11101140 checkVersion (id , version , false );
11111141 CohortVersion cohortVersion = versionService .getById (VersionType .COHORT , id , version );
0 commit comments