Search in sources :

Example 6 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method putIfExistsAsync.

@Override
public RFuture<V> putIfExistsAsync(K key, V value) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        ByteBuf mapKey = encodeMapKey(key);
        CacheKey cacheKey = localCacheView.toCacheKey(mapKey);
        CacheValue prevValue = cachePutIfExists(cacheKey, key, value);
        if (prevValue != null) {
            broadcastLocalCacheStore((V) value, mapKey, cacheKey);
            return new CompletableFutureWrapper<>((V) prevValue.getValue());
        } else {
            mapKey.release();
            return new CompletableFutureWrapper((Void) null);
        }
    }
    RFuture<V> future = super.putIfExistsAsync(key, value);
    CompletionStage<V> f = future.thenApply(res -> {
        if (res != null) {
            CacheKey cacheKey = localCacheView.toCacheKey(key);
            cachePut(cacheKey, key, value);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 7 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method fastPutIfExistsAsync.

@Override
public RFuture<Boolean> fastPutIfExistsAsync(K key, V value) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        ByteBuf mapKey = encodeMapKey(key);
        CacheKey cacheKey = localCacheView.toCacheKey(mapKey);
        CacheValue prevValue = cachePutIfExists(cacheKey, key, value);
        if (prevValue != null) {
            broadcastLocalCacheStore(value, mapKey, cacheKey);
            return new CompletableFutureWrapper<>(true);
        } else {
            mapKey.release();
            return new CompletableFutureWrapper<>(false);
        }
    }
    RFuture<Boolean> future = super.fastPutIfExistsAsync(key, value);
    CompletionStage<Boolean> f = future.thenApply(res -> {
        if (res) {
            CacheKey cacheKey = localCacheView.toCacheKey(key);
            cachePut(cacheKey, key, value);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method fastRemoveOperationAsync.

@Override
protected RFuture<Long> fastRemoveOperationAsync(K... keys) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        long count = 0;
        for (K k : keys) {
            CacheKey cacheKey = localCacheView.toCacheKey(k);
            CacheValue val = cache.remove(cacheKey);
            if (val != null) {
                count++;
                LocalCachedMapInvalidate msg = new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash());
                listener.getInvalidationTopic().publishAsync(msg);
            }
        }
        return new CompletableFutureWrapper<>(count);
    }
    if (invalidateEntryOnChange == 1) {
        List<Object> params = new ArrayList<Object>(keys.length * 2);
        for (K k : keys) {
            ByteBuf keyEncoded = encodeMapKey(k);
            params.add(keyEncoded);
            CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
            cache.remove(cacheKey);
            ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
            params.add(msgEncoded);
        }
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_LONG, "local counter = 0; " + "for j = 1, #ARGV, 2 do " + "if redis.call('hdel', KEYS[1], ARGV[j]) == 1 then " + "redis.call('publish', KEYS[2], ARGV[j+1]); " + "counter = counter + 1;" + "end;" + "end;" + "return counter;", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName()), params.toArray());
    }
    if (invalidateEntryOnChange == 2) {
        List<Object> params = new ArrayList<Object>(keys.length * 3);
        params.add(System.currentTimeMillis());
        for (K k : keys) {
            ByteBuf keyEncoded = encodeMapKey(k);
            params.add(keyEncoded);
            CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
            cache.remove(cacheKey);
            ByteBuf msgEncoded = encode(new LocalCachedMapInvalidate(instanceId, cacheKey.getKeyHash()));
            params.add(msgEncoded);
            byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
            params.add(entryId);
        }
        return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_LONG, "local counter = 0; " + "for j = 2, #ARGV, 3 do " + "if redis.call('hdel', KEYS[1], ARGV[j]) == 1 then " + "redis.call('zadd', KEYS[3], ARGV[1], ARGV[j+2]);" + "redis.call('publish', KEYS[2], ARGV[j+1]); " + "counter = counter + 1;" + "end;" + "end;" + "return counter;", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), params.toArray());
    }
    List<Object> params = new ArrayList<Object>(keys.length + 1);
    params.add(getRawName());
    for (K k : keys) {
        ByteBuf keyEncoded = encodeMapKey(k);
        params.add(keyEncoded);
        CacheKey cacheKey = localCacheView.toCacheKey(keyEncoded);
        cache.remove(cacheKey);
    }
    return commandExecutor.writeAsync(getRawName(), codec, RedisCommands.HDEL, params.toArray());
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method fastPutOperationAsync.

@Override
protected RFuture<Boolean> fastPutOperationAsync(K key, V value) {
    ByteBuf encodedKey = encodeMapKey(key);
    CacheKey cacheKey = localCacheView.toCacheKey(encodedKey);
    CacheValue prevValue = cachePut(cacheKey, key, value);
    broadcastLocalCacheStore(value, encodedKey, cacheKey);
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        return new CompletableFutureWrapper<>(prevValue == null);
    }
    ByteBuf encodedValue = encodeMapValue(value);
    byte[] entryId = generateLogEntryId(cacheKey.getKeyHash());
    ByteBuf msg = createSyncMessage(encodedKey, encodedValue, cacheKey);
    return commandExecutor.evalWriteAsync(getRawName(), codec, RedisCommands.EVAL_BOOLEAN, "if ARGV[4] == '1' then " + "redis.call('publish', KEYS[2], ARGV[3]); " + "end;" + "if ARGV[4] == '2' then " + "redis.call('zadd', KEYS[3], ARGV[5], ARGV[6]);" + "redis.call('publish', KEYS[2], ARGV[3]); " + "end;" + "if redis.call('hset', KEYS[1], ARGV[1], ARGV[2]) == 0 then " + "return 0; " + "end; " + "return 1; ", Arrays.<Object>asList(getRawName(), listener.getInvalidationTopicName(), listener.getUpdatesLogName()), encodedKey, encodedValue, msg, invalidateEntryOnChange, System.currentTimeMillis(), entryId);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Example 10 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonLocalCachedMap method fastReplaceAsync.

@Override
public RFuture<Boolean> fastReplaceAsync(K key, V value) {
    if (storeMode == LocalCachedMapOptions.StoreMode.LOCALCACHE) {
        ByteBuf mapKey = encodeMapKey(key);
        CacheKey cacheKey = localCacheView.toCacheKey(mapKey);
        CacheValue prevValue = cacheReplace(cacheKey, key, value);
        if (prevValue != null) {
            broadcastLocalCacheStore(value, mapKey, cacheKey);
            return new CompletableFutureWrapper<>(true);
        } else {
            mapKey.release();
            return new CompletableFutureWrapper<>(false);
        }
    }
    RFuture<Boolean> future = super.fastReplaceAsync(key, value);
    CompletionStage<Boolean> f = future.thenApply(res -> {
        if (res) {
            CacheKey cacheKey = localCacheView.toCacheKey(key);
            cachePut(cacheKey, key, value);
        }
        return res;
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)38 ByteBuf (io.netty.buffer.ByteBuf)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 RFuture (org.redisson.api.RFuture)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 StringCodec (org.redisson.client.codec.StringCodec)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ByteBufUtil (io.netty.buffer.ByteBufUtil)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)5 Timeout (io.netty.util.Timeout)4 java.util.concurrent (java.util.concurrent)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RSemaphore (org.redisson.api.RSemaphore)4 RTopic (org.redisson.api.RTopic)4 RedissonClient (org.redisson.api.RedissonClient)4 RedisCommand (org.redisson.client.protocol.RedisCommand)4 TimerTask (io.netty.util.TimerTask)3