From 7ccbd7f6fc245ee6a8704140ae7ad09e2509e6c4 Mon Sep 17 00:00:00 2001 From: ludc Date: 星期五, 27 九月 2024 09:46:06 +0800 Subject: [PATCH] redis服务移动到base下,实现超时过期用户减去一个在线用户数 --- Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/redis/RedisService.java | 38 ++++++++++++++++++++++++++------------ 1 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/redis/RedisService.java b/Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/redis/RedisService.java similarity index 91% rename from Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/redis/RedisService.java rename to Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/redis/RedisService.java index 7172e62..b795342 100644 --- a/Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/redis/RedisService.java +++ b/Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/redis/RedisService.java @@ -1,4 +1,4 @@ -package com.vci.web.redis; +package com.vci.starter.web.redis; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -11,6 +11,7 @@ import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.io.IOException; import java.util.*; @@ -21,10 +22,9 @@ * @author dangsn **/ @SuppressWarnings(value = { "unchecked", "rawtypes" }) -@Component +@Service @Slf4j -public class RedisService -{ +public class RedisService { @Autowired(required = false) public RedisTemplate redisTemplate; @@ -393,19 +393,33 @@ /** * 鐢ㄦ埛鐧诲綍 * @param currentLoggedUserKey - * @param username */ - public void userLogin(String currentLoggedUserKey,String username) { - redisTemplate.opsForSet().add(currentLoggedUserKey, username); + public void increOnlineUser(String currentLoggedUserKey) { + // 妫�鏌ラ敭鏄惁瀛樺湪 + if (!redisTemplate.hasKey(currentLoggedUserKey)) { + // 濡傛灉閿笉瀛樺湪锛屽垵濮嬪寲涓�0 + redisTemplate.opsForValue().set(currentLoggedUserKey, 0); + } + // 鑷鍦ㄧ嚎鐢ㄦ埛鏁� + redisTemplate.opsForValue().increment(currentLoggedUserKey); } /** * 鐢ㄦ埛鐧诲嚭 * @param currentLoggedUserKey - * @param username */ - public void userLogout(String currentLoggedUserKey,String username) { - redisTemplate.opsForSet().remove(currentLoggedUserKey, username); + public void decreOnlineUser(String currentLoggedUserKey) { + // 妫�鏌ュ綋鍓嶅湪绾跨敤鎴锋暟 + String currentLoggedUser = (String)redisTemplate.opsForValue().get(currentLoggedUserKey); + + if (currentLoggedUser != null) { + Long currentCount = Long.parseLong((String)redisTemplate.opsForValue().get(currentLoggedUserKey)); + if(currentCount > 0){ + + } + // 鑷噺鍦ㄧ嚎鐢ㄦ埛鏁� + redisTemplate.opsForValue().decrement(currentLoggedUserKey); + } } /** @@ -413,8 +427,8 @@ * @param currentLoggedUserKey * @return */ - public long getCurrentLoggedUserCount(String currentLoggedUserKey) { - return redisTemplate.opsForSet().size(currentLoggedUserKey); + public long getCurrentOnlineUserCount(String currentLoggedUserKey) { + return Long.parseLong(redisTemplate.opsForValue().get(currentLoggedUserKey).toString()); } /** -- Gitblit v1.9.3