Commit 4c56b9a767f17860674fa69e1dc8eebd643ab623
1 parent
c7e9e1ef
feature(*): 对数据加上缓存
- 对数据加上缓存 - 发送消息异步去操作
Showing
6 changed files
with
40 additions
and
11 deletions
fw-hestia-server/src/main/java/cn/fw/hestia/server/task/SendMessageTask.java
... | ... | @@ -9,11 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired; |
9 | 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
10 | 10 | import org.springframework.scheduling.annotation.Scheduled; |
11 | 11 | import org.springframework.stereotype.Component; |
12 | -import org.springframework.transaction.annotation.Transactional; | |
13 | 12 | import org.springframework.util.CollectionUtils; |
14 | 13 | |
15 | 14 | import java.util.Date; |
16 | 15 | import java.util.List; |
16 | +import java.util.concurrent.CompletableFuture; | |
17 | 17 | |
18 | 18 | import static cn.fw.hestia.common.constant.MessageConstant.MAX_FREQUENCY; |
19 | 19 | |
... | ... | @@ -39,8 +39,7 @@ public class SendMessageTask { |
39 | 39 | /** |
40 | 40 | * 发送模板消息 |
41 | 41 | */ |
42 | - @Scheduled(initialDelay = 1000 * 10, fixedRate = 1000 * 10) | |
43 | - @Transactional(rollbackFor = Exception.class) | |
42 | + @Scheduled(initialDelay = 1000 * 10, fixedRate = 1000 * 5) | |
44 | 43 | public void sendNotice() { |
45 | 44 | List<MessageHistory> list = messageHistoryService.list(Wrappers.<MessageHistory>lambdaQuery() |
46 | 45 | .eq(MessageHistory::getState, MessageStateEnum.MADA) |
... | ... | @@ -53,7 +52,7 @@ public class SendMessageTask { |
53 | 52 | return; |
54 | 53 | } |
55 | 54 | for (MessageHistory history : list) { |
56 | - messageCenterBizService.sendMessage(history); | |
55 | + CompletableFuture.runAsync(() -> messageCenterBizService.sendMessage(history)); | |
57 | 56 | } |
58 | 57 | } |
59 | 58 | } | ... | ... |
fw-hestia-server/src/main/resources/application-gray.yml
... | ... | @@ -8,7 +8,7 @@ spring: |
8 | 8 | version: 1.0 |
9 | 9 | server-addr: 192.168.0.91:8848 |
10 | 10 | datasource: |
11 | - url: jdbc:mysql://192.168.0.102:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false | |
11 | + url: jdbc:mysql://192.168.0.102:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true | |
12 | 12 | username: root |
13 | 13 | password: Feewee_Data2_Root123 |
14 | 14 | redis: | ... | ... |
fw-hestia-server/src/main/resources/application-prd.yml
... | ... | @@ -8,7 +8,7 @@ spring: |
8 | 8 | version: 1.0 |
9 | 9 | server-addr: 192.168.0.91:8848 |
10 | 10 | datasource: |
11 | - url: jdbc:mysql://192.168.0.102:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false | |
11 | + url: jdbc:mysql://192.168.0.102:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true | |
12 | 12 | username: root |
13 | 13 | password: Feewee_Data2_Root123 |
14 | 14 | redis: | ... | ... |
fw-hestia-server/src/main/resources/application-test.yml
... | ... | @@ -9,7 +9,7 @@ spring: |
9 | 9 | namespace: 0ba6d5d6-2a49-4086-b6c3-dbeec01c61a4 |
10 | 10 | datasource: |
11 | 11 | password: mysql@pwd123 |
12 | - url: jdbc:mysql://192.168.0.9:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false | |
12 | + url: jdbc:mysql://192.168.0.9:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true | |
13 | 13 | username: test |
14 | 14 | redis: |
15 | 15 | host: 192.168.0.9 | ... | ... |
fw-hestia-server/src/main/resources/application.yml
... | ... | @@ -7,7 +7,10 @@ spring: |
7 | 7 | locker: |
8 | 8 | key-prefix: 'hestia:locker' |
9 | 9 | custom: |
10 | - global-prefix: 'hestia' | |
10 | + global-prefix: 'hestia:global' | |
11 | + global: | |
12 | + ttl: 30000 | |
13 | + key-prefix: 'hestia:spring-cache' | |
11 | 14 | |
12 | 15 | cloud: |
13 | 16 | nacos: |
... | ... | @@ -19,8 +22,9 @@ spring: |
19 | 22 | connection-test-query: SELECT 1 FROM DUAL |
20 | 23 | maximum-pool-size: 100 |
21 | 24 | password: mysql@pwd123 |
22 | - url: jdbc:mysql://192.168.0.234:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false | |
25 | + url: jdbc:mysql://192.168.0.234:3306/fw_hestia?characterEncoding=UTF-8&useSSL=false&rewriteBatchedStatements=true | |
23 | 26 | username: root |
27 | + | |
24 | 28 | jackson: |
25 | 29 | default-property-inclusion: non_null |
26 | 30 | serialization: | ... | ... |
fw-hestia-service/src/main/java/cn/fw/hestia/service/buz/MessageCenterBizService.java
... | ... | @@ -26,14 +26,18 @@ import lombok.Getter; |
26 | 26 | import lombok.RequiredArgsConstructor; |
27 | 27 | import lombok.extern.slf4j.Slf4j; |
28 | 28 | import org.springframework.beans.factory.annotation.Value; |
29 | +import org.springframework.cache.annotation.Cacheable; | |
30 | +import org.springframework.data.redis.core.BoundSetOperations; | |
29 | 31 | import org.springframework.data.redis.core.BoundValueOperations; |
30 | 32 | import org.springframework.data.redis.core.StringRedisTemplate; |
31 | 33 | import org.springframework.stereotype.Service; |
32 | 34 | import org.springframework.transaction.annotation.Transactional; |
33 | 35 | import org.springframework.util.CollectionUtils; |
34 | 36 | |
37 | +import java.time.LocalDate; | |
35 | 38 | import java.time.LocalDateTime; |
36 | 39 | import java.util.*; |
40 | +import java.util.concurrent.TimeUnit; | |
37 | 41 | import java.util.function.Consumer; |
38 | 42 | import java.util.stream.Collectors; |
39 | 43 | |
... | ... | @@ -60,6 +64,9 @@ public class MessageCenterBizService { |
60 | 64 | @Getter |
61 | 65 | private String keyPrefix; |
62 | 66 | |
67 | + @Value("${spring.cache.custom.global-prefix}:template:send:scene:") | |
68 | + private String sendDataPrefix; | |
69 | + | |
63 | 70 | /** |
64 | 71 | * 发送 |
65 | 72 | * |
... | ... | @@ -98,7 +105,18 @@ public class MessageCenterBizService { |
98 | 105 | */ |
99 | 106 | @Transactional(rollbackFor = Exception.class) |
100 | 107 | public void sendMessage(MessageHistory history) { |
101 | - String result = saveLog(createTmParam(history), history.getId(), r -> r.setManual(Boolean.FALSE)); | |
108 | + String key = getSendDataPrefix(); | |
109 | + final BoundSetOperations<String, String> setOps = redisTemplate.boundSetOps(key); | |
110 | + final String seanceToken = String.valueOf(history.getId()); | |
111 | + if (Boolean.TRUE.equals(setOps.isMember(seanceToken))) { | |
112 | + return; | |
113 | + } | |
114 | + String result = saveLog(createTmParam(history), history.getId(), r -> { | |
115 | + r.setManual(Boolean.FALSE); | |
116 | + if (Boolean.TRUE.equals(r.getSucceed())) { | |
117 | + setOps.add(seanceToken); | |
118 | + } | |
119 | + }); | |
102 | 120 | boolean succeed = MessageConstant.SUCCEED_STR.equals(result); |
103 | 121 | final int frequency = history.getFrequency() + 1; |
104 | 122 | history.setFrequency(frequency); |
... | ... | @@ -106,6 +124,7 @@ public class MessageCenterBizService { |
106 | 124 | history.setState(MessageStateEnum.SUMI); |
107 | 125 | //发送成功发送mq |
108 | 126 | sendMsgProducer.send(new MessageSendMq(history.getId(), history.getMemberId(), new Date())); |
127 | + redisTemplate.expire(key, 24L, TimeUnit.HOURS); | |
109 | 128 | } else { |
110 | 129 | history.setSendTime(DateUtil.getExpired(history.getSendTime(), 1 << frequency, Calendar.MINUTE)); |
111 | 130 | } |
... | ... | @@ -134,6 +153,7 @@ public class MessageCenterBizService { |
134 | 153 | * @param memberId |
135 | 154 | * @return |
136 | 155 | */ |
156 | + @Cacheable(cacheNames = "UnreadCount", key = "':unread-count:' + #memberId", condition = "#result != 0") | |
137 | 157 | public int unreadCount(Long memberId) { |
138 | 158 | return messageHistoryService.count(Wrappers.<MessageHistory>lambdaQuery() |
139 | 159 | .eq(MessageHistory::getMemberId, memberId) |
... | ... | @@ -149,6 +169,7 @@ public class MessageCenterBizService { |
149 | 169 | * @param sceneToken |
150 | 170 | * @return |
151 | 171 | */ |
172 | + @Cacheable(cacheNames = "ExchangePageParam", key = "':Exchange-Page-Param:' + #sceneToken", condition = "#result != null") | |
152 | 173 | public Map<String, String> queryPageParams(Long sceneToken) { |
153 | 174 | MessageHistory history = messageHistoryService.getById(sceneToken); |
154 | 175 | BV.notNull(history, () -> "消息不存在或者已经被撤销"); |
... | ... | @@ -170,7 +191,7 @@ public class MessageCenterBizService { |
170 | 191 | PageData<MessageHistory> pageData = messageHistoryService.page(new PageData<>(query), Wrappers.<MessageHistory>lambdaQuery() |
171 | 192 | .eq(MessageHistory::getMemberId, query.getMemberId()) |
172 | 193 | .eq(MessageHistory::getYn, Boolean.TRUE) |
173 | - .ge(MessageHistory::getSendTime, DateUtil.localDateTime2Date(LocalDateTime.now().minusMonths(3L))) | |
194 | + .ge(MessageHistory::getSendTime, DateUtil.localDateTime2Date(LocalDate.now().minusMonths(3L).atStartOfDay())) | |
174 | 195 | .orderByDesc(MessageHistory::getCreateTime) |
175 | 196 | ); |
176 | 197 | AppPage<MessageHistoryVO> page = AppPage.empty(query); |
... | ... | @@ -318,4 +339,9 @@ public class MessageCenterBizService { |
318 | 339 | } |
319 | 340 | return templateCode; |
320 | 341 | } |
342 | + | |
343 | + private String getSendDataPrefix() { | |
344 | + String today = LocalDate.now().toString(); | |
345 | + return this.sendDataPrefix + today; | |
346 | + } | |
321 | 347 | } | ... | ... |