diff --git a/src/main/java/com/lh/mapper/VoterMapper.java b/src/main/java/com/lh/mapper/VoterMapper.java index 923106d..09fd863 100644 --- a/src/main/java/com/lh/mapper/VoterMapper.java +++ b/src/main/java/com/lh/mapper/VoterMapper.java @@ -9,7 +9,7 @@ import java.util.List; @Mapper public interface VoterMapper { // 查询用户今日投票信息 - List countVotesToday(@Param("jwcode") String jwcode); + //List countVotesToday(@Param("jwcode") String jwcode); //插入投票记录 void insertVote(@Param("jwcode") String jwcode, @Param("candidateJwcode") String candidateJwcode,@Param("name") String namne); //根据候选人的jwcode查询投票记录 diff --git a/src/main/java/com/lh/service/VoteServiceImpl.java b/src/main/java/com/lh/service/VoteServiceImpl.java index 1acea68..960ce0d 100644 --- a/src/main/java/com/lh/service/VoteServiceImpl.java +++ b/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 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 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 getVotesByCandidate(String candidateJwcode) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 91fe0c7..73cb933 100644 --- a/src/main/resources/application.properties +++ b/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信息 diff --git a/src/main/resources/com/lh/mapper/VoterMapper.xml b/src/main/resources/com/lh/mapper/VoterMapper.xml index a2a999a..53a13de 100644 --- a/src/main/resources/com/lh/mapper/VoterMapper.xml +++ b/src/main/resources/com/lh/mapper/VoterMapper.xml @@ -5,9 +5,9 @@ INSERT INTO voters(jwcode, candidate_jwcode, name) VALUES(#{jwcode}, #{candidateJwcode}, #{name}) - + + + diff --git a/src/test/java/com/lh/VoteSystemApplicationTests.java b/src/test/java/com/lh/VoteSystemApplicationTests.java index 5e19ae6..c87f29d 100644 --- a/src/test/java/com/lh/VoteSystemApplicationTests.java +++ b/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() {