作者 crossoverJie

:sparkles: Introducing new features.新增获取在线用户

... ... @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
import java.util.Set;
/**
* Function:
... ... @@ -166,5 +167,26 @@ public class RouteController {
return res;
}
/**
* 注册账号
*
* @return
*/
@ApiOperation("获取所有在线用户")
@RequestMapping(value = "onlineUser", method = RequestMethod.POST)
@ResponseBody()
public BaseResponse<Set<CIMUserInfo>> onlineUser() throws Exception {
BaseResponse<Set<CIMUserInfo>> res = new BaseResponse();
Set<CIMUserInfo> cimUserInfos = userInfoCacheService.onlineUser();
res.setDataBody(cimUserInfos) ;
res.setCode(StatusEnum.SUCCESS.getCode());
res.setMessage(StatusEnum.SUCCESS.getMessage());
return res;
}
}
... ...
... ... @@ -2,6 +2,8 @@ package com.crossoverjie.cim.route.service;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import java.util.Set;
/**
* Function:
*
... ... @@ -17,7 +19,7 @@ public interface UserInfoCacheService {
* @return
* @throws Exception
*/
CIMUserInfo loadUserInfoByUserId(Long userId) throws Exception ;
CIMUserInfo loadUserInfoByUserId(Long userId) ;
/**
* 保存和检查用户登录情况
... ... @@ -35,5 +37,9 @@ public interface UserInfoCacheService {
void removeLoginStatus(Long userId) throws Exception ;
/**
*
* @return 获取所有在线用户
*/
Set<CIMUserInfo> onlineUser() ;
}
... ...
... ... @@ -6,7 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX;
... ... @@ -31,7 +33,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
private RedisTemplate<String,String> redisTemplate ;
@Override
public CIMUserInfo loadUserInfoByUserId(Long userId) throws Exception {
public CIMUserInfo loadUserInfoByUserId(Long userId) {
//优先从本地缓存获取
CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId);
... ... @@ -65,4 +67,19 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService {
redisTemplate.opsForSet().remove(LOGIN_STATUS_PREFIX,userId.toString()) ;
}
@Override
public Set<CIMUserInfo> onlineUser() {
Set<CIMUserInfo> set = null ;
Set<String> members = redisTemplate.opsForSet().members(LOGIN_STATUS_PREFIX);
for (String member : members) {
if (set == null){
set = new HashSet<>(64) ;
}
CIMUserInfo cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ;
set.add(cimUserInfo) ;
}
return set;
}
}
... ...
package com.crossoverjie.cim.route.service.impl;
import com.alibaba.fastjson.JSON;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.route.RouteApplication;
import com.crossoverjie.cim.route.service.UserInfoCacheService;
import org.junit.Test;
... ... @@ -10,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Set;
@SpringBootTest(classes = RouteApplication.class)
@RunWith(SpringRunner.class)
public class UserInfoCacheServiceImplTest {
... ... @@ -31,4 +35,10 @@ public class UserInfoCacheServiceImplTest {
userInfoCacheService.removeLoginStatus(2000L);
}
@Test
public void onlineUser(){
Set<CIMUserInfo> cimUserInfos = userInfoCacheService.onlineUser();
LOGGER.info("cimUserInfos={}", JSON.toJSONString(cimUserInfos));
}
}
\ No newline at end of file
... ...