0

I am newbie at Caffeine and Spring boot. I am trying to find a solution to cache a list of objects. the DB transaction takes 3 seconds to load the list, so I am looking to cache the resultset using Caffeine. To cache each item individually, I am following the below approach, but it doesn't seem to work.

public List<Item> getAllItemsOnStartUp() {
            allItemsList = repo.getAllItems();
            for (Item item : allItemsList) {
                staticItem = item;
                getItem(item.getItmsId());
            }
        
        return allItemsList;
    }

    @CachePut(value = "allItems", key = "#itmsId")
    public Item getItem(BigInteger itmsId) {
        return item;
    }

@Override
@Cacheable(value = "allItems")
    public List<Item> getAllItems() {
        
        allItemsList = repo.getAllItems();
        return allItemsList;
    }

@Override
    @Transactional
    @CachePut(value = "allItems", key="#a0.itmsId")
    public Item insertItem(Item item) {
      Item savedItem = rRepo.save(item);
      return savedItem;
    }

When the server starts up, getAllItemsOnStartUp() must run and populate cache. the app calls getAllItems() to retrieve the list, it is expected to use cache here but every time the app gets data from DB which takes 3 seconds.

  • I think it is because Spring AOP is proxy-based so it doesn’t intercept the local method call (see related [question](https://stackoverflow.com/questions/13564627/spring-aop-not-working-for-method-call-inside-another-method)). – Ben Manes May 10 '21 at 15:17

0 Answers0