use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method replace.
@Override
public boolean replace(K key, V oldValue, V newValue) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
if (oldValue == null) {
throw new NullPointerException();
}
if (newValue == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
long result = replaceValueLocked(key, oldValue, newValue);
if (result == 1) {
try {
cacheWriter.write(new JCacheEntry<K, V>(key, newValue));
} catch (CacheWriterException e) {
removeValues(key);
throw e;
} catch (Exception e) {
removeValues(key);
throw new CacheWriterException(e);
}
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return true;
} else {
if (result == 0) {
cacheManager.getStatBean(this).addMisses(1);
} else {
cacheManager.getStatBean(this).addHits(1);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return false;
}
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
long result = replaceValueLocked(key, oldValue, newValue);
if (result == 1) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
} else if (result == 0) {
cacheManager.getStatBean(this).addMisses(1);
} else {
cacheManager.getStatBean(this).addHits(1);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return result == 1;
} finally {
lock.unlock();
}
}
}
use of javax.cache.integration.CacheWriterException in project caffeine by ben-manes.
the class CacheProxy method putAll.
@Override
public void putAll(Map<? extends K, ? extends V> map) {
requireNotClosed();
boolean statsEnabled = statistics.isEnabled();
long start = statsEnabled ? ticker.read() : 0L;
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
requireNonNull(entry.getKey());
requireNonNull(entry.getValue());
}
int[] puts = { 0 };
CacheWriterException e = writeAllToCacheWriter(map);
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
putNoCopyOrAwait(entry.getKey(), entry.getValue(), /* publishToWriter */
false, puts);
}
dispatcher.awaitSynchronous();
if (statsEnabled) {
statistics.recordPuts(puts[0]);
statistics.recordPutTime(ticker.read() - start);
}
if (e != null) {
throw e;
}
}
use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method removeAll.
@Override
public void removeAll(Set<? extends K> keys) {
checkNotClosed();
Map<K, V> deletedKeys = new HashMap<K, V>();
for (K key : keys) {
if (key == null) {
throw new NullPointerException();
}
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
for (K key : keys) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
V result = getAndRemoveValue(key);
if (result != null) {
deletedKeys.put(key, result);
}
}
try {
try {
cacheWriter.deleteAll(deletedKeys.keySet());
} catch (CacheWriterException e) {
for (Map.Entry<K, V> deletedEntry : deletedKeys.entrySet()) {
if (deletedEntry.getValue() != null) {
putValue(deletedEntry.getKey(), deletedEntry.getValue());
}
}
throw e;
} catch (Exception e) {
for (Map.Entry<K, V> deletedEntry : deletedKeys.entrySet()) {
if (deletedEntry.getValue() != null) {
putValue(deletedEntry.getKey(), deletedEntry.getValue());
}
}
throw new CacheWriterException(e);
}
cacheManager.getStatBean(this).addRemovals(deletedKeys.size());
} finally {
for (K key : keys) {
getLock(key).unlock();
}
}
} else {
long removedKeys = removeValues(keys.toArray());
cacheManager.getStatBean(this).addRemovals(removedKeys);
}
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
}
use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method getAndReplace.
@Override
public V getAndReplace(K key, V value) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
if (value == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
V result = getAndReplaceValueLocked(key, value);
if (result != null) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
try {
cacheWriter.write(new JCacheEntry<K, V>(key, value));
} catch (CacheWriterException e) {
removeValues(key);
throw e;
} catch (Exception e) {
removeValues(key);
throw new CacheWriterException(e);
}
} else {
cacheManager.getStatBean(this).addMisses(1);
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
V result = getAndReplaceValueLocked(key, value);
if (result != null) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
} else {
cacheManager.getStatBean(this).addMisses(1);
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
}
}
use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method putIfAbsent.
@Override
public boolean putIfAbsent(K key, V value) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
if (value == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
boolean result = putIfAbsentValueLocked(key, value);
if (result) {
cacheManager.getStatBean(this).addPuts(1);
try {
cacheWriter.write(new JCacheEntry<K, V>(key, value));
} catch (CacheWriterException e) {
removeValues(key);
throw e;
} catch (Exception e) {
removeValues(key);
throw new CacheWriterException(e);
}
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
boolean result = putIfAbsentValueLocked(key, value);
if (result) {
cacheManager.getStatBean(this).addPuts(1);
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
}
}
Aggregations