use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method load.
V load(K key) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
V value = getValueLocked(key);
if (value == null) {
value = loadValue(key);
}
return value;
} finally {
lock.unlock();
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method getAndRemove.
@Override
public V getAndRemove(K key) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
Object value = getAndRemoveValue(key);
if (value != null) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addRemovals(1);
} else {
cacheManager.getStatBean(this).addMisses(1);
}
try {
cacheWriter.delete(key);
} catch (CacheWriterException e) {
if (value != null) {
putValue(key, value);
}
throw e;
} catch (Exception e) {
if (value != null) {
putValue(key, value);
}
throw new CacheWriterException(e);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return (V) value;
} finally {
lock.unlock();
}
} else {
V value = getAndRemoveValue(key);
if (value != null) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addRemovals(1);
} else {
cacheManager.getStatBean(this).addMisses(1);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return value;
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method remove.
@Override
public boolean remove(K key, V value) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
if (value == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
boolean result;
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
result = removeValueLocked(key, value);
if (result) {
try {
cacheWriter.delete(key);
} catch (CacheWriterException e) {
putValue(key, value);
throw e;
} catch (Exception e) {
putValue(key, value);
throw new CacheWriterException(e);
}
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addRemovals(1);
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return true;
} else {
cacheManager.getStatBean(this).addMisses(1);
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return false;
}
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
result = removeValueLocked(key, value);
if (result) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addRemovals(1);
} else {
cacheManager.getStatBean(this).addMisses(1);
}
cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
return result;
} finally {
lock.unlock();
}
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method putAll.
@Override
public void putAll(Map<? extends K, ? extends V> map) {
checkNotClosed();
Map<K, V> deletedKeys = new HashMap<K, V>();
Map<K, Cache.Entry<? extends K, ? extends V>> addedEntries = new HashMap<K, Cache.Entry<? extends K, ? extends V>>();
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
K key = entry.getKey();
if (key == null) {
throw new NullPointerException();
}
V value = entry.getValue();
if (value == null) {
throw new NullPointerException();
}
}
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
List<Object> result = getAndPutValue(key, value);
if (result.isEmpty()) {
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
continue;
}
Long added = (Long) result.get(0);
if (added == null) {
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
continue;
}
if (Long.valueOf(1).equals(added)) {
addedEntries.put(key, new JCacheEntry<K, V>(key, value));
} else {
V val = null;
if (result.size() == 4) {
val = (V) result.get(1);
}
deletedKeys.put(key, val);
}
cacheManager.getStatBean(this).addPuts(1);
waitSync(result);
} else {
boolean result = putValue(key, value);
if (result) {
cacheManager.getStatBean(this).addPuts(1);
}
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
}
if (config.isWriteThrough()) {
try {
try {
cacheWriter.writeAll(addedEntries.values());
} catch (CacheWriterException e) {
removeValues(addedEntries.keySet().toArray());
throw e;
} catch (Exception e) {
removeValues(addedEntries.keySet().toArray());
throw new CacheWriterException(e);
}
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);
}
} finally {
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
getLock(entry.getKey()).unlock();
}
}
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonTwoLockedThread method testLock.
@Test(timeout = 3000)
public void testLock() throws InterruptedException {
final String lockName = "lock1";
final CountDownLatch startSignal = new CountDownLatch(1);
final CountDownLatch testSignal = new CountDownLatch(1);
final CountDownLatch completeSignal = new CountDownLatch(2);
System.out.println("configure");
final long millis = System.currentTimeMillis();
new Thread() {
@Override
public void run() {
try {
startSignal.await();
RLock lock = redisson.getLock(lockName);
System.out.println("1. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
lock.lock();
System.out.println("1. lock " + lock.getName() + " - " + Thread.currentThread().getId());
testSignal.countDown();
Thread.sleep(500);
lock.unlock();
System.out.println("1. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
new Thread() {
@Override
public void run() {
try {
testSignal.await();
RLock lock = redisson.getLock(lockName);
System.out.println("2. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
lock.lock();
System.out.println("2. lock " + lock.getName() + " - " + Thread.currentThread().getId());
long current = System.currentTimeMillis();
Assert.assertTrue("current=" + current + ", millis=" + millis, current - millis >= 500);
Thread.sleep(500);
lock.unlock();
System.out.println("2. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
System.out.println("start");
startSignal.countDown();
completeSignal.await();
System.out.println("complete");
}
Aggregations