Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

商品发布的MongoDB #3

Closed
liutao-lx opened this issue Mar 15, 2021 · 3 comments
Closed

商品发布的MongoDB #3

liutao-lx opened this issue Mar 15, 2021 · 3 comments

Comments

@liutao-lx
Copy link

似乎没有添加到MongoDB的功能

@HMingR
Copy link
Owner

HMingR commented Mar 15, 2021

是的,目前只有在对接推荐系统的地方用到了mongoDB作为一个中间件传输数据,其他的没有找到合适的场景去使用它。你觉得需要在什么地方用到呢?

@liutao-lx
Copy link
Author

您好,我只看见了查询MongoDB代码,添加数据到推荐系统MongoDB代码我似乎没找到,我一直卡在这里

是的,目前只有在对接推荐系统的地方用到了mongoDB作为一个中间件传输数据,其他的没有找到合适的场景去使用它。你觉得需要在什么地方用到呢?

@liutao-lx
Copy link
Author

在goods服务的RecommendProductServiceImpl的getRealtimeRecommend()方法
public Message<Page> getRealtimeRecommend(Integer pageSize, Integer currentPage, Long userId) {
//如果是游客登录直接 查询发布时间最新的商品

    if(userId == null){
        log.info("智能推送{}---->商城首页"," userId == null ");
        return productInfoService.getProductBriefInfoByPage(currentPage, pageSize);
    }
    //获得存储在redis中的实时推荐的商品id的list
    String redisKey = RedisUtil.getProductRealtimeRecommendListKey(userId);
    Boolean aBoolean = redisTemplate.hasKey(redisKey);
    //直接返回redis的的数据
    if(aBoolean){
        log.info("智能推送{}---->商城首页","redis存储实时推荐商品id的list");
        return getInfoFromRedis(pageSize, currentPage, redisKey);
    }

    //MongoDB的数据,这里找不到数据所以每次推荐都是离线推荐
    ProductRealtimeRecommendDto recommendInfo = productRealtimeRecommendRepository.findByUserId(userId);
    List<MongoProductInfo> recs = null;
    if(recommendInfo != null){
        recs = recommendInfo.getRecs();
    }

    String userBrowseRecordBitmap = RedisUtil.getUserBrowseRecordBitmap(BrowserType.ES_SELL_PRODUCT, userId);
    //当实时推荐中没有商品时,转到离线推荐
    if(CollectionUtils.isEmpty(recs)) {
        log.info("智能推送{}---->商城首页","实时推荐没有商品转离线推荐");
        return getOfflineRecommendListByUserId(pageSize, currentPage, userId);
    }
    //将商品信息存入redis中
    List<Long> ids = recs.stream().map(MongoProductInfo::getProductId).collect(Collectors.toList());
    ids.stream().forEach(id -> {
        if(commands.getbit(userBrowseRecordBitmap.getBytes(), id) == null){
            redisTemplate.opsForSet().add(redisKey, id);
        }
    });
    return getInfoFromRedis(pageSize, currentPage, redisKey);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants