use of org.redisson.api.RLock 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();
}
}
}
use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method getLock.
private RLock getLock(K key) {
String lockName = getLockName(key);
RLock lock = redisson.getLock(lockName);
return lock;
}
use of org.redisson.api.RLock in project redisson by redisson.
the class JCache method put.
@Override
public void put(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 {
List<Object> result = getAndPutValueLocked(key, value);
if (result.isEmpty()) {
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return;
}
Long added = (Long) result.get(0);
if (added == null) {
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return;
}
if (Long.valueOf(1).equals(added)) {
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 {
try {
cacheWriter.delete(key);
} catch (CacheWriterException e) {
if (result.size() == 4 && result.get(1) != null) {
putValue(key, result.get(1));
}
throw e;
} catch (Exception e) {
if (result.size() == 4 && result.get(1) != null) {
putValue(key, result.get(1));
}
throw new CacheWriterException(e);
}
}
cacheManager.getStatBean(this).addPuts(1);
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
boolean result = putValueLocked(key, value);
if (result) {
cacheManager.getStatBean(this).addPuts(1);
}
} finally {
lock.unlock();
}
}
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class BaseTransactionalMap method executeLocked.
protected <R> void executeLocked(RPromise<R> promise, K key, Runnable runnable) {
RLock lock = getLock(key);
executeLocked(promise, runnable, lock);
}
use of org.redisson.api.RLock in project redisson by redisson.
the class RedissonCache method get.
public <T> T get(Object key, Callable<T> valueLoader) {
Object value;
if (mapCache != null && config.getMaxIdleTime() == 0 && config.getMaxSize() == 0) {
value = mapCache.getWithTTLOnly(key);
} else {
value = map.get(key);
}
if (value == null) {
addCacheMiss();
RLock lock = map.getLock(key);
lock.lock();
try {
value = map.get(key);
if (value == null) {
value = putValue(key, valueLoader, value);
}
} finally {
lock.unlock();
}
} else {
addCacheHit();
}
return (T) fromStoreValue(value);
}
Aggregations