Search in sources :

Example 6 with RLock

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();
        }
    }
}
Also used : RLock(org.redisson.api.RLock) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 7 with RLock

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;
}
Also used : RLock(org.redisson.api.RLock)

Example 8 with RLock

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);
}
Also used : RLock(org.redisson.api.RLock) RedissonObject(org.redisson.RedissonObject) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 9 with RLock

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);
}
Also used : RLock(org.redisson.api.RLock)

Example 10 with RLock

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);
}
Also used : RLock(org.redisson.api.RLock)

Aggregations

RLock (org.redisson.api.RLock)111 Test (org.junit.jupiter.api.Test)77 RedissonClient (org.redisson.api.RedissonClient)21 RReadWriteLock (org.redisson.api.RReadWriteLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RedisProcess (org.redisson.RedisRunner.RedisProcess)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 Config (org.redisson.config.Config)10 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.Test)6 RedissonObject (org.redisson.RedissonObject)4 RSemaphore (org.redisson.api.RSemaphore)4 SecureRandom (java.security.SecureRandom)3 Date (java.util.Date)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Lock (java.util.concurrent.locks.Lock)3