簡體   English   中英

Async和EhCache不適用於第二個用戶登錄

[英]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 ,因為您的CacheHelperCacheHelperDelegate不是同一對象。

現在,將要緩存的是Future 不是它返回的值。 有點奇怪。 但是它仍然可以工作。

最后,Spring默認使用參數作為緩存鍵。 在您的情況下, UserLoginSessionInfo userInfo, HttpSession session 這些是高度特定於用戶的。 因此,對於兩個不同的用戶,通常會調用該方法。 如果這不是您想要的,則可能不需要傳遞這些參數。 您還可以通過@Cacheable(key=...)告訴Spring將什么用作鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM