ludc
2024-09-27 7ccbd7f6fc245ee6a8704140ae7ad09e2509e6c4
Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/redis/RedisService.java
ÎļþÃû´Ó Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/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());
    }
    /**