com.github.lontime.extredisson.RedissonInstance Maven / Gradle / Ivy
package com.github.lontime.extredisson;
import com.github.lontime.base.commonj.utils.Holder;
import com.github.lontime.base.commonj.utils.LoggerHelper;
import com.github.lontime.extredisson.provider.DefaultRedissonProvider;
import com.github.lontime.extredisson.provider.RedissonProvider;
import com.github.lontime.extredisson.provider.RedissonReactiveProvider;
import com.github.lontime.extredisson.provider.RedissonRxProvider;
/**
* RedissonInstance.
* @author lontime
* @since 1.0
*/
@SuppressWarnings("unchecked")
public class RedissonInstance {
private static Holder globalHolder = new Holder<>();
private static final Object globalLock = new Object();
public static RedissonProvider get() {
if (null == globalHolder.get()) {
synchronized (globalLock) {
if (null == globalHolder.get()) {
globalHolder.set(DefaultRedissonProvider.create());
}
}
}
return globalHolder.get();
}
public static DefaultRedissonProvider getDefault() {
return (DefaultRedissonProvider) get();
}
public static RedissonRxProvider getRx() {
if (null == globalHolder.get()) {
synchronized (globalLock) {
if (null == globalHolder.get()) {
globalHolder.set(RedissonRxProvider.create());
}
}
}
return (RedissonRxProvider) globalHolder.get();
}
public static RedissonReactiveProvider getReactive() {
if (null == globalHolder.get()) {
synchronized (globalLock) {
if (null == globalHolder.get()) {
globalHolder.set(RedissonReactiveProvider.create());
}
}
}
return (RedissonReactiveProvider) globalHolder.get();
}
public static RedissonProvider set(RedissonProvider custom) {
synchronized (globalLock) {
final RedissonProvider provider = globalHolder.get();
if (null != provider) {
if (provider.equals(custom)) {
return provider;
}
LoggerHelper.infov("close {0} now {1}", provider.getName(), custom.getName());
provider.close();
}
globalHolder.set(custom);
}
return globalHolder.get();
}
}