Browse Source

redis优化

lh_vote_java
lenghui 5 months ago
parent
commit
2e7e3f10de
  1. 2
      src/main/java/com/lh/mapper/VoterMapper.java
  2. 34
      src/main/java/com/lh/service/VoteServiceImpl.java
  3. 7
      src/main/resources/application.properties
  4. 6
      src/main/resources/com/lh/mapper/VoterMapper.xml
  5. 2
      src/test/java/com/lh/VoteSystemApplicationTests.java

2
src/main/java/com/lh/mapper/VoterMapper.java

@ -9,7 +9,7 @@ import java.util.List;
@Mapper
public interface VoterMapper {
// 查询用户今日投票信息
List<Voter> countVotesToday(@Param("jwcode") String jwcode);
//List<Voter> countVotesToday(@Param("jwcode") String jwcode);
//插入投票记录
void insertVote(@Param("jwcode") String jwcode, @Param("candidateJwcode") String candidateJwcode,@Param("name") String namne);
//根据候选人的jwcode查询投票记录

34
src/main/java/com/lh/service/VoteServiceImpl.java

@ -70,33 +70,33 @@ public class VoteServiceImpl implements VoteService {
}
// 2. 增加候选人的投票数Hash 表和 ZSet 同步更新
// 3. 增加候选人的投票数Hash 表和 ZSet 同步更新
String candidateKey = "candidate:" + candidateJwcode;
// 使用 Redis Hash 增加候选人投票数
// 4.使用 Redis Hash 增加候选人投票数
redisTemplate.opsForHash().increment(candidateKey, "votes", 1);
// 使用 Redis ZSet 增加候选人投票数
// 5.使用 Redis ZSet 增加候选人投票数
redisTemplate.opsForZSet().incrementScore("candidate:votes", candidateJwcode, 1);
// 6. 更新 Redis 中的投票次数
redisTemplate.opsForValue().increment(redisKey, 1);
// 设置 Redis 键的过期时间为当天的23:59:59
// 7.设置 Redis 键的过期时间为当天的23:59:59
LocalDateTime now = LocalDateTime.now();
LocalDateTime endOfDay = now.toLocalDate().atTime(23, 59, 59);
long secondsUntilEndOfDay = Duration.between(now, endOfDay).getSeconds();
redisTemplate.expire(redisKey, secondsUntilEndOfDay, TimeUnit.SECONDS);
//更新投票重复键
//8.更新投票重复键
stringRedisTemplate.opsForValue().set(voteStatusKey, "true", secondsUntilEndOfDay, TimeUnit.SECONDS);
//打印剩余长时间过期
//9.控制台打印剩余长时间过期
System.out.println("Redis键" + redisKey + "将在" + secondsUntilEndOfDay + "秒后过期。");
System.out.println("Redis键" + voteStatusKey + "将在" + secondsUntilEndOfDay + "秒后过期。");
//将投票请求发送到 Kafka 消息队列
//10.将投票请求发送到 Kafka 消息队列
voteProducer.sendVoteMessage(new VoteMessage(voterJwcode,
candidateJwcode, voterName,
Timestamp.valueOf(LocalDateTime.now()).toString()));
return 2-voteCountToday;
return dailyVoteLimit - voteCountToday - 1;
}
//获取所有候选人
@ -117,21 +117,25 @@ public class VoteServiceImpl implements VoteService {
candidateList.add(candidate);
}
// 插入投票记录 List 插入是否投过票的状态
for (Candidate candidate : candidateList) {
List<Voter> voters = voterMapper.countVotesToday(voterJwcode);
for (Voter voter : voters) {
if (voter.getCandidateJwCode().equals(candidate.getJwCode())) {
candidate.setVoted(true);
break;
//从redis查询该用户投票状态
String voteStatusKey = "vote_status:" + voterJwcode + ":";
//获取stringRedisTemplate的所有键值
Set<String> redisCandidateJwCode = stringRedisTemplate.keys(voteStatusKey + "*");
//提取最后5位
if (redisCandidateJwCode != null) {
for (String redisKey : redisCandidateJwCode) {
String candidateJwCode = redisKey.substring(redisKey.length() - 5);
if (candidate.getJwCode().equals(candidateJwCode)) {
candidate.setVoted(true);
}
}
}
}
return candidateList;
}
//获取候选人被投票记录
@Override
public List<Voter> getVotesByCandidate(String candidateJwcode) {

7
src/main/resources/application.properties

@ -3,6 +3,13 @@ spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/vote_system?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
# HikariCP连接池配置
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.pool-name=HwgoldHikariCP
# mybatis配置
# 打印log信息

6
src/main/resources/com/lh/mapper/VoterMapper.xml

@ -5,9 +5,9 @@
INSERT INTO voters(jwcode, candidate_jwcode, name)
VALUES(#{jwcode}, #{candidateJwcode}, #{name})
</insert>
<select id="countVotesToday" resultType="com.lh.bean.Voter">
SELECT * FROM voters WHERE jwcode = #{jwcode} AND DATE(vote_time) = CURDATE()
</select>
<!--<select id="countVotesToday" resultType="com.lh.bean.Voter">-->
<!-- SELECT * FROM voters WHERE jwcode = #{jwcode} AND DATE(vote_time) = CURDATE()-->
<!--</select>-->
<select id="getVotesByCandidate" resultType="com.lh.bean.Voter">
SELECT * FROM voters WHERE candidate_jwcode = #{candidateJwcode}
</select>

2
src/test/java/com/lh/VoteSystemApplicationTests.java

@ -16,7 +16,7 @@ class VoteSystemApplicationTests {
@Test
void contextLoads() {
//candidatesMapper.getCandidates().forEach(System.out::println);
System.out.print( voterMapper.countVotesToday("10010"));
//System.out.print( voterMapper.countVotesToday("10010"));
}
@Test
void contextLoads1() {

Loading…
Cancel
Save