[英]Spring @Cacheable not working - MultiThreading
我有一個 spring 批處理,它使用執行器服務執行多個線程。 所有線程都訪問下面的方法。 我嘗試使用@Cacheable 緩存該方法。 但它不起作用。 每個線程都在不緩存的情況下執行此方法。 我使用 sysouts 發現了這一點。 更糟糕的是,即使同一個線程在沒有緩存的情況下多次進入方法內部(在 for 循環內)。
你能告訴我我在這里做錯了什么嗎?
@Cacheable(key="#userID",sync=true)
public String getPassword(String userID) throws Exceptions{
logger.info("cache not working");
SecurityTokenClientResponse password=client.getPassword(req, true, notify.getEnv());
byte[] encryptedPassword=password.getPasswordByteArray();
if (encryptedPassword!=null)
{
try {
return retrieveCreds(client, encryptedPassword);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}else {
password.getBusinessError();
}
return null;
}
要使 Spring 緩存工作,請確保您具有:
@EnableCaching
@Cacheable(cacheNames = "name of your cache")
例子:
@Bean
public SimpleCacheManager simpleCacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
simpleCacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("myCacheName"));
return simpleCacheManager;
}
和:
@Cacheable(key="#userID",sync=true, cacheNames ="myCacheName")
public String getPassword(String userID) throws Exceptions
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.