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

时间:2015-01-30 11:25:49

标签: java spring spring-mvc caching

我必须缓存以下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;
    }

我还在这里配置了缓存:

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

}

我声明如下:

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

    由于

1 个答案:

答案 0 :(得分:5)

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