Search in sources :

Example 1 with RLock

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();
        }
    }
}
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 2 with RLock

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

Example 3 with RLock

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);
}
Also used : ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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 4 with RLock

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();
        }
    }
}
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 5 with RLock

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();
    }
}
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