Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class BondDao {
- private static final String CACHE_BOND = "bond";
- private final BondRepository bondRepository;
- private final CaffeineCache cache;
- public BondDao(BondRepository bondRepository, CacheManager cacheManager) {
- this.bondRepository = bondRepository;
- this.cache = (CaffeineCache) cacheManager.getCache(CACHE_BOND);
- }
- @PostConstruct
- private void warmUpCache() {
- bondRepository.findAll()
- .forEach(entity -> cache.putIfAbsent(entity.getId(), entity));
- }
- public List<BondEntity> findAll() {
- return StreamEx.ofValues(cache.getNativeCache().asMap())
- .map(o -> (BondEntity) o)
- .collect(Collectors.toList());
- }
- @Cacheable(cacheNames = "bond", key = "#id", unless = "#result == null")
- public BondEntity findById(Long id) {
- return bondRepository.findById(id).orElse(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement