use of org.redisson.api.RLock 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 org.redisson.api.RLock in project redisson by redisson.
the class RedissonLockTest method testDelete.
@Test
public void testDelete() {
RLock lock = redisson.getLock("lock");
Assert.assertFalse(lock.delete());
lock.lock();
Assert.assertTrue(lock.delete());
}
use of org.redisson.api.RLock 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 org.redisson.api.RLock 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 org.redisson.api.RLock in project redisson by redisson.
the class JCache method get.
@Override
public V get(K key) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
RLock lock = getLockedLock(key);
try {
V value = getValueLocked(key);
if (value == null) {
cacheManager.getStatBean(this).addMisses(1);
if (config.isReadThrough()) {
value = loadValue(key);
}
} else {
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addHits(1);
}
return value;
} finally {
lock.unlock();
}
}
Aggregations