Java Code Examples for org.springframework.data.redis.core.ZSetOperations#TypedTuple

The following examples show how to use org.springframework.data.redis.core.ZSetOperations#TypedTuple . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisDelayBucket.java    From Milkomeda with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public DelayJob poll(Integer index) {
    String name = bucketNames.get(index);
    BoundZSetOperations<String, String> bucket = getBucket(name);
    // 升序查第一个(最上面的是延迟/TTR过期的)
    Set<ZSetOperations.TypedTuple<String>> set = bucket.rangeWithScores(0, 1);
    if (CollectionUtils.isEmpty(set)) {
        return null;
    }
    ZSetOperations.TypedTuple<String> typedTuple = set.toArray(new ZSetOperations.TypedTuple[]{})[0];
    if (typedTuple.getValue() == null) {
        return null;
    }
    return DelayJob.compatibleDecode(typedTuple.getValue(), typedTuple.getScore());
}
 
Example 2
Source File: JobOperationServiceImpl.java    From mykit-delay with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getBucketTopJobs(String bucketName, int size) {
    double                                 to   = Long.valueOf(System.currentTimeMillis() + BucketTask.TIME_OUT).doubleValue();
    Set<ZSetOperations.TypedTuple<String>> sets = redisSupport.zrangeByScoreWithScores(bucketName, 0, to, 0, size);
    List<String>                           lsts = Lists.newArrayList();
    if (sets != null && sets.size() > 0) {
        Iterator<ZSetOperations.TypedTuple<String>> it = sets.iterator();
        while (it.hasNext()) {
            ZSetOperations.TypedTuple<String> curr = it.next();
            if (curr.getScore() <= System.currentTimeMillis()) {
                lsts.add(curr.getValue());
            } else {
                break;
            }
        }
        // String jobMsgId = Objects.toString(sets.toArray()[0]);
        return lsts;
    }
    return null;
}
 
Example 3
Source File: AnswerService.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
/**
    * 获取排名数据
    * @param recordId recordId
    * @return List
    * @author tangyi
    * @date 2019/12/8 23:36
    */
public List<RankInfoDto> getRankInfo(Long recordId) {
	List<RankInfoDto> rankInfos = new ArrayList<>();
	// 查询缓存
	Set<ZSetOperations.TypedTuple<String>> typedTuples = redisTemplate.opsForZSet()
			.reverseRangeByScoreWithScores(AnswerConstant.CACHE_PREFIX_RANK + recordId, 0, Integer.MAX_VALUE);
	if (typedTuples != null) {
		// 用户ID列表
		Set<Long> userIds = new HashSet<>();
		typedTuples.forEach(typedTuple -> {
			ExaminationRecord record = JsonMapper.getInstance()
					.fromJson(typedTuple.getValue(), ExaminationRecord.class);
			if (record != null) {
				RankInfoDto rankInfo = new RankInfoDto();
				rankInfo.setUserId(record.getUserId());
				userIds.add(record.getUserId());
				rankInfo.setScore(typedTuple.getScore());
				rankInfos.add(rankInfo);
			}
		});
		if (!userIds.isEmpty()) {
			ResponseBean<List<UserVo>> userResponse = userServiceClient.findUserById(userIds.toArray(new Long[0]));
			if (ResponseUtil.isSuccess(userResponse)) {
				rankInfos.forEach(rankInfo -> {
					userResponse.getData().stream().filter(user -> user.getId().equals(rankInfo.getUserId()))
							.findFirst().ifPresent(user -> {
						// 设置考生信息
						rankInfo.setName(user.getName());
						rankInfo.setAvatarUrl(user.getAvatarUrl());
					});
				});
			}
		}
	}
	return rankInfos;
}
 
Example 4
Source File: JobOperationServiceImpl.java    From sdmq with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> getBucketTopJobs(String bucketName, int size) {
    double                                 to   = Long.valueOf(System.currentTimeMillis() + BucketTask.TIME_OUT).doubleValue();
    Set<ZSetOperations.TypedTuple<String>> sets = redisSupport.zrangeByScoreWithScores(bucketName, 0, to, 0, size);
    List<String>                           lsts = Lists.newArrayList();
    if (sets != null && sets.size() > 0) {
        Iterator<ZSetOperations.TypedTuple<String>> it = sets.iterator();
        while (it.hasNext()) {
            ZSetOperations.TypedTuple<String> curr = it.next();
            if (curr.getScore() <= System.currentTimeMillis()) {
                lsts.add(curr.getValue());
            } else {
                break;
            }
        }
        // String jobMsgId = Objects.toString(sets.toArray()[0]);
        return lsts;
    }
    return null;
}
 
Example 5
Source File: RedisDelayBucket.java    From Milkomeda with MIT License 5 votes vote down vote up
@Override
public void add(List<DelayJob> delayJobs) {
    String bucketName = getCurrentBucketName();
    BoundZSetOperations<String, String> bucket = getBucket(bucketName);
    Set<ZSetOperations.TypedTuple<String>> delayJobSet = delayJobs.stream()
            .map(delayJob -> new DefaultTypedTuple<>(delayJob.toSimple(), (double) delayJob.getDelayTime()))
            .collect(Collectors.toSet());
    bucket.add(delayJobSet);
}
 
Example 6
Source File: HotPostService.java    From expper with GNU General Public License v3.0 5 votes vote down vote up
public void sortByScore(List<Post> posts, Set<ZSetOperations.TypedTuple<Long>> scores) {
    Map<Long, Double> map = new HashMap<>();
    scores.forEach(s -> map.put(s.getValue(), s.getScore()));

    posts.sort((o1, o2) -> {
        double diff = map.get(o2.getId()) - map.get(o1.getId());
        return diff > 0 ? 1 : (diff < 0 ? -1 : 0);
    });
}
 
Example 7
Source File: RedisSupport.java    From mykit-delay with Apache License 2.0 4 votes vote down vote up
public Set<ZSetOperations.TypedTuple<String>> zrangeByScoreWithScores(String key, double min, double max, int offset,
                                                                      int count) {
    ZSetOperations<String, String>         zset = template.opsForZSet();
    Set<ZSetOperations.TypedTuple<String>> set  = zset.rangeByScoreWithScores(key, min, max, offset, count);
    return set;
}
 
Example 8
Source File: RedisSupport.java    From sdmq with Apache License 2.0 4 votes vote down vote up
public Set<ZSetOperations.TypedTuple<String>> zrangeByScoreWithScores(String key, double min, double max, int offset,
                                                                      int count) {
    ZSetOperations<String, String>         zset = template.opsForZSet();
    Set<ZSetOperations.TypedTuple<String>> set  = zset.rangeByScoreWithScores(key, min, max, offset, count);
    return set;
}
 
Example 9
Source File: PostListService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public List<Post> getHotPostsOfPage(int page, int pageSize) throws PageNotFoundException {
    Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScore(page, pageSize);
    return getHotPostsOfPage(page, idsWithScore);
}
 
Example 10
Source File: PostListService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public List<Post> getHotPostsOfPage(int page, int pageSize, Tag tag) throws PageNotFoundException {
    Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScoreOfTag(tag.getId(), page, pageSize);
    return getHotPostsOfPage(page, idsWithScore);
}
 
Example 11
Source File: PostListService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public List<Post> getHotPostsOfPage(int page, int pageSize, Topic topic) throws PageNotFoundException {
    Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScoreOfTopic(topic.getId(), page, pageSize);
    return getHotPostsOfPage(page, idsWithScore);
}
 
Example 12
Source File: HotPostService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScore(int page, int pageSize) {
    return hotPosts.reverseRangeWithScores(CACHE_HOT_POSTS, page * pageSize, (page + 1) * pageSize - 1);
}
 
Example 13
Source File: HotPostService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScoreOfTag(Long tagId, int page, int pageSize) {
    return hotPosts.reverseRangeWithScores(CACHE_HOT_TAG_POSTS + tagId, page * pageSize, (page + 1) * pageSize - 1);
}
 
Example 14
Source File: HotPostService.java    From expper with GNU General Public License v3.0 4 votes vote down vote up
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScoreOfTopic(Long topicId, int page, int pageSize) {
    return hotPosts.reverseRangeWithScores(CACHE_HOT_TOPIC_POSTS + topicId, page * pageSize, (page + 1) * pageSize - 1);
}