
org.redisson.reactive.RedissonSetCacheReactive Maven / Gradle / Ivy
/**
* Copyright 2016 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.reactive;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import org.redisson.RedissonSetCache;
import org.redisson.api.RFuture;
import org.redisson.api.RSetCacheReactive;
import org.redisson.client.RedisClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.client.protocol.decoder.ScanObjectEntry;
import org.redisson.command.CommandReactiveExecutor;
import org.redisson.eviction.EvictionScheduler;
import io.netty.buffer.ByteBuf;
import reactor.core.publisher.Flux;
/**
* Set-based cache with ability to set TTL for each entry via
* {@link #add(Object, long, TimeUnit)} method.
* And therefore has an complex lua-scripts inside.
* Uses map(value_hash, value) to tie with sorted set which contains expiration record for every value with TTL.
*
*
* Current Redis implementation doesn't have set entry eviction functionality.
* Thus values are checked for TTL expiration during any value read operation.
* If entry expired then it doesn't returns and clean task runs hronous.
* Clean task deletes removes 100 expired entries at once.
* In addition there is {@link org.redisson.eviction.EvictionScheduler}. This scheduler
* deletes expired entries in time interval between 5 seconds to 2 hours.
*
* If eviction is not required then it's better to use {@link org.redisson.api.RSet}.
*
* @author Nikita Koksharov
*
* @param value
*/
public class RedissonSetCacheReactive extends RedissonExpirableReactive implements RSetCacheReactive {
private final RedissonSetCache instance;
public RedissonSetCacheReactive(EvictionScheduler evictionScheduler, CommandReactiveExecutor commandExecutor, String name) {
super(commandExecutor, name);
instance = new RedissonSetCache(evictionScheduler, commandExecutor, name, null);
}
public RedissonSetCacheReactive(Codec codec, EvictionScheduler evictionScheduler, CommandReactiveExecutor commandExecutor, String name) {
super(codec, commandExecutor, name);
instance = new RedissonSetCache(codec, evictionScheduler, commandExecutor, name, null);
}
@Override
public Publisher size() {
return commandExecutor.readReactive(getName(), codec, RedisCommands.ZCARD_INT, getName());
}
@Override
public Publisher contains(final Object o) {
return reactive(new Supplier>() {
@Override
public RFuture get() {
return instance.containsAsync(o);
}
});
}
Publisher> scanIterator(final RedisClient client, final long startPos) {
return reactive(new Supplier>>() {
@Override
public RFuture> get() {
return instance.scanIteratorAsync(getName(), client, startPos, null);
}
});
}
@Override
public Publisher iterator() {
return Flux.create(new SetReactiveIterator() {
@Override
protected Publisher> scanIteratorReactive(RedisClient client, long nextIterPos) {
return RedissonSetCacheReactive.this.scanIterator(client, nextIterPos);
}
});
}
@Override
public Publisher add(final V value, final long ttl, final TimeUnit unit) {
return reactive(new Supplier>() {
@Override
public RFuture get() {
return instance.addAsync(value, ttl, unit);
}
});
}
@Override
public Publisher add(V value) {
long timeoutDate = 92233720368547758L;
return commandExecutor.evalWriteReactive(getName(), codec, RedisCommands.EVAL_INTEGER,
"local expireDateScore = redis.call('zscore', KEYS[1], ARGV[3]); "
+ "if expireDateScore ~= false and tonumber(expireDateScore) > tonumber(ARGV[1]) then "
+ "return 0;"
+ "end; " +
"redis.call('zadd', KEYS[1], ARGV[2], ARGV[3]); " +
"return 1; ",
Arrays.