1
1
package ca .bc .gov .educ .api .trax .service .institute ;
2
2
3
+ import ca .bc .gov .educ .api .trax .constant .CacheKey ;
4
+ import ca .bc .gov .educ .api .trax .constant .CacheStatus ;
3
5
import ca .bc .gov .educ .api .trax .model .entity .institute .*;
4
6
import ca .bc .gov .educ .api .trax .repository .redis .*;
5
7
import com .google .common .collect .Lists ;
6
- import lombok .RequiredArgsConstructor ;
8
+ import lombok .AllArgsConstructor ;
7
9
import lombok .extern .slf4j .Slf4j ;
8
- import org .springframework .beans .factory .annotation .Autowired ;
9
10
import org .springframework .scheduling .annotation .Async ;
10
11
import org .springframework .stereotype .Service ;
11
12
import org .springframework .util .CollectionUtils ;
13
+ import redis .clients .jedis .JedisCluster ;
12
14
13
15
import java .util .List ;
14
16
15
17
@ Slf4j
16
- @ RequiredArgsConstructor
18
+ @ AllArgsConstructor
17
19
@ Service ("cacheService" )
18
20
public class CacheService {
19
21
20
- @ Autowired
21
22
SchoolRedisRepository schoolRedisRepository ;
22
23
23
- @ Autowired
24
24
SchoolDetailRedisRepository schoolDetailRedisRepository ;
25
25
26
- @ Autowired
27
26
DistrictRedisRepository districtRedisRepository ;
28
27
29
- @ Autowired
30
28
SchoolCategoryCodeRedisRepository schoolCategoryCodeRedisRepository ;
31
29
32
- @ Autowired
33
30
SchoolFundingGroupCodeRedisRepository schoolFundingGroupCodeRedisRepository ;
34
31
32
+ JedisCluster jedisCluster ;
33
+
35
34
@ Async ("taskExecutor" )
36
35
public void loadSchoolsIntoRedisCacheAsync (List <SchoolEntity > schools ) {
37
36
if (!CollectionUtils .isEmpty (schools )) {
@@ -40,13 +39,15 @@ public void loadSchoolsIntoRedisCacheAsync(List<SchoolEntity> schools) {
40
39
}
41
40
42
41
public void loadSchoolsIntoRedisCache (List <SchoolEntity > schools ) {
43
- if (!CollectionUtils .isEmpty (schools )) {
42
+ if (!isCacheLoading (CacheKey .SCHOOL_CACHE ) && !CollectionUtils .isEmpty (schools )) {
43
+ setCacheStateLoading (CacheKey .SCHOOL_CACHE );
44
44
long start = System .currentTimeMillis ();
45
45
log .debug ("****Before loading schools into cache" );
46
46
for (List <SchoolEntity > partition : Lists .partition (schools , 1000 )) {
47
47
schoolRedisRepository .saveAll (partition );
48
48
}
49
49
log .info ("{} Schools Loaded into cache in {} ms." , schools .size (), (System .currentTimeMillis () - start ));
50
+ setCacheReadiness (CacheKey .SCHOOL_CACHE );
50
51
}
51
52
}
52
53
@@ -58,13 +59,15 @@ public void loadSchoolDetailsIntoRedisCacheAsync(List<SchoolDetailEntity> school
58
59
}
59
60
60
61
public void loadSchoolDetailsIntoRedisCache (List <SchoolDetailEntity > schoolDetails ) {
61
- if (!CollectionUtils .isEmpty (schoolDetails )) {
62
+ if (!isCacheLoading (CacheKey .SCHOOL_DETAIL_CACHE ) && !CollectionUtils .isEmpty (schoolDetails )) {
63
+ setCacheStateLoading (CacheKey .SCHOOL_DETAIL_CACHE );
62
64
long start = System .currentTimeMillis ();
63
65
log .debug ("****Before loading school details into cache" );
64
66
for (List <SchoolDetailEntity > partition : Lists .partition (schoolDetails , 1000 )) {
65
67
schoolDetailRedisRepository .saveAll (partition );
66
68
}
67
69
log .info ("{} School Details Loaded into cache in {} ms." , schoolDetails .size (), (System .currentTimeMillis () - start ));
70
+ setCacheReadiness (CacheKey .SCHOOL_DETAIL_CACHE );
68
71
}
69
72
}
70
73
@@ -76,11 +79,13 @@ public void loadDistrictsIntoRedisCacheAsync(List<DistrictEntity> districts) {
76
79
}
77
80
78
81
public void loadDistrictsIntoRedisCache (List <DistrictEntity > districts ) {
79
- if (!CollectionUtils .isEmpty (districts )) {
82
+ if (!isCacheLoading (CacheKey .DISTRICT_CACHE ) && !CollectionUtils .isEmpty (districts )) {
83
+ setCacheStateLoading (CacheKey .DISTRICT_CACHE );
80
84
long start = System .currentTimeMillis ();
81
85
log .debug ("****Before loading districts into cache" );
82
86
districtRedisRepository .saveAll (districts );
83
87
log .info ("{} Districts Loaded into cache in {} ms." , districts .size (), (System .currentTimeMillis () - start ));
88
+ setCacheReadiness (CacheKey .DISTRICT_CACHE );
84
89
}
85
90
}
86
91
@@ -92,26 +97,46 @@ public void loadSchoolCategoryCodesIntoRedisCacheAsync(List<SchoolCategoryCodeEn
92
97
}
93
98
94
99
public void loadSchoolCategoryCodesIntoRedisCache (List <SchoolCategoryCodeEntity > schoolCategoryCodes ) {
95
- if (!CollectionUtils .isEmpty (schoolCategoryCodes )) {
100
+ if (!isCacheLoading ( CacheKey . SCHOOL_CATEGORY_CODE_CACHE ) && ! CollectionUtils .isEmpty (schoolCategoryCodes )) {
96
101
long start = System .currentTimeMillis ();
97
102
log .debug ("****Before loading School Category Codes into cache" );
103
+ setCacheStateLoading (CacheKey .SCHOOL_CATEGORY_CODE_CACHE );
98
104
schoolCategoryCodeRedisRepository .saveAll (schoolCategoryCodes );
99
105
log .info ("{} School Category Codes Loaded into cache in {} ms." , schoolCategoryCodes .size (), (System .currentTimeMillis () - start ));
106
+ setCacheReadiness (CacheKey .SCHOOL_CATEGORY_CODE_CACHE );
100
107
}
101
108
}
102
109
103
110
@ Async ("taskExecutor" )
104
111
public void loadSchoolFundingGroupCodesIntoRedisCacheAsync (List <SchoolFundingGroupCodeEntity > schoolFundingGroupCodes ) {
105
- loadSchoolFundingGroupCodesIntoRedisCache (schoolFundingGroupCodes );
112
+ if (!CollectionUtils .isEmpty (schoolFundingGroupCodes )) {
113
+ loadSchoolFundingGroupCodesIntoRedisCache (schoolFundingGroupCodes );
114
+ }
106
115
}
107
116
108
117
public void loadSchoolFundingGroupCodesIntoRedisCache (List <SchoolFundingGroupCodeEntity > schoolFundingGroupCodes ) {
109
- if (!CollectionUtils .isEmpty (schoolFundingGroupCodes )) {
118
+ if (!isCacheLoading ( CacheKey . SCHOOL_FUNDING_GROUP_CODE_CACHE ) && ! CollectionUtils .isEmpty (schoolFundingGroupCodes )) {
110
119
long start = System .currentTimeMillis ();
111
120
log .debug ("****Before loading School Funding Group Codes into cache" );
121
+ setCacheStateLoading (CacheKey .SCHOOL_FUNDING_GROUP_CODE_CACHE );
112
122
schoolFundingGroupCodeRedisRepository .saveAll (schoolFundingGroupCodes );
113
123
log .info ("{} School Funding Group Codes Loaded into cache in {} ms." , schoolFundingGroupCodes .size (), (System .currentTimeMillis () - start ));
124
+ setCacheReadiness (CacheKey .SCHOOL_FUNDING_GROUP_CODE_CACHE );
114
125
}
115
126
}
116
127
128
+ private boolean isCacheLoading (CacheKey cacheKey ) {
129
+ String cacheStatus = jedisCluster .get (cacheKey .name ());
130
+ return CacheStatus .LOADING .name ().equals (cacheStatus );
131
+ }
132
+
133
+ private void setCacheStateLoading (CacheKey cacheKey ) {
134
+ jedisCluster .set (cacheKey .name (), CacheStatus .LOADING .name ());
135
+ }
136
+
137
+ private void setCacheReadiness (CacheKey cacheKey ) {
138
+ jedisCluster .set (cacheKey .name (), CacheStatus .READY .name ());
139
+ log .info (String .format ("Success! - %s is now READY" , cacheKey ));
140
+ }
141
+
117
142
}
0 commit comments