Java Code Examples for redis.clients.jedis.JedisCommands#zrevrange()

The following examples show how to use redis.clients.jedis.JedisCommands#zrevrange() . 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: CommonController.java    From feiqu-opensource with Apache License 2.0 5 votes vote down vote up
@PostMapping("findActiveUserNames")
@ResponseBody
public Object findActiveUserNames(){
    BaseResult result = new BaseResult();
    try {int month = DateUtil.thisMonth()+1;
        JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
        String key = "activeUserNames"+month;
        Set<String> activeNames = commands.smembers(key);
        if(CollectionUtil.isEmpty(activeNames)){
            Set<String> userIds =commands.zrevrange(CommonConstant.FQ_ACTIVE_USER_SORT+month,0,9);
            if(CollectionUtils.isNotEmpty(userIds)){
                List<Integer> userIdList = Lists.newArrayList();
                for(String userId : userIds){
                    userIdList.add(Integer.valueOf(userId));
                }
                FqUserExample example = new FqUserExample();
                example.createCriteria().andIdIn(userIdList);
                List<FqUser> fqUsers = fqUserService.selectByExample(example);
                List<String> names = fqUsers.stream().map(FqUser::getNickname).collect(Collectors.toList());
                result.setData(names);
                commands.sadd(key,names.toArray(new String[names.size()]));
                commands.expire(key,7*24*60*60);
            }
        }else {
            result.setData(activeNames);
        }
    } catch (Exception e) {
        logger.error("获取活跃用户信息报错",e);
    } finally {
        JedisProviderFactory.getJedisProvider(null).release();
    }
    return result;
}
 
Example 2
Source File: HotContentJob.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@Scheduled(cron = "0 0/30 9-18 * * ? ")//从9点到18点 每半个小时更新一次
//    @Scheduled(cron = "0 51 15 * * ? ")
    public void work(){
        Stopwatch stopwatch = Stopwatch.createStarted();

        PageHelper.startPage(1, 5 , false);
        ThoughtExample thoughtExample = new ThoughtExample();
        thoughtExample.setOrderByClause("create_time desc");
        thoughtExample.createCriteria().andDelFlagEqualTo(YesNoEnum.NO.getValue());
        List<ThoughtWithUser> newThoughts = thoughtService.getThoughtWithUser(thoughtExample);
        if(CollectionUtil.isNotEmpty(newThoughts)){
            newThoughts.forEach(t->{
                if(StringUtils.isNotEmpty(t.getPicList())){
                    t.setPictures(Arrays.asList(t.getPicList().split(",")));
                }
            });
        }
        CommonConstant.NEW_THOUGHT_LIST = newThoughts;

        PageHelper.startPage(1, 5 , false);
        thoughtExample.clear();
        thoughtExample.setOrderByClause("comment_count desc ");
        thoughtExample.createCriteria().andDelFlagEqualTo(YesNoEnum.NO.getValue());
        CommonConstant.HOT_THOUGHT_LIST = thoughtService.getThoughtWithUser(thoughtExample);

        ArticleExample example = new ArticleExample();
        example.setOrderByClause("browse_count desc");
        example.createCriteria().andCreateTimeGreaterThan(DateUtil.offsetDay(new Date(),-31));
        PageHelper.startPage(0,10,false);
        List<Article> hotArticles = articleService.selectByExample(example);
        /*for(Article article : hotArticles){
            if(article.getArticleTitle().length() > 20){
                article.setArticleTitle(StringUtils.substring(article.getArticleTitle(),0,20) + "……");
            }
        }*/

        if(CollectionUtil.isNotEmpty(hotArticles)){
            CommonConstant.HOT_ARTICLE_LIST = hotArticles;
        }

        FqNoticeExample fqNoticeExample = new FqNoticeExample();
        fqNoticeExample.setOrderByClause("fq_order asc");
        fqNoticeExample.createCriteria().andIsShowEqualTo(YesNoEnum.YES.getValue());
        List<FqNotice> list = fqNoticeService.selectByExample(fqNoticeExample);
        if(CollectionUtil.isNotEmpty(list)){
            CommonConstant.FQ_NOTICE_LIST = list;
        }

        PageHelper.startPage(0,5,false);
        SuperBeautyExample beautyExample = new SuperBeautyExample();
        beautyExample.setOrderByClause("like_count desc");
        List<BeautyUserResponse> beauties = superBeautyService.selectDetailByExample(beautyExample);
        if(CollectionUtil.isNotEmpty(beauties)){
            CommonConstant.HOT_BEAUTY_LIST = beauties;
        }

        try {
            int month = DateUtil.thisMonth()+1;
            JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
            Set<String> userIds =commands.zrevrange(CommonConstant.FQ_ACTIVE_USER_SORT+month,0,4);
            if(CollectionUtils.isNotEmpty(userIds)){
                List<Integer> userIdList = Lists.newArrayList();
                for(String userId : userIds){
                    userIdList.add(Integer.valueOf(userId));
                }
                FqUserExample fqUserExample = new FqUserExample();
                example.createCriteria().andIdIn(userIdList);
                List<FqUser> fqUsers = fqUserService.selectByExample(fqUserExample);
                Map<Integer,FqUser> userMap = Maps.newHashMap();
                fqUsers.forEach(fqUser -> {
                    userMap.put(fqUser.getId(),fqUser);
                });
                List<UserActiveNumResponse> responses = Lists.newArrayList();
                for(int i = 0;i<userIdList.size();i++){
                    double score = commands.zscore(CommonConstant.FQ_ACTIVE_USER_SORT+month,userIdList.get(i).toString());
                    if(userMap.get(userIdList.get(i)) == null){
                        continue;
                    }
                    UserActiveNumResponse userActiveNumResponse = new UserActiveNumResponse(userMap.get(userIdList.get(i)),score,i+1);
                    responses.add(userActiveNumResponse);
                }
                CommonConstant.FQ_ACTIVE_USER_LIST = responses;
            }
        } catch (Exception e) {
            logger.error("",e);
        }

        Runtime runtime = Runtime.getRuntime();
        String memoryInfo ="freeMemory:"+runtime.freeMemory()+",totalMemory:"+runtime.totalMemory();

        stopwatch.stop();
        long seconds = stopwatch.elapsed(TimeUnit.SECONDS);
        logger.info("热门文章以及通知以及热门图片更新完毕,耗时{}秒,内存信息:{}",seconds,memoryInfo);
    }
 
Example 3
Source File: DailyOnceJob.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@Scheduled(cron = "30 0 0 * * ? ")
public void activeCount(){
    Stopwatch stopwatch = Stopwatch.createStarted();
    try {
        Date now = new Date();
        int day = DateUtil.dayOfMonth(now);
        DateTime tbegin = DateUtil.beginOfDay(now);
        DateTime ybegin = DateUtil.beginOfDay(DateUtil.yesterday());
        boolean isfirstDay = day == 1;
        int month = DateUtil.thisMonth()+1;
        JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
        Set<String> userIds =commands.zrevrange(CommonConstant.FQ_ACTIVE_USER_SORT+month,0,-1);
        if(CollectionUtils.isNotEmpty(userIds)){
            List<Integer> userIdList = Lists.newArrayList();
            for(String userId : userIds){
                userIdList.add(Integer.valueOf(userId));
            }
            FqUserExample fqUserExample = new FqUserExample();
            fqUserExample.createCriteria().andIdIn(userIdList);
            List<FqUser> fqUsers = fqUserService.selectByExample(fqUserExample);
            Map<Integer,FqUser> userMap = Maps.newHashMap();
            fqUsers.forEach(fqUser -> {
                userMap.put(fqUser.getId(),fqUser);
            });
            FqUserActiveNumExample example = new FqUserActiveNumExample();
            for(int i = 0;i<userIdList.size();i++){
                Double score = commands.zscore(CommonConstant.FQ_ACTIVE_USER_SORT+month,userIdList.get(i).toString());
                if(isfirstDay){
                    FqUserActiveNum fqUserActiveNum = new FqUserActiveNum();
                    fqUserActiveNum.setGmtCreate(now);
                    fqUserActiveNum.setActiveNum(score == null ? 0 : score.intValue());
                    fqUserActiveNum.setUserId(userIdList.get(i));
                    fqUserActiveNum.setMark(userMap.get(userIdList.get(i)).getNickname()+":当天活跃度:"+fqUserActiveNum.getActiveNum());
                    fqUserActiveNumService.insert(fqUserActiveNum);
                }else {
                    example.clear();
                    example.createCriteria().andUserIdEqualTo(userIdList.get(i)).andGmtCreateBetween(ybegin.toJdkDate(),tbegin.toJdkDate());
                    FqUserActiveNum yesactive = fqUserActiveNumService.selectFirstByExample(example);
                    if(yesactive == null){
                        yesactive = new FqUserActiveNum();
                        yesactive.setGmtCreate(now);
                        yesactive.setActiveNum(score == null ? 0 : score.intValue());
                        yesactive.setUserId(userIdList.get(i));
                        yesactive.setMark(userMap.get(userIdList.get(i)).getNickname()+":当天活跃度:"+yesactive.getActiveNum());
                        fqUserActiveNumService.insert(yesactive);
                    }else {
                        //如果昨天的和今天的数据不一样 进行插入
                        if(yesactive.getActiveNum() != score.intValue()){
                            yesactive = new FqUserActiveNum();
                            yesactive.setGmtCreate(now);
                            yesactive.setActiveNum(score.intValue() - yesactive.getActiveNum());
                            yesactive.setUserId(userIdList.get(i));
                            yesactive.setMark(userMap.get(userIdList.get(i)).getNickname()+":当天活跃度:"+yesactive.getActiveNum());
                            fqUserActiveNumService.insert(yesactive);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("活跃度计算出错!",e);
    }finally {
        JedisProviderFactory.getJedisProvider(null).release();
    }
    stopwatch.stop();
    long seconds = stopwatch.elapsed(TimeUnit.SECONDS);
    logger.info("活跃度计算完毕,耗时{}秒",seconds);
}