GUAVA缓存+递归查树

//GUAVA缓存
private LoadingCache<Long, List<Long>> treeCacheBuilder = CacheBuilder.newBuilder()
        .expireAfterWrite(2, TimeUnit.MINUTES).build(new CacheLoader<Long, List<Long>>() {
            @Override
            public List<Long> load(Long id) throws Exception {
                List<Long> ids = new ArrayList<>();
                List<Tree> trees = knowledgeService.getAllTreesByCompanyId(8099L);
                ids.addAll(getTreeList(trees, id));
                return ids;
            }
        });
//递归查找
private List<Long> getTreeList(List<Tree> trees, Long pid){
    List<Long> ids = new ArrayList<>();
    for (Tree tree : trees){
        if(pid.equals(tree.getFather())) {
            ids.add(tree.getTreeId());
            ids.addAll(getTreeList(trees, tree.getTreeId()));
        }
    }
    return ids;
}

//根据parent_id查下面所有子类

public List<ProductCategoryVo> getCategoryMapByLeafCid(Long cid) {
   List<ProductCategoryVo> categoryVos = new ArrayList<>();
   Long parentId = cid;
   while(parentId > 0){
      ProductCategoryVo parent = getById(parentId);
      if(parent != null){
         categoryVos.add(0, parent);
         parentId = parent.getParentId();
      }else{
         break;
      }
   }
   return categoryVos;
}

猜你喜欢

转载自blog.csdn.net/a18827547638/article/details/82985863