com.github.lontime.extredisson.provider.DefaultRedissonProvider Maven / Gradle / Ivy
package com.github.lontime.extredisson.provider;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.github.lontime.base.commonj.utils.LoggerHelper;
import com.github.lontime.extredisson.cache.ExtRLocalCachedMapCache;
import com.github.lontime.extredisson.cache.ExtRedissonLocalCachedMapCache;
import com.github.lontime.extredisson.configuration.ConnectionOption;
import org.redisson.Redisson;
import org.redisson.WriteBehindService;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
/**
* DefaultRedissonProvider.
* @author lontime
* @since 1.0
*/
public class DefaultRedissonProvider extends AbstractRedissonProvider {
private final Map map = new ConcurrentHashMap<>();
public static DefaultRedissonProvider create() {
return new DefaultRedissonProvider();
}
@Override
public String getName() {
return "redisson[default]";
}
@Override
@SuppressWarnings("unchecked")
public T client(String name) {
lazyReentrantStart();
final String actualName = resolveName(name);
if (!map.containsKey(actualName)) {
throw new IllegalArgumentException("Illegal name '" + actualName + "'");
}
final RedissonClient client = map.get(actualName).getClient();
if (client.isShutdown() || client.isShuttingDown()) {
LoggerHelper.warn(getClass(), "redisson is shutdowning, {0}-{1}", client.isShutdown(), client.isShuttingDown());
throw new IllegalMonitorStateException("Redisson is shutdowning");
}
return (T) client;
}
@Override
public ExtRLocalCachedMapCache getLocalCachedMapCache(String name, String objectName, Codec codec, LocalCachedMapOptions options) {
return getLocalCachedMapCacheInternal(client(name), objectName, codec, options);
}
private ExtRLocalCachedMapCache getLocalCachedMapCacheInternal(Redisson redisson, String objectName, Codec codec, LocalCachedMapOptions options) {
final WriteBehindService writeBehindService = new WriteBehindService(redisson.getCommandExecutor());
if (codec == null) {
return new ExtRedissonLocalCachedMapCache<>(redisson.getCommandExecutor(), objectName,
options, redisson.getEvictionScheduler(), redisson, writeBehindService);
}
return new ExtRedissonLocalCachedMapCache<>(codec, redisson.getCommandExecutor(), objectName,
options, redisson.getEvictionScheduler(), redisson, writeBehindService);
}
@Override
protected void loadIfAbsent(ConnectionOption options) {
map.computeIfAbsent(options.getName(), name -> RedissonClientWrapper.from(load(options), options));
}
@Override
protected void shutdownClient() {
map.forEach((k, v) -> {
final RedissonClient client = v.getClient();
if (client.isShutdown() || client.isShuttingDown()) {
return;
}
client.shutdown();
});
}
@Override
public List getKeys() {
return new ArrayList<>(map.keySet());
}
}