@Cacheable 同一个service方法中不起作用


@Service
@Transactional
@CacheConfig(cacheNames = Constants.ESEN_PROFILE_TAG_CACHE_NAME)
public class TagServiceImpl implements TagService{

    private final Logger log = LoggerFactory.getLogger(TagServiceImpl.class);

    private final TagRepository tagRepository;

    private final TagMapper tagMapper;

    @Autowired
    private TagServiceImpl instance;

    public TagServiceImpl(TagRepository tagRepository, TagMapper tagMapper) {
        this.tagRepository = tagRepository;
        this.tagMapper = tagMapper;
    }

  
    @Override
    public List query(String userId) {
        String currentUserId = SecurityUtils.getCurrentUserId();
        if (StringUtils.isEmpty(userId)) {
            userId = currentUserId;
        }
        List tags = instance.getTags(userId);
        return tags.stream().map(tag -> {
            TagResultDTO resultDTO = new TagResultDTO();
            resultDTO.setName(tag.getName());
            resultDTO.setFavorite(false);

            if (null != tag.getFavorites() && tag.getFavorites().size() > 0) {
                resultDTO.setCount(Long.valueOf(tag.getFavorites().size()));
                tag.getFavorites().forEach(
                    tagFavorite -> {
                        if (currentUserId.equals(tagFavorite.getCreatedBy())){
                            resultDTO.setFavorite(true);
                        }
                    });

            } else {
                resultDTO.setCount(0L);
            }

            return resultDTO;
        }).collect(Collectors.toList());

    }

    @Cacheable(key = "'tags' + #userId")
    public List getTags(String userId) {
        List tags =  tagRepository.findAllByUserIdAndDelFlagFalseOrderByCreatedTime(userId);
        tags.forEach(tag -> {
            tag.getFavorites();
        });
        return tags;
    }

}

你可能感兴趣的:(@Cacheable 同一个service方法中不起作用)