我们有多个线程试图从EHCache获取数据,这有时会导致并发修改异常。我尝试了Retryable来解决此问题,但它似乎没有做任何事情。有什么建议?
@Override
@Retryable(include= ConcurrentModificationException.class, backoff = @Backoff(delay = 5000, maxDelay = 3000), maxAttempts = 5)
public Object getFromCache(String cacheDomain, String cacheKey) {
Object value = null;
Cache cacheObject = getCacheObject();
if (cacheObject != null) {
StringBuilder fullCacheKey = new StringBuilder();
fullCacheKey.append(cacheDomain);
fullCacheKey.append("|");
fullCacheKey.append(cacheKey);
synchronized (cacheObject) {
Element cacheElement = cacheObject.get(fullCacheKey.toString());
if (cacheElement != null) {
value = cacheElement.getObjectValue();
}
}
log.debug("Value from Cache with key [" + fullCacheKey.toString() + "] is " + value);
}
return value;
}
最佳答案
我正在对该方法进行深层复制,以防止出现此错误。这可能会带来不可预见的后果吗?该方法只能从缓存中获取,所以我相信不会有任何影响
元素cacheElement =(元素)
SerializationUtils.clone(cacheObject.get(fullCacheKey.toString()));