public User doUserLogin(User user) throws Exception { User loginUser = new User(); try { HashOperations<String,Object,Object> hashOperations = redisTemplate.opsForHash(); Map<Object, Object> userMap = hashOperations.entries(user.getUserName()); System.out.println("userpassword:" + user.getPassword()); System.out.println("userpassword:" + userMap.get("password")); if (user.getPassword().equals(userMap.get("password")) && user.getVerifyCode().equals(userMap.get("verifyCode"))) { //登陆成功返回用户名 System.out.println("登陆成功"); loginUser.setUserName((String) userMap.get("userName")); loginUser.setNickName((String) userMap.get("nickName")); //登陆成功清除验证码 hashOperations.put(loginUser.getUserName(),"verifyCode",""); } }catch(Exception e){ //发生错误时强制关闭实例 e.printStackTrace(); } return loginUser; }
@Override public List<ShieldStatusEntity> getGroupPush(long groupId, List<String> userIdList) { final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, groupId, RedisKeys.SETTING_INFO); HashOperations<String, String, String> groupMapOps = redisTemplate.opsForHash(); List<String> statusList = groupMapOps.multiGet(groupSetKey, userIdList); List<ShieldStatusEntity> shieldStatusList = new ArrayList<>(); for (int i = 0; i < userIdList.size(); i++) { String status = statusList.get(i); String userId = userIdList.get(i); ShieldStatusEntity shieldStatus = new ShieldStatusEntity(); shieldStatus.setUserId(Long.valueOf(userId)); shieldStatus.setShieldStatus(status == null?DBConstant.GROUP_STATUS_ONLINE: DBConstant.GROUP_STATUS_SHIELD); shieldStatus.setUserToken(userTokenService.getToken(userId)); shieldStatusList.add(shieldStatus); } return shieldStatusList; }
/** * 消息用户计数 * @param userCountReq 会话信息 * @return 更新结果 * @since 1.0 */ @PostMapping("/clearUserCounter") public BaseModel<?> clearUserCounter(@RequestBody ClearUserCountReq userCountReq) { HashOperations<String, String, String> hashOptions = redisTemplate.opsForHash(); if (userCountReq.getSessionType() == IMBaseDefine.SessionType.SESSION_TYPE_SINGLE) { // Clear P2P msg Counter final String userKey = RedisKeys.concat(RedisKeys.USER_UNREAD, userCountReq.getUserId()); hashOptions.delete(userKey, String.valueOf(userCountReq.getPeerId())); } else if (userCountReq.getSessionType() == IMBaseDefine.SessionType.SESSION_TYPE_GROUP) { // Clear Group msg Counter final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, userCountReq.getPeerId(), RedisKeys.SETTING_INFO); final String countValue = hashOptions.get(groupSetKey, RedisKeys.COUNT); final String userUnreadKey = RedisKeys.concat(RedisKeys.GROUP_UNREAD, userCountReq.getUserId()); hashOptions.put(userUnreadKey, String.valueOf(userCountReq.getPeerId()), countValue); } else { logger.warn("参数不正: SessionType={}", userCountReq.getSessionType()); } return null; }
@Test public void testObj() throws Exception { AccountEntity accountEntity = accountService.getAccount(1); HashOperations<String, String, String> operations=redisTemplate.opsForHash(); operations.put("demo:account",String.valueOf(accountEntity.getId()), JSON.toJSONString(accountEntity)); Thread.sleep(1000); boolean exists=redisTemplate.hasKey("demo:account"); Assert.assertTrue("不存在",exists); AccountEntity accountEntityCache =JSON.parseObject(String.valueOf(operations.get("demo:account","1")) ,AccountEntity.class); Assert.assertEquals("json",accountEntityCache.getNickName()); }
@Override public boolean isValidate(long groupId, long userId) { String key = RedisKeys.concat(RedisKeys.GROUP_INFO, groupId); if (redisTemplate.hasKey(key)) { HashOperations<String, String, String> groupMemberHash = redisTemplate.opsForHash(); groupMemberHash.hasKey(key, String.valueOf(userId)); } return false; }
@Override public String getToken(long userId) { HashOperations<String, String, String> opsHash = redisTemplate.opsForHash(); String key = RedisKeys.concat(RedisKeys.USER_INFO, userId); return opsHash.get(key, RedisKeys.USER_TOKEN); }
@Override public String getToken(String userId) { HashOperations<String, String, String> opsHash = redisTemplate.opsForHash(); String key = RedisKeys.concat(RedisKeys.USER_INFO, userId); return opsHash.get(key, RedisKeys.USER_TOKEN); }
/** * 查询组的属性 * @param groupVersionList * @return 查询结果:群资料(版本) * @since 1.0 */ @GetMapping(path = "/groupInfoList") public BaseModel<List<GroupEntity>> groupInfoList(@RequestParam("groupIdList") List<Long> groupIdList) { SearchCriteria<IMGroup> groupSearchCriteria = new SearchCriteria<>(); groupSearchCriteria.add(JpaRestrictions.in("id", groupIdList, false)); Sort sort = new Sort(Sort.Direction.DESC, "updated"); List<IMGroup> groups = groupRepository.findAll(groupSearchCriteria, sort); List<GroupEntity> resData = new ArrayList<>(); for (IMGroup group: groups) { GroupEntity groupEntity = new GroupEntity(); groupEntity.setId(group.getId()); groupEntity.setAvatar(group.getAvatar()); groupEntity.setCreated(group.getCreated()); groupEntity.setCreatorId(group.getCreator()); groupEntity.setGroupType(group.getType()); groupEntity.setStatus(group.getStatus()); groupEntity.setMainName(group.getName()); groupEntity.setVersion(group.getVersion()); resData.add(groupEntity); // fillGroupMember String key = RedisKeys.concat(RedisKeys.GROUP_INFO, group.getId()); HashOperations<String, String, String> groupMapOps = redisTemplate.opsForHash(); Map<String, String> groupMemberMap = groupMapOps.entries(key); List<Long> userIds = new ArrayList<>(); if (groupMemberMap != null) { for (String memberId : groupMemberMap.keySet()) { userIds.add(Long.valueOf(memberId)); } } groupEntity.setUserList(userIds); } BaseModel<List<GroupEntity>> groupRes = new BaseModel<>(); groupRes.setData(resData); return groupRes; }
/** * 查询组的属性 * @param groupVersionList * @return 查询结果:群资料(版本) * @since 1.0 */ @GetMapping(path = "/groupPushInfo") @Transactional(readOnly = true) public BaseModel<GroupPushEntity> getGroupPushInfo(@RequestParam("groupId") Long groupId) { BaseModel<GroupPushEntity> groupRes = new BaseModel<>(); IMGroup group = groupRepository.findOne(groupId); if (group != null) { GroupPushEntity groupEntity = new GroupPushEntity(); groupEntity.setId(group.getId()); groupEntity.setAvatar(group.getAvatar()); groupEntity.setCreated(group.getCreated()); groupEntity.setCreatorId(group.getCreator()); groupEntity.setGroupType(group.getType()); groupEntity.setStatus(group.getStatus()); groupEntity.setMainName(group.getName()); groupEntity.setVersion(group.getVersion()); // fillGroupMember String key = RedisKeys.concat(RedisKeys.GROUP_INFO, group.getId()); HashOperations<String, String, String> groupMapOps = redisTemplate.opsForHash(); Map<String, String> groupMemberMap = groupMapOps.entries(key); List<String> userIdList = new ArrayList<>(); if (groupMemberMap != null) { for (String memberId : groupMemberMap.keySet()) { userIdList.add(memberId); } } groupRes.setData(groupEntity); // push shield status List<ShieldStatusEntity> shieldStatusList = groupInternalService.getGroupPush(groupId, userIdList); groupEntity.setUserList(shieldStatusList); } else { groupRes.setCode(1); } return groupRes; }
@PostMapping(path = "/login/pushShield") public BaseModel<Integer> pushShield(@RequestParam("userId") long userId, @RequestParam("shieldStatus") int shieldStatus) { String key = RedisKeys.concat(RedisKeys.USER_INFO, userId); HashOperations<String, String, String> userMapOps = redisTemplate.opsForHash(); userMapOps.put(key, RedisKeys.USER_SHIELD, String.valueOf(shieldStatus)); return new BaseModel<Integer>(); }
@GetMapping(path = "/login/queryPushShield") public BaseModel<Integer> queryPushShield(@RequestParam("userId") long userId) { String key = RedisKeys.concat(RedisKeys.USER_INFO, userId); HashOperations<String, String, String> userMapOps = redisTemplate.opsForHash(); String shieldStatus = userMapOps.get(key, RedisKeys.USER_SHIELD); BaseModel<Integer> res = new BaseModel<Integer>(); if (shieldStatus != null) { res.setData(Integer.valueOf(shieldStatus)); } return res; }
@Override public <HK, HV> HashOperations<K, HK, HV> opsForHash() { try { return redisTemplate.opsForHash(); } catch (Exception ex) { throw new RedisBaoException(ex); } }
private HashOperations<String, Object, Object> getHashOps() { return redisTemplate.opsForHash(); }
@Override public void putHash(final String key, final Object hashKey, final Object hashValue) { final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash(); operation.put(getKey(key), hashKey, hashValue); }
@Override public void putAllHash(final String key, final Map<? extends Object, ? extends Object> keyValue) { final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash(); operation.putAll(getKey(key), keyValue); }
@Override public Object getHash(final String key, final String hashKey) { final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash(); final Object value = operation.get(getKey(key), hashKey); return value; }
@Override public Map<Object, Object> entries(final String key) { final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash(); return operation.entries(getKey(key)); }
@Override public void delete(final String key, final Object... hashKeys) { final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash(); operation.delete(getKey(key), hashKeys); }
@Override public void setGroupPush(long groupId, long userId, int shieldStatus) { final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, groupId, RedisKeys.SETTING_INFO); HashOperations<String, String, String> groupMapOps = redisTemplate.opsForHash(); groupMapOps.put(groupSetKey, String.valueOf(userId), String.valueOf(shieldStatus)); }
/** * 查询组的属性 * @param groupVersionList * @return 查询结果:群资料 * @since 1.0 */ @PostMapping(path = "/infoList") @Transactional(readOnly = true) public BaseModel<List<GroupEntity>> groupInfoList(@RequestBody Map<String, Integer> groupIdList) { List<Long> groupIds = new ArrayList<>(); for (String id: groupIdList.keySet()) { groupIds.add(Long.valueOf(id)); } SearchCriteria<IMGroup> groupSearchCriteria = new SearchCriteria<>(); groupSearchCriteria.add(JpaRestrictions.in("id", groupIds, false)); Sort sort = new Sort(Sort.Direction.DESC, "updated"); List<IMGroup> groups = groupRepository.findAll(groupSearchCriteria, sort); List<GroupEntity> resData = new ArrayList<>(); for (IMGroup group: groups) { int version = groupIdList.get(String.valueOf(group.getId())); if (version < group.getVersion()) { GroupEntity groupEntity = new GroupEntity(); groupEntity.setId(group.getId()); groupEntity.setAvatar(group.getAvatar()); groupEntity.setCreated(group.getCreated()); groupEntity.setCreatorId(group.getCreator()); groupEntity.setGroupType(group.getType()); groupEntity.setStatus(group.getStatus()); groupEntity.setMainName(group.getName()); groupEntity.setVersion(group.getVersion()); resData.add(groupEntity); // fillGroupMember String key = RedisKeys.concat(RedisKeys.GROUP_INFO, group.getId()); HashOperations<String, String, String> groupMapOps = redisTemplate.opsForHash(); Map<String, String> groupMemberMap = groupMapOps.entries(key); List<Long> userIds = new ArrayList<>(); if (groupMemberMap != null) { for (String memberId : groupMemberMap.keySet()) { userIds.add(Long.valueOf(memberId)); } } groupEntity.setUserList(userIds); } } BaseModel<List<GroupEntity>> groupRes = new BaseModel<>(); groupRes.setData(resData); return groupRes; }
@Override public <HK, HV> HashOperations<K, HK, HV> opsForHash() { int dbIndex = RedisApplication.redisConnectionDbIndex.get(); return new DefaultHashOperations<K, HK, HV>(this, dbIndex); }
public HashOperations<String, String, String> hashOps() { return redisTemplate.opsForHash(); }
@Before public void setUp() throws Exception { redisTemplate = mock(RedisTemplate.class); hashOperations = mock(HashOperations.class); super.setUp(); }
/** * 哈希 添加 * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash(); hash.put(key, hashKey, value); }
/** * 哈希获取数据 * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash(); return hash.get(key, hashKey); }
/** * Ops for hash hash operations. * * @return the hash operations */ public HashOperations<String, String, String> opsForHash() { return redisTemplate.opsForHash(); }
public HashOperations<String, Object, Object> opsForHash(){ return redisTemplate.opsForHash(); }
<HK, HV> HashOperations<K, HK, HV> opsForHash();