Search code examples
javaspringcachingspring-transactions

@Cacheable not creating cache keys when called from a method having @transactional annotation in spring boot


ran into a strange issue, I annotated my controller with @Transactional(just for testing) and it took me by surprise that internally when it hits the method with @cacheable annotation it is not creating any cache keys

@Transactional
@RequestMapping(value = "/auth", method = RequestMethod.POST)
public void createAuth() {
    ApplicationContextProvider.getApplicationContext().getBean(GeographyServiceHelper.class).getAggregatedClusterData(1);
}

method with @cacheable issue.

@Cacheable(value = "getAllClusterHierarchyHash", key = "\"all_cluster_hirerarchy_map\"", unless = CONDITION_NULL,
        cacheNames = "getAllClusterHierarchyHash")
@CacheEvict(value = "getAllClusterHierarchyHash", key = "\"all_cluster_hirerarchy_map\"",
        condition = "#forceReload", cacheNames = "getAllClusterHierarchyHash", beforeInvocation = true)
public Map<Integer, ClusterDetails> getAllClusterHierarchyHash(Boolean forceReload) {// do something}

Internally geography service helper calls api manager directly with forceReload = false.

Am I missing something silly?


Solution

  • Hey buddy just wait for transaction to get commit and you will see what you want to see.