ConcurrentModification EHCache 多线程 Java
ConcurrentModification EHCache Multi Thread Java
我们有多个线程试图从 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;
}
我现在正在方法中进行深度复制以防止出现此错误。这可能会产生任何不可预见的后果吗?该方法只会从缓存中获取,所以我相信不会有任何影响
Element cacheElement = (Element)
SerializationUtils.clone(cacheObject.get(fullCacheKey.toString()));
我们有多个线程试图从 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;
}
我现在正在方法中进行深度复制以防止出现此错误。这可能会产生任何不可预见的后果吗?该方法只会从缓存中获取,所以我相信不会有任何影响
Element cacheElement = (Element) SerializationUtils.clone(cacheObject.get(fullCacheKey.toString()));