gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:15:53 24 4
gpt4 key购买 nike

我必须缓存以下 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 上下文中
  2. 我使用 jvisualvm 来跟踪 ConcurrentMapCache(2 个实例),它们是在堆里但是是空的
  3. 方法返回每个 user.username 相同的值
  4. 我使用基于 spring-boot 的项目尝试了相同的配置,并且成功了
  5. 该方法是公开的并且在 Spring Controller 中
  6. 注解 @CacheConfig(cacheNames = "tasks") 添加在我的上面控制者

Spring 版本 4.1.3.RELEASEJdk 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);
}

谢谢

最佳答案

调用方法 getDocumentsByKinematicByStatus() 是否在与可缓存方法相同的 bean 中?如果为真,则这是正常行为,因为您不是通过代理而是直接调用可缓存方法。

关于java - 我的@Cacheable 似乎被忽略了( Spring ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28234878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com