[英]Async and EhCache is not working for the second user logging in
我正在尝试使用@cacheable来实现@async,如下所示。
@RequestMapping(value="ehcacheExample")
public ModelAndView loadehcacheExamplePage(HttpServletRequest request, HttpSession session, ModelAndView modelAndView) {
cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session);
//diffetent execution
...
return modelAndView;
}
和
@Cacheable(value="cacheKey")
public Future<String> getDataUsingAsyncAndStoreInEhcache(UserLoginSessionInfo userInfo,
HttpSession session) {
return ehcacheExampleDelegate.getDataUsingAsyncAndStoreInEhcache(userInfo,session);
}
@Async
public Future<String> getDataUsingAsyncAndStoreInEhcache(UserLoginSessionInfo userInfo,
HttpSession session) {
//Async execution
return new AsyncResult<String>(jsonInString);
}
现在,根据我的理解,此“ getDataUsingAsyncAndStoreInEhcache”方法产生的结果与调用方方法无关,并将其保存在ehcache实现中。 因此,下次我调用ajax方法从将来获取数据时,我将在实现下面进行操作-
@RequestMapping(value="getCachedData")
public @ResponseBody String getCachedData(HttpSession session) {
UserLoginSessionInfo userInfo = HttpRequestSessionUtils.getLoggedinUserSessionInfo(session);
Future<String> cachedData = cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session);
…
return dataFromFuture;
}
现在,我的期望是,如果有其他用户在第一个用户之后登录,则cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session)
不应为他执行整个方法。 而是应从Ehcache内存中检索数据。
但这没有发生。 我可以确保缓存有效,因为在同一会话中,如果用户多次调用getCachedData ajax调用,则它仅从缓存中返回数据。 但是问题发生在cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session)
实现(在@RequestMapping(value="ehcacheExample")
方法中提到)。
您能否帮助我理解为什么每次用户调用loadehcacheExamplePage
方法时该方法整体执行,而不是在第二个用户登录时从缓存中检索它?
我在这里看到了其他可能的问题。 首先,Spring中的缓存和异步之间存在一个错误 。 但是,我认为这与您CacheHelper
,因为您的CacheHelper
和CacheHelperDelegate
不是同一对象。
现在,将要缓存的是Future
。 不是它返回的值。 有点奇怪。 但是它仍然可以工作。
最后,Spring默认使用参数作为缓存键。 在您的情况下, UserLoginSessionInfo userInfo, HttpSession session
。 这些是高度特定于用户的。 因此,对于两个不同的用户,通常会调用该方法。 如果这不是您想要的,则可能不需要传递这些参数。 您还可以通过@Cacheable(key=...)
告诉Spring将什么用作键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.