131
131
import java .util .Optional ;
132
132
import java .util .Set ;
133
133
import java .util .stream .Collectors ;
134
+ import javax .cache .CacheManager ;
135
+ import javax .cache .configuration .MutableConfiguration ;
134
136
import javax .ws .rs .core .Response .ResponseBuilder ;
135
137
136
138
import static org .ohdsi .webapi .Constants .Params .COHORT_DEFINITION_ID ;
137
139
import static org .ohdsi .webapi .Constants .Params .JOB_NAME ;
138
140
import static org .ohdsi .webapi .Constants .Params .SOURCE_ID ;
139
141
import org .ohdsi .webapi .source .SourceService ;
140
142
import 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 ;
141
146
142
147
/**
143
148
* Provides REST services for working with cohort definitions.
149
154
@ Component
150
155
public class CohortDefinitionService extends AbstractDaoService implements HasTags <Integer > {
151
156
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
+
152
175
private static final CohortExpressionQueryBuilder queryBuilder = new CohortExpressionQueryBuilder ();
153
176
154
177
@ Autowired
@@ -205,7 +228,7 @@ public class CohortDefinitionService extends AbstractDaoService implements HasTa
205
228
@ Autowired
206
229
private VersionService <CohortVersion > versionService ;
207
230
208
- @ Value ("${security.defaultGlobalReadPermissions}" )
231
+ @ Value ("${security.defaultGlobalReadPermissions}" )
209
232
private boolean defaultGlobalReadPermissions ;
210
233
211
234
private final MarkdownRender markdownPF = new MarkdownRender ();
@@ -408,6 +431,7 @@ public GenerateSqlResult generateSql(GenerateSqlRequest request) {
408
431
@ Path ("/" )
409
432
@ Produces (MediaType .APPLICATION_JSON )
410
433
@ Transactional
434
+ @ Cacheable (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , key = "@permissionService.getSubjectCacheKey()" )
411
435
public List <CohortMetadataDTO > getCohortDefinitionList () {
412
436
List <CohortDefinition > definitions = cohortDefinitionRepository .list ();
413
437
return definitions .stream ()
@@ -436,6 +460,7 @@ public List<CohortMetadataDTO> getCohortDefinitionList() {
436
460
@ Transactional
437
461
@ Produces (MediaType .APPLICATION_JSON )
438
462
@ Consumes (MediaType .APPLICATION_JSON )
463
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
439
464
public CohortDTO createCohortDefinition (CohortDTO dto ) {
440
465
441
466
Date currentTime = Calendar .getInstance ().getTime ();
@@ -538,6 +563,7 @@ public int getCountCDefWithSameName(@PathParam("id") @DefaultValue("0") final in
538
563
@ Produces (MediaType .APPLICATION_JSON )
539
564
@ Consumes (MediaType .APPLICATION_JSON )
540
565
@ Transactional
566
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
541
567
public CohortDTO saveCohortDefinition (@ PathParam ("id" ) final int id , CohortDTO def ) {
542
568
Date currentTime = Calendar .getInstance ().getTime ();
543
569
@@ -670,6 +696,7 @@ public List<CohortGenerationInfoDTO> getInfo(@PathParam("id") final int id) {
670
696
@ Produces (MediaType .APPLICATION_JSON )
671
697
@ Path ("/{id}/copy" )
672
698
@ Transactional
699
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
673
700
public CohortDTO copy (@ PathParam ("id" ) final int id ) {
674
701
CohortDTO sourceDef = getCohortDefinition (id );
675
702
sourceDef .setId (null ); // clear the ID
@@ -954,6 +981,7 @@ private Response printFrindly(String markdown, String format) {
954
981
@ POST
955
982
@ Produces (MediaType .APPLICATION_JSON )
956
983
@ Path ("/{id}/tag/" )
984
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
957
985
@ Transactional
958
986
public void assignTag (@ PathParam ("id" ) final Integer id , final int tagId ) {
959
987
CohortDefinition entity = cohortDefinitionRepository .findOne (id );
@@ -971,6 +999,7 @@ public void assignTag(@PathParam("id") final Integer id, final int tagId) {
971
999
@ DELETE
972
1000
@ Produces (MediaType .APPLICATION_JSON )
973
1001
@ Path ("/{id}/tag/{tagId}" )
1002
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
974
1003
@ Transactional
975
1004
public void unassignTag (@ PathParam ("id" ) final Integer id , @ PathParam ("tagId" ) final int tagId ) {
976
1005
CohortDefinition entity = cohortDefinitionRepository .findOne (id );
@@ -1106,6 +1135,7 @@ public void deleteVersion(@PathParam("id") final int id, @PathParam("version") f
1106
1135
@ Produces (MediaType .APPLICATION_JSON )
1107
1136
@ Path ("/{id}/version/{version}/createAsset" )
1108
1137
@ Transactional
1138
+ @ CacheEvict (cacheNames = CachingSetup .COHORT_DEFINITION_LIST_CACHE , allEntries = true )
1109
1139
public CohortDTO copyAssetFromVersion (@ PathParam ("id" ) final int id , @ PathParam ("version" ) final int version ) {
1110
1140
checkVersion (id , version , false );
1111
1141
CohortVersion cohortVersion = versionService .getById (VersionType .COHORT , id , version );
0 commit comments