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