org.redisson.Redisson 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 org.redisson.api.ExecutorOptions;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.MapCacheOptions;
import org.redisson.api.MapOptions;
import org.redisson.api.*;
import org.redisson.api.options.*;
import org.redisson.api.redisnode.*;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonCodec;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.config.Config;
import org.redisson.config.ConfigSupport;
import org.redisson.connection.ConnectionManager;
import org.redisson.connection.ServiceManager;
import org.redisson.eviction.EvictionScheduler;
import org.redisson.liveobject.core.RedissonObjectBuilder;
import org.redisson.redisnode.RedissonClusterNodes;
import org.redisson.redisnode.RedissonMasterSlaveNodes;
import org.redisson.redisnode.RedissonSentinelMasterSlaveNodes;
import org.redisson.redisnode.RedissonSingleNode;
import org.redisson.transaction.RedissonTransaction;
import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server.
*
* @author Nikita Koksharov
*
*/
public final class Redisson implements RedissonClient {
private final EvictionScheduler evictionScheduler;
private final WriteBehindService writeBehindService;
private final ConnectionManager connectionManager;
private final CommandAsyncExecutor commandExecutor;
private final ConcurrentMap, Class>> liveObjectClassCache = new ConcurrentHashMap<>();
private final Config config;
Redisson(Config config) {
this.config = config;
Config configCopy = new Config(config);
connectionManager = ConfigSupport.createConnectionManager(configCopy);
RedissonObjectBuilder objectBuilder = null;
if (config.isReferenceEnabled()) {
objectBuilder = new RedissonObjectBuilder(this);
}
commandExecutor = connectionManager.createCommandExecutor(objectBuilder, RedissonObjectBuilder.ReferenceType.DEFAULT);
evictionScheduler = new EvictionScheduler(commandExecutor);
writeBehindService = new WriteBehindService(commandExecutor);
}
public EvictionScheduler getEvictionScheduler() {
return evictionScheduler;
}
public CommandAsyncExecutor getCommandExecutor() {
return commandExecutor;
}
public ConnectionManager getConnectionManager() {
return connectionManager;
}
public ServiceManager getServiceManager() {
return connectionManager.getServiceManager();
}
/**
* Create sync/async Redisson instance with default config
*
* @return Redisson instance
*/
public static RedissonClient create() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://127.0.0.1:6379");
return create(config);
}
/**
* Create sync/async Redisson instance with provided config
*
* @param config for Redisson
* @return Redisson instance
*/
public static RedissonClient create(Config config) {
return new Redisson(config);
}
/*
* Use Redisson.create().rxJava() method instead
*/
@Deprecated
public static RedissonRxClient createRx() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return createRx(config);
}
/*
* Use Redisson.create(config).rxJava() method instead
*/
@Deprecated
public static RedissonRxClient createRx(Config config) {
return new RedissonRx(config);
}
@Override
public RedissonRxClient rxJava() {
return new RedissonRx(connectionManager, evictionScheduler, writeBehindService);
}
/*
* Use Redisson.create().reactive() method instead
*/
@Deprecated
public static RedissonReactiveClient createReactive() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return createReactive(config);
}
/*
* Use Redisson.create(config).reactive() method instead
*/
@Deprecated
public static RedissonReactiveClient createReactive(Config config) {
return new RedissonReactive(config);
}
@Override
public RedissonReactiveClient reactive() {
return new RedissonReactive(connectionManager, evictionScheduler, writeBehindService);
}
@Override
public RTimeSeries getTimeSeries(String name) {
return new RedissonTimeSeries<>(evictionScheduler, commandExecutor, name);
}
@Override
public RTimeSeries getTimeSeries(String name, Codec codec) {
return new RedissonTimeSeries<>(codec, evictionScheduler, commandExecutor, name);
}
@Override
public RTimeSeries getTimeSeries(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonTimeSeries<>(params.getCodec(), evictionScheduler,
commandExecutor.copy(params),
params.getName());
}
@Override
public RStream getStream(String name) {
return new RedissonStream(commandExecutor, name);
}
@Override
public RStream getStream(String name, Codec codec) {
return new RedissonStream<>(codec, commandExecutor, name);
}
@Override
public RStream getStream(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonStream<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RSearch getSearch() {
return new RedissonSearch(null, commandExecutor);
}
@Override
public RSearch getSearch(Codec codec) {
return new RedissonSearch(codec, commandExecutor);
}
@Override
public RSearch getSearch(OptionalOptions options) {
OptionalParams params = (OptionalParams) options;
return new RedissonSearch(params.getCodec(), commandExecutor.copy(params));
}
@Override
public RBinaryStream getBinaryStream(String name) {
return new RedissonBinaryStream(commandExecutor, name);
}
@Override
public RBinaryStream getBinaryStream(CommonOptions options) {
CommonParams params = (CommonParams) options;
return new RedissonBinaryStream(commandExecutor.copy(params), params.getName());
}
@Override
public RGeo getGeo(String name) {
return new RedissonGeo(commandExecutor, name, this);
}
@Override
public RGeo getGeo(String name, Codec codec) {
return new RedissonGeo(codec, commandExecutor, name, this);
}
@Override
public RGeo getGeo(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonGeo<>(params.getCodec(), commandExecutor.copy(params),
params.getName(), this);
}
@Override
public RBucket getBucket(String name) {
return new RedissonBucket(commandExecutor, name);
}
@Override
public RRateLimiter getRateLimiter(String name) {
return new RedissonRateLimiter(commandExecutor, name);
}
@Override
public RRateLimiter getRateLimiter(CommonOptions options) {
CommonParams params = (CommonParams) options;
return new RedissonRateLimiter(commandExecutor.copy(params), params.getName());
}
@Override
public RBucket getBucket(String name, Codec codec) {
return new RedissonBucket<>(codec, commandExecutor, name);
}
@Override
public RBucket getBucket(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonBucket<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RBuckets getBuckets() {
return new RedissonBuckets(commandExecutor);
}
@Override
public RBuckets getBuckets(Codec codec) {
return new RedissonBuckets(codec, commandExecutor);
}
@Override
public RBuckets getBuckets(OptionalOptions options) {
OptionalParams params = (OptionalParams) options;
return new RedissonBuckets(params.getCodec(), commandExecutor.copy(params));
}
@Override
public RJsonBucket getJsonBucket(String name, JsonCodec codec) {
return new RedissonJsonBucket<>(codec, commandExecutor, name);
}
@Override
public RJsonBucket getJsonBucket(JsonBucketOptions options) {
JsonBucketParams params = (JsonBucketParams) options;
return new RedissonJsonBucket<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RJsonBuckets getJsonBuckets(JsonCodec codec) {
return new RedissonJsonBuckets(codec, commandExecutor);
}
@Override
public RHyperLogLog getHyperLogLog(String name) {
return new RedissonHyperLogLog(commandExecutor, name);
}
@Override
public RHyperLogLog getHyperLogLog(String name, Codec codec) {
return new RedissonHyperLogLog(codec, commandExecutor, name);
}
@Override
public RHyperLogLog getHyperLogLog(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonHyperLogLog(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RList getList(String name) {
return new RedissonList(commandExecutor, name, this);
}
@Override
public RList getList(String name, Codec codec) {
return new RedissonList(codec, commandExecutor, name, this);
}
@Override
public RList getList(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonList(params.getCodec(), commandExecutor.copy(params), params.getName(), this);
}
@Override
public RListMultimap getListMultimap(String name) {
return new RedissonListMultimap(commandExecutor, name);
}
@Override
public RListMultimap getListMultimap(String name, Codec codec) {
return new RedissonListMultimap(codec, commandExecutor, name);
}
@Override
public RListMultimap getListMultimap(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonListMultimap<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RLocalCachedMap getLocalCachedMap(String name, LocalCachedMapOptions options) {
return getLocalCachedMap(name, null, options);
}
@Override
public RLocalCachedMap getLocalCachedMap(String name, Codec codec, LocalCachedMapOptions options) {
return new RedissonLocalCachedMap(codec, commandExecutor, name,
options, evictionScheduler, this, writeBehindService);
}
@Override
public RLocalCachedMap getLocalCachedMap(org.redisson.api.options.LocalCachedMapOptions options) {
LocalCachedMapParams params = (LocalCachedMapParams) options;
LocalCachedMapOptions ops = LocalCachedMapOptions.defaults()
.cacheProvider(LocalCachedMapOptions.CacheProvider.valueOf(params.getCacheProvider().toString()))
.cacheSize(params.getCacheSize())
.storeMode(LocalCachedMapOptions.StoreMode.valueOf(params.getStoreMode().toString()))
.evictionPolicy(LocalCachedMapOptions.EvictionPolicy.valueOf(params.getEvictionPolicy().toString()))
.maxIdle(params.getMaxIdleInMillis())
.loader(params.getLoader())
.loaderAsync(params.getLoaderAsync())
.reconnectionStrategy(LocalCachedMapOptions.ReconnectionStrategy.valueOf(params.getReconnectionStrategy().toString()))
.storeCacheMiss(params.isStoreCacheMiss())
.timeToLive(params.getTimeToLiveInMillis())
.syncStrategy(LocalCachedMapOptions.SyncStrategy.valueOf(params.getSyncStrategy().toString()))
.useObjectAsCacheKey(params.isUseObjectAsCacheKey())
.useTopicPattern(params.isUseTopicPattern())
.expirationEventPolicy(LocalCachedMapOptions.ExpirationEventPolicy.valueOf(params.getExpirationEventPolicy().toString()))
.writer(params.getWriter())
.writerAsync(params.getWriterAsync())
.writeBehindDelay(params.getWriteBehindDelay())
.writeBehindBatchSize(params.getWriteBehindBatchSize())
.writerRetryInterval(Duration.ofMillis(params.getWriteRetryInterval()));
if (params.getWriteMode() != null) {
ops.writeMode(MapOptions.WriteMode.valueOf(params.getWriteMode().toString()));
}
if (params.getWriteRetryAttempts() > 0) {
ops.writerRetryAttempts(params.getWriteRetryAttempts());
}
return new RedissonLocalCachedMap<>(params.getCodec(), commandExecutor.copy(params), params.getName(),
ops, evictionScheduler, this, writeBehindService);
}
@Override
public RMap getMap(String name) {
return new RedissonMap(commandExecutor, name, this, null, null);
}
@Override
public RMap getMap(String name, MapOptions options) {
return new RedissonMap(commandExecutor, name, this, options, writeBehindService);
}
@Override
public RMap getMap(org.redisson.api.options.MapOptions options) {
MapParams params = (MapParams) options;
MapOptions ops = MapOptions.defaults()
.loader(params.getLoader())
.loaderAsync(params.getLoaderAsync())
.writer(params.getWriter())
.writerAsync(params.getWriterAsync())
.writeBehindDelay(params.getWriteBehindDelay())
.writeBehindBatchSize(params.getWriteBehindBatchSize())
.writerRetryInterval(Duration.ofMillis(params.getWriteRetryInterval()));
if (params.getWriteMode() != null) {
ops.writeMode(MapOptions.WriteMode.valueOf(params.getWriteMode().toString()));
}
if (params.getWriteRetryAttempts() > 0) {
ops.writerRetryAttempts(params.getWriteRetryAttempts());
}
return new RedissonMap<>(params.getCodec(), commandExecutor.copy(params), params.getName(),
this, ops, writeBehindService);
}
@Override
public RMapCacheNative getMapCacheNative(String name) {
return new RedissonMapCacheNative<>(commandExecutor, name, this, null, null);
}
@Override
public RMapCacheNative getMapCacheNative(String name, Codec codec) {
return new RedissonMapCacheNative<>(codec, commandExecutor, name, this, null, null);
}
@Override
public RMapCacheNative getMapCacheNative(org.redisson.api.options.MapOptions options) {
MapParams params = (MapParams) options;
MapOptions ops = MapOptions.defaults()
.loader(params.getLoader())
.loaderAsync(params.getLoaderAsync())
.writer(params.getWriter())
.writerAsync(params.getWriterAsync())
.writeBehindDelay(params.getWriteBehindDelay())
.writeBehindBatchSize(params.getWriteBehindBatchSize())
.writerRetryInterval(Duration.ofMillis(params.getWriteRetryInterval()));
if (params.getWriteMode() != null) {
ops.writeMode(MapOptions.WriteMode.valueOf(params.getWriteMode().toString()));
}
if (params.getWriteRetryAttempts() > 0) {
ops.writerRetryAttempts(params.getWriteRetryAttempts());
}
return new RedissonMapCacheNative<>(params.getCodec(), commandExecutor.copy(params), params.getName(),
this, ops, writeBehindService);
}
@Override
public RSetMultimap getSetMultimap(String name) {
return new RedissonSetMultimap(commandExecutor, name);
}
@Override
public RSetMultimapCache getSetMultimapCache(String name) {
return new RedissonSetMultimapCache(evictionScheduler, commandExecutor, name);
}
@Override
public RSetMultimapCache getSetMultimapCache(String name, Codec codec) {
return new RedissonSetMultimapCache(evictionScheduler, codec, commandExecutor, name);
}
@Override
public RSetMultimapCache getSetMultimapCache(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonSetMultimapCache(evictionScheduler, params.getCodec(),
commandExecutor.copy(params), params.getName());
}
@Override
public RSetMultimapCacheNative getSetMultimapCacheNative(String name) {
return new RedissonSetMultimapCacheNative<>(commandExecutor, name);
}
@Override
public RSetMultimapCacheNative getSetMultimapCacheNative(String name, Codec codec) {
return new RedissonSetMultimapCacheNative<>(codec, commandExecutor, name);
}
@Override
public RSetMultimapCacheNative getSetMultimapCacheNative(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonSetMultimapCacheNative<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RListMultimapCache getListMultimapCache(String name) {
return new RedissonListMultimapCache(evictionScheduler, commandExecutor, name);
}
@Override
public RListMultimapCache getListMultimapCache(String name, Codec codec) {
return new RedissonListMultimapCache(evictionScheduler, codec, commandExecutor, name);
}
@Override
public RListMultimapCache getListMultimapCache(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonListMultimapCache(evictionScheduler, params.getCodec(),
commandExecutor.copy(params), params.getName());
}
@Override
public RListMultimapCacheNative getListMultimapCacheNative(String name) {
return new RedissonListMultimapCacheNative(commandExecutor, name);
}
@Override
public RListMultimapCacheNative getListMultimapCacheNative(String name, Codec codec) {
return new RedissonListMultimapCacheNative<>(codec, commandExecutor, name);
}
@Override
public RListMultimapCacheNative getListMultimapCacheNative(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonListMultimapCacheNative<>(params.getCodec(),
commandExecutor.copy(params), params.getName());
}
@Override
public RSetMultimap getSetMultimap(String name, Codec codec) {
return new RedissonSetMultimap(codec, commandExecutor, name);
}
@Override
public RSetMultimap getSetMultimap(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonSetMultimap<>(params.getCodec(), commandExecutor.copy(params), params.getName());
}
@Override
public RSetCache getSetCache(String name) {
return new RedissonSetCache(evictionScheduler, commandExecutor, name, this);
}
@Override
public RSetCache getSetCache(String name, Codec codec) {
return new RedissonSetCache(codec, evictionScheduler, commandExecutor, name, this);
}
@Override
public RSetCache getSetCache(PlainOptions options) {
PlainParams params = (PlainParams) options;
return new RedissonSetCache(params.getCodec(), evictionScheduler,
commandExecutor.copy(params), params.getName(), this);
}
@Override
public RMapCache getMapCache(String name) {
return new RedissonMapCache(evictionScheduler, commandExecutor, name, this, null, null);
}
@Override
public RMapCache getMapCache(String name, MapCacheOptions options) {
return new RedissonMapCache(evictionScheduler, commandExecutor, name, this, options, writeBehindService);
}
@Override
public RMapCache getMapCache(String name, Codec codec) {
return new RedissonMapCache(codec, evictionScheduler, commandExecutor, name, this, null, null);
}
@Override
public RMapCache getMapCache(String name, Codec codec, MapCacheOptions options) {
return new RedissonMapCache(codec, evictionScheduler, commandExecutor, name, this, options, writeBehindService);
}
@Override
public RMapCache getMapCache(org.redisson.api.options.MapCacheOptions options) {
MapCacheParams params = (MapCacheParams) options;
MapCacheOptions ops = createOptions(params);
return new RedissonMapCache<>(params.getCodec(), evictionScheduler,
commandExecutor.copy(params), params.getName(), this, ops, writeBehindService);
}
private static MapCacheOptions createOptions(MapCacheParams params) {
MapCacheOptions ops = MapCacheOptions.defaults()
.loader(params.getLoader())
.loaderAsync(params.getLoaderAsync())
.writer(params.getWriter())
.writerAsync(params.getWriterAsync())
.writeBehindDelay(params.getWriteBehindDelay())
.writeBehindBatchSize(params.getWriteBehindBatchSize())
.writerRetryInterval(Duration.ofMillis(params.getWriteRetryInterval()));
if (params.getWriteMode() != null) {
ops.writeMode(MapOptions.WriteMode.valueOf(params.getWriteMode().toString()));
}
if (params.getWriteRetryAttempts() > 0) {
ops.writerRetryAttempts(params.getWriteRetryAttempts());
}
if (params.isRemoveEmptyEvictionTask()) {
ops.removeEmptyEvictionTask();
}
return ops;
}
@Override
public RMap getMap(String name, Codec codec) {
return new RedissonMap(codec, commandExecutor, name, this, null, null);
}
@Override
public RMap getMap(String name, Codec codec, MapOptions options) {
return new RedissonMap(codec, commandExecutor, name, this, options, writeBehindService);
}
@Override
public RLock getLock(String name) {
return new RedissonLock(commandExecutor, name);
}
@Override
public RLock getLock(CommonOptions options) {
CommonParams params = (CommonParams) options;
return new RedissonLock(commandExecutor.copy(params), params.getName());
}
@Override
public RLock getSpinLock(String name) {
return getSpinLock(name, LockOptions.defaults());
}
@Override
public RLock getSpinLock(String name, LockOptions.BackOff backOff) {
return new RedissonSpinLock(commandExecutor, name, backOff);
}
@Override
public RFencedLock getFencedLock(String name) {
return new RedissonFencedLock(commandExecutor, name);
}
@Override
public RFencedLock getFencedLock(CommonOptions options) {
CommonParams params = (CommonParams) options;
return new RedissonFencedLock(commandExecutor.copy(params), params.getName());
}
@Override
public RLock getMultiLock(RLock... locks) {
return new RedissonMultiLock(locks);
}
@Override
public RLock getMultiLock(String group, Collection