Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My @Cacheable seems to be ignored (Spring)

I have to cache the result of the following public method :

    @Cacheable(value = "tasks", key = "#user.username")
    public Set<MyPojo> retrieveCurrentUserTailingTasks(UserInformation user) {
        Set<MyPojo> resultSet;
        try {
            nodeInformationList = taskService.getTaskList(user);
        } catch (Exception e) {
            throw new ApiException("Error while retrieving tailing tasks", e);
        }
        return resultSet;
    }

I also configured Caching here :

@Configuration
@EnableCaching(mode = AdviceMode.PROXY)
public class CacheConfig  {

@Bean
public CacheManager cacheManager() {
    final SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("tasks"),new ConcurrentMapCache("templates")));
    return cacheManager;
}

@Bean
public CacheResolver cacheResolver() {
    final SimpleCacheResolver cacheResolver = new SimpleCacheResolver(cacheManager());
    return cacheResolver;
}

}

I assert the following :

  1. Cache is initialized and does exist within Spring Context
  2. I used jvisualvm to track ConcurrentMapCache (2 instances), they are there in the heap but empty
  3. Method returns same values per user.username
  4. I tried the same configuration using spring-boot based project and it worked
  5. The method is public and is inside a Spring Controller
  6. The annotation @CacheConfig(cacheNames = "tasks") added on top of my controller

Spring version 4.1.3.RELEASE Jdk 1.6

Update 001 :

  @RequestMapping(value = "/{kinematicId}/status/{status}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public DocumentNodeWrapper getDocumentsByKinematicByStatus(@PathVariable String kinematicId, @PathVariable String status, HttpServletRequest request) {
    UserInformation user = getUserInformation(request);
    Set<ParapheurNodeInformation> nodeInformationList =  retrieveCurrentUserTailingTasks(user);
    final List<DocumentNodeVO> documentsList = getDocumentsByKinematic(kinematicId, user, nodeInformationList);

    List<DocumentNodeVO> onlyWithGivenStatus = filterByStatus(documentsList);

    return new DocumentNodeWrapper("filesModel", onlyWithGivenStatus, user, currentkinematic);
}

Thanks

like image 462
Abderrazak BOUADMA Avatar asked Sep 19 '25 09:09

Abderrazak BOUADMA


1 Answers

Is the calling method getDocumentsByKinematicByStatus() in the same bean as the cacheable method ? If true, then this is a normal behavior because you're not calling the cacheable method via proxy but directly.

like image 140
Mohamed Abdennebi Avatar answered Sep 21 '25 00:09

Mohamed Abdennebi