Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. @Component
  2. public class BondDao {
  3.     private static final String CACHE_BOND = "bond";
  4.  
  5.     private final BondRepository bondRepository;
  6.     private final CaffeineCache cache;
  7.  
  8.     public BondDao(BondRepository bondRepository, CacheManager cacheManager) {
  9.         this.bondRepository = bondRepository;
  10.         this.cache = (CaffeineCache) cacheManager.getCache(CACHE_BOND);
  11.     }
  12.  
  13.     @PostConstruct
  14.     private void warmUpCache() {
  15.         bondRepository.findAll()
  16.                 .forEach(entity -> cache.putIfAbsent(entity.getId(), entity));
  17.     }
  18.  
  19.     public List<BondEntity> findAll() {
  20.         return StreamEx.ofValues(cache.getNativeCache().asMap())
  21.                 .map(o -> (BondEntity) o)
  22.                 .collect(Collectors.toList());
  23.     }
  24.  
  25.     @Cacheable(cacheNames = "bond", key = "#id", unless = "#result == null")
  26.     public BondEntity findById(Long id) {
  27.         return bondRepository.findById(id).orElse(null);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement