我的@Cacheable 似乎被忽略了 (Spring)
My @Cacheable seems to be ignored (Spring)
我必须缓存以下 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;
}
}
我断言如下:
- 缓存已初始化并且确实存在于 Spring 上下文中
- 我使用 jvisualvm 来跟踪
ConcurrentMapCache
(2 个实例),它们是
在堆中但为空
- 方法 returns 每个 user.username
相同的值
- 我尝试使用基于 spring-boot 的项目进行相同的配置,并且
有效
- 方法是 public 并且在 Spring 控制器中
- 注释
@CacheConfig(cacheNames = "tasks")
添加在我的上面
控制器
Spring 版本 4.1.3.RELEASE
Jdk1.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 中?如果为真,则这是正常行为,因为您不是通过代理而是直接调用可缓存方法。
我必须缓存以下 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;
}
}
我断言如下:
- 缓存已初始化并且确实存在于 Spring 上下文中
- 我使用 jvisualvm 来跟踪
ConcurrentMapCache
(2 个实例),它们是 在堆中但为空 - 方法 returns 每个 user.username 相同的值
- 我尝试使用基于 spring-boot 的项目进行相同的配置,并且 有效
- 方法是 public 并且在 Spring 控制器中
- 注释
@CacheConfig(cacheNames = "tasks")
添加在我的上面 控制器
Spring 版本 4.1.3.RELEASE Jdk1.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 中?如果为真,则这是正常行为,因为您不是通过代理而是直接调用可缓存方法。