org.redisson.RedissonRingBuffer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* Copyright (c) 2013-2024 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RFuture;
import org.redisson.api.RRingBuffer;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.convertor.IntegerReplayConvertor;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.CompletableFutureWrapper;
/**
*
* @author Nikita Koksharov
*
* @param value type
*/
public class RedissonRingBuffer extends RedissonQueue implements RRingBuffer {
private static final RedisStrictCommand GET_INTEGER = new RedisStrictCommand("GET", new IntegerReplayConvertor(0));
private final String settingsName;
public RedissonRingBuffer(CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(commandExecutor, name, redisson);
settingsName = prefixName("redisson_rb", getRawName());
}
public RedissonRingBuffer(Codec codec, CommandAsyncExecutor commandExecutor, String name, RedissonClient redisson) {
super(codec, commandExecutor, name, redisson);
settingsName = prefixName("redisson_rb", getRawName());
}
@Override
public RFuture trySetCapacityAsync(int capacity) {
return commandExecutor.writeAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.SETNX, settingsName, capacity);
}
@Override
public boolean trySetCapacity(int capacity) {
return get(trySetCapacityAsync(capacity));
}
@Override
public RFuture setCapacityAsync(int capacity) {
return commandExecutor.evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_VOID,
"redis.call('set', KEYS[2], ARGV[1]); " +
"local len = redis.call('llen', KEYS[1]); " +
"if len > tonumber(ARGV[1]) then " +
"redis.call('ltrim', KEYS[1], len - tonumber(ARGV[1]), -1); " +
"end; ",
Arrays.asList(getRawName(), settingsName), capacity);
}
@Override
public void setCapacity(int capacity) {
get(setCapacityAsync(capacity));
}
@Override
public RFuture addAsync(V e) {
return commandExecutor.evalWriteNoRetryAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local limit = redis.call('get', KEYS[2]); "
+ "assert(limit ~= false, 'RingBuffer capacity is not defined'); "
+ "local size = redis.call('rpush', KEYS[1], ARGV[1]); "
+ "if size > tonumber(limit) then "
+ "redis.call('lpop', KEYS[1]); "
+ "end; "
+ "return 1; ",
Arrays.asList(getRawName(), settingsName), encode(e));
}
@Override
public RFuture addAllAsync(Collection extends V> c) {
if (c.isEmpty()) {
return new CompletableFutureWrapper<>(false);
}
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy