我的@Cacheable似乎被忽略了(Spring)

Abd*_*DMA 2 java spring caching spring-mvc

我必须缓存以下public方法的结果:

    @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;
    }
Run Code Online (Sandbox Code Playgroud)

我还在这里配置了缓存:

@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;
}

}
Run Code Online (Sandbox Code Playgroud)

我断言如下:

  1. 缓存已初始化,并且确实存在于Spring Context中
  2. 我使用jvisualvm来跟踪ConcurrentMapCache(2个实例),它们在堆中但是空的
  3. 方法每user.username返回相同的值
  4. 我使用基于spring-boot的项目尝试了相同的配置,并且它工作正常
  5. 该方法是公共的,位于Spring Controller中
  6. 注释@CacheConfig(cacheNames = "tasks")添加在我的控制器之上

Spring版本4.1.3.RELEASE Jdk 1.6

更新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);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 5

调用方法getDocumentsByKinematicByStatus()与可缓存方法在同一个bean中吗?如果为true,则这是正常行为,因为您不是通过代理直接调用可缓存方法.