简体   繁体   English

Async和EhCache不适用于第二个用户登录

[英]Async and EhCache is not working for the second user logging in

I am trying to implement @async with @cacheable as below .. 我正在尝试使用@cacheable来实现@async,如下所示。

@RequestMapping(value="ehcacheExample")
public ModelAndView loadehcacheExamplePage(HttpServletRequest request, HttpSession session, ModelAndView modelAndView) {

    cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session);

    //diffetent execution
    ...

    return modelAndView;
}

And

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

Now as per my understanding this 'getDataUsingAsyncAndStoreInEhcache' method produces the result independent of the caller method,saves it in ehcache implementation. 现在,根据我的理解,此“ getDataUsingAsyncAndStoreInEhcache”方法产生的结果与调用方方法无关,并将其保存在ehcache实现中。 So, the next time I call an ajax method for getting the data from future, I am doing below implementation - 因此,下次我调用ajax方法从将来获取数据时,我将在实现下面进行操作-

@RequestMapping(value="getCachedData")
    public @ResponseBody String getCachedData(HttpSession session) {
        UserLoginSessionInfo userInfo = HttpRequestSessionUtils.getLoggedinUserSessionInfo(session);

        Future<String> cachedData = cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session);
        …
        return dataFromFuture; 

    }

Now my expectation is, if any other user logs in after the first user, for him cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session) should not execute the whole method. 现在,我的期望是,如果有其他用户在第一个用户之后登录,则cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session)不应为他执行整个方法。 Rather it should retrieve the data from Ehcache memory. 而是应从Ehcache内存中检索数据。

But it is not happening. 但这没有发生。 I can assure that caching is working because in the same session if the user calls the getCachedData ajax call multiple times, it is returning the data from cache only. 我可以确保缓存有效,因为在同一会话中,如果用户多次调用getCachedData ajax调用,则它仅从缓存中返回数据。 But the issue is happening with the cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session) implementation (mentioned in the @RequestMapping(value="ehcacheExample") method). 但是问题发生在cacheHelper.getDataUsingAsyncAndStoreInEhcache(userInfo, session)实现(在@RequestMapping(value="ehcacheExample")方法中提到)。

Can you please help me understand why this method is executing in whole, everytime the user calls loadehcacheExamplePage method rather than retrieving it from cache when a second user logging in? 您能否帮助我理解为什么每次用户调用loadehcacheExamplePage方法时该方法整体执行,而不是在第二个用户登录时从缓存中检索它?

I see different possible issues here. 我在这里看到了其他可能的问题。 First, there is a bug between caching and async in Spring. 首先,Spring中的缓存和异步之间存在一个错误 However, I don't think it is relevant to you since your CacheHelper and CacheHelperDelegate are not the same object. 但是,我认为这与您CacheHelper ,因为您的CacheHelperCacheHelperDelegate不是同一对象。

Now, it's the Future that will get cached. 现在,将要缓存的是Future Not the value returned by it. 不是它返回的值。 It's weird. 有点奇怪。 But it should still work. 但是它仍然可以工作。

Finally, Spring uses by default the parameters as the cache key. 最后,Spring默认使用参数作为缓存键。 In your case UserLoginSessionInfo userInfo, HttpSession session . 在您的情况下, UserLoginSessionInfo userInfo, HttpSession session These are highly user specific. 这些是高度特定于用户的。 So it's normal that for two different users, the method will be called. 因此,对于两个不同的用户,通常会调用该方法。 If that's not what you want, you probably do not need to pass these parameters. 如果这不是您想要的,则可能不需要传递这些参数。 You can also tell Spring what to use as a key with @Cacheable(key=...) . 您还可以通过@Cacheable(key=...)告诉Spring将什么用作键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM