正在显示
4 个修改的文件
包含
58 行增加
和
3 行删除
| @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestMethod; | @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestMethod; | ||
| 24 | import org.springframework.web.bind.annotation.ResponseBody; | 24 | import org.springframework.web.bind.annotation.ResponseBody; |
| 25 | 25 | ||
| 26 | import java.util.Map; | 26 | import java.util.Map; |
| 27 | +import java.util.Set; | ||
| 27 | 28 | ||
| 28 | /** | 29 | /** |
| 29 | * Function: | 30 | * Function: |
| @@ -166,5 +167,26 @@ public class RouteController { | @@ -166,5 +167,26 @@ public class RouteController { | ||
| 166 | return res; | 167 | return res; |
| 167 | } | 168 | } |
| 168 | 169 | ||
| 170 | + /** | ||
| 171 | + * 注册账号 | ||
| 172 | + * | ||
| 173 | + * @return | ||
| 174 | + */ | ||
| 175 | + @ApiOperation("获取所有在线用户") | ||
| 176 | + @RequestMapping(value = "onlineUser", method = RequestMethod.POST) | ||
| 177 | + @ResponseBody() | ||
| 178 | + public BaseResponse<Set<CIMUserInfo>> onlineUser() throws Exception { | ||
| 179 | + BaseResponse<Set<CIMUserInfo>> res = new BaseResponse(); | ||
| 180 | + | ||
| 181 | + Set<CIMUserInfo> cimUserInfos = userInfoCacheService.onlineUser(); | ||
| 182 | + res.setDataBody(cimUserInfos) ; | ||
| 183 | + res.setCode(StatusEnum.SUCCESS.getCode()); | ||
| 184 | + res.setMessage(StatusEnum.SUCCESS.getMessage()); | ||
| 185 | + return res; | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + | ||
| 189 | + | ||
| 190 | + | ||
| 169 | 191 | ||
| 170 | } | 192 | } |
| @@ -2,6 +2,8 @@ package com.crossoverjie.cim.route.service; | @@ -2,6 +2,8 @@ package com.crossoverjie.cim.route.service; | ||
| 2 | 2 | ||
| 3 | import com.crossoverjie.cim.common.pojo.CIMUserInfo; | 3 | import com.crossoverjie.cim.common.pojo.CIMUserInfo; |
| 4 | 4 | ||
| 5 | +import java.util.Set; | ||
| 6 | + | ||
| 5 | /** | 7 | /** |
| 6 | * Function: | 8 | * Function: |
| 7 | * | 9 | * |
| @@ -17,7 +19,7 @@ public interface UserInfoCacheService { | @@ -17,7 +19,7 @@ public interface UserInfoCacheService { | ||
| 17 | * @return | 19 | * @return |
| 18 | * @throws Exception | 20 | * @throws Exception |
| 19 | */ | 21 | */ |
| 20 | - CIMUserInfo loadUserInfoByUserId(Long userId) throws Exception ; | 22 | + CIMUserInfo loadUserInfoByUserId(Long userId) ; |
| 21 | 23 | ||
| 22 | /** | 24 | /** |
| 23 | * 保存和检查用户登录情况 | 25 | * 保存和检查用户登录情况 |
| @@ -35,5 +37,9 @@ public interface UserInfoCacheService { | @@ -35,5 +37,9 @@ public interface UserInfoCacheService { | ||
| 35 | void removeLoginStatus(Long userId) throws Exception ; | 37 | void removeLoginStatus(Long userId) throws Exception ; |
| 36 | 38 | ||
| 37 | 39 | ||
| 38 | - | 40 | + /** |
| 41 | + * | ||
| 42 | + * @return 获取所有在线用户 | ||
| 43 | + */ | ||
| 44 | + Set<CIMUserInfo> onlineUser() ; | ||
| 39 | } | 45 | } |
| @@ -6,7 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -6,7 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
| 6 | import org.springframework.data.redis.core.RedisTemplate; | 6 | import org.springframework.data.redis.core.RedisTemplate; |
| 7 | import org.springframework.stereotype.Service; | 7 | import org.springframework.stereotype.Service; |
| 8 | 8 | ||
| 9 | +import java.util.HashSet; | ||
| 9 | import java.util.Map; | 10 | import java.util.Map; |
| 11 | +import java.util.Set; | ||
| 10 | import java.util.concurrent.ConcurrentHashMap; | 12 | import java.util.concurrent.ConcurrentHashMap; |
| 11 | 13 | ||
| 12 | import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX; | 14 | import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX; |
| @@ -31,7 +33,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { | @@ -31,7 +33,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { | ||
| 31 | private RedisTemplate<String,String> redisTemplate ; | 33 | private RedisTemplate<String,String> redisTemplate ; |
| 32 | 34 | ||
| 33 | @Override | 35 | @Override |
| 34 | - public CIMUserInfo loadUserInfoByUserId(Long userId) throws Exception { | 36 | + public CIMUserInfo loadUserInfoByUserId(Long userId) { |
| 35 | 37 | ||
| 36 | //优先从本地缓存获取 | 38 | //优先从本地缓存获取 |
| 37 | CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId); | 39 | CIMUserInfo cimUserInfo = USER_INFO_MAP.get(userId); |
| @@ -65,4 +67,19 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { | @@ -65,4 +67,19 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { | ||
| 65 | redisTemplate.opsForSet().remove(LOGIN_STATUS_PREFIX,userId.toString()) ; | 67 | redisTemplate.opsForSet().remove(LOGIN_STATUS_PREFIX,userId.toString()) ; |
| 66 | } | 68 | } |
| 67 | 69 | ||
| 70 | + @Override | ||
| 71 | + public Set<CIMUserInfo> onlineUser() { | ||
| 72 | + Set<CIMUserInfo> set = null ; | ||
| 73 | + Set<String> members = redisTemplate.opsForSet().members(LOGIN_STATUS_PREFIX); | ||
| 74 | + for (String member : members) { | ||
| 75 | + if (set == null){ | ||
| 76 | + set = new HashSet<>(64) ; | ||
| 77 | + } | ||
| 78 | + CIMUserInfo cimUserInfo = loadUserInfoByUserId(Long.valueOf(member)) ; | ||
| 79 | + set.add(cimUserInfo) ; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + return set; | ||
| 83 | + } | ||
| 84 | + | ||
| 68 | } | 85 | } |
| 1 | package com.crossoverjie.cim.route.service.impl; | 1 | package com.crossoverjie.cim.route.service.impl; |
| 2 | 2 | ||
| 3 | +import com.alibaba.fastjson.JSON; | ||
| 4 | +import com.crossoverjie.cim.common.pojo.CIMUserInfo; | ||
| 3 | import com.crossoverjie.cim.route.RouteApplication; | 5 | import com.crossoverjie.cim.route.RouteApplication; |
| 4 | import com.crossoverjie.cim.route.service.UserInfoCacheService; | 6 | import com.crossoverjie.cim.route.service.UserInfoCacheService; |
| 5 | import org.junit.Test; | 7 | import org.junit.Test; |
| @@ -10,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -10,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | import org.springframework.boot.test.context.SpringBootTest; | 12 | import org.springframework.boot.test.context.SpringBootTest; |
| 11 | import org.springframework.test.context.junit4.SpringRunner; | 13 | import org.springframework.test.context.junit4.SpringRunner; |
| 12 | 14 | ||
| 15 | +import java.util.Set; | ||
| 16 | + | ||
| 13 | @SpringBootTest(classes = RouteApplication.class) | 17 | @SpringBootTest(classes = RouteApplication.class) |
| 14 | @RunWith(SpringRunner.class) | 18 | @RunWith(SpringRunner.class) |
| 15 | public class UserInfoCacheServiceImplTest { | 19 | public class UserInfoCacheServiceImplTest { |
| @@ -31,4 +35,10 @@ public class UserInfoCacheServiceImplTest { | @@ -31,4 +35,10 @@ public class UserInfoCacheServiceImplTest { | ||
| 31 | userInfoCacheService.removeLoginStatus(2000L); | 35 | userInfoCacheService.removeLoginStatus(2000L); |
| 32 | } | 36 | } |
| 33 | 37 | ||
| 38 | + @Test | ||
| 39 | + public void onlineUser(){ | ||
| 40 | + Set<CIMUserInfo> cimUserInfos = userInfoCacheService.onlineUser(); | ||
| 41 | + LOGGER.info("cimUserInfos={}", JSON.toJSONString(cimUserInfos)); | ||
| 42 | + } | ||
| 43 | + | ||
| 34 | } | 44 | } |
-
请 注册 或 登录 后发表评论