ÎļþÃû´Ó Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/redis/RedisService.java ÐÞ¸Ä |
| | |
| | | package com.vci.web.redis; |
| | | package com.vci.starter.web.redis; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | 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.*; |
| | |
| | | * @author dangsn |
| | | **/ |
| | | @SuppressWarnings(value = { "unchecked", "rawtypes" }) |
| | | @Component |
| | | @Service |
| | | @Slf4j |
| | | public class RedisService |
| | | { |
| | | public class RedisService { |
| | | @Autowired(required = false) |
| | | public RedisTemplate redisTemplate; |
| | | |
| | |
| | | /** |
| | | * ç¨æ·ç»å½ |
| | | * @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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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()); |
| | | } |
| | | |
| | | /** |