帆软使用的第三方框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
16 KiB

/**
* Copyright 2018 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 com.fr.third.org.redisson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import com.fr.third.org.redisson.api.ClusterNode;
import com.fr.third.org.redisson.api.MapOptions;
import com.fr.third.org.redisson.api.Node;
import com.fr.third.org.redisson.api.NodesGroup;
import com.fr.third.org.redisson.api.RAtomicDoubleReactive;
import com.fr.third.org.redisson.api.RAtomicLongReactive;
import com.fr.third.org.redisson.api.RBatchReactive;
import com.fr.third.org.redisson.api.RBitSetReactive;
import com.fr.third.org.redisson.api.RBlockingQueueReactive;
import com.fr.third.org.redisson.api.RBucketReactive;
import com.fr.third.org.redisson.api.RDequeReactive;
import com.fr.third.org.redisson.api.RFuture;
import com.fr.third.org.redisson.api.RHyperLogLogReactive;
import com.fr.third.org.redisson.api.RKeysReactive;
import com.fr.third.org.redisson.api.RLexSortedSetReactive;
import com.fr.third.org.redisson.api.RListMultimapReactive;
import com.fr.third.org.redisson.api.RListReactive;
import com.fr.third.org.redisson.api.RLockReactive;
import com.fr.third.org.redisson.api.RMapCacheReactive;
import com.fr.third.org.redisson.api.RMapReactive;
import com.fr.third.org.redisson.api.RPatternTopicReactive;
import com.fr.third.org.redisson.api.RQueueReactive;
import com.fr.third.org.redisson.api.RReadWriteLockReactive;
import com.fr.third.org.redisson.api.RScoredSortedSetReactive;
import com.fr.third.org.redisson.api.RScriptReactive;
import com.fr.third.org.redisson.api.RSemaphoreReactive;
import com.fr.third.org.redisson.api.RSetCacheReactive;
import com.fr.third.org.redisson.api.RSetMultimapReactive;
import com.fr.third.org.redisson.api.RSetReactive;
import com.fr.third.org.redisson.api.RTopicReactive;
import com.fr.third.org.redisson.api.RedissonReactiveClient;
import com.fr.third.org.redisson.client.codec.Codec;
import com.fr.third.org.redisson.client.protocol.RedisCommands;
import com.fr.third.org.redisson.codec.ReferenceCodecProvider;
import com.fr.third.org.redisson.config.Config;
import com.fr.third.org.redisson.config.ConfigSupport;
import com.fr.third.org.redisson.pubsub.SemaphorePubSub;
import com.fr.third.org.redisson.api.ClusterNode;
import com.fr.third.org.redisson.api.MapOptions;
import com.fr.third.org.redisson.api.Node;
import com.fr.third.org.redisson.api.NodesGroup;
import com.fr.third.org.redisson.api.RAtomicDoubleReactive;
import com.fr.third.org.redisson.api.RAtomicLongReactive;
import com.fr.third.org.redisson.api.RBatchReactive;
import com.fr.third.org.redisson.api.RBitSetReactive;
import com.fr.third.org.redisson.api.RBlockingQueueReactive;
import com.fr.third.org.redisson.api.RBucketReactive;
import com.fr.third.org.redisson.api.RDequeReactive;
import com.fr.third.org.redisson.api.RFuture;
import com.fr.third.org.redisson.api.RHyperLogLogReactive;
import com.fr.third.org.redisson.api.RKeysReactive;
import com.fr.third.org.redisson.api.RLexSortedSetReactive;
import com.fr.third.org.redisson.api.RListMultimapReactive;
import com.fr.third.org.redisson.api.RListReactive;
import com.fr.third.org.redisson.api.RLockReactive;
import com.fr.third.org.redisson.api.RMapCacheReactive;
import com.fr.third.org.redisson.api.RMapReactive;
import com.fr.third.org.redisson.api.RPatternTopicReactive;
import com.fr.third.org.redisson.api.RQueueReactive;
import com.fr.third.org.redisson.api.RReadWriteLockReactive;
import com.fr.third.org.redisson.api.RScoredSortedSetReactive;
import com.fr.third.org.redisson.api.RScriptReactive;
import com.fr.third.org.redisson.api.RSemaphoreReactive;
import com.fr.third.org.redisson.api.RSetCacheReactive;
import com.fr.third.org.redisson.api.RSetMultimapReactive;
import com.fr.third.org.redisson.api.RSetReactive;
import com.fr.third.org.redisson.api.RTopicReactive;
import com.fr.third.org.redisson.api.RedissonReactiveClient;
import com.fr.third.org.redisson.client.codec.Codec;
import com.fr.third.org.redisson.client.protocol.RedisCommands;
import com.fr.third.org.redisson.codec.ReferenceCodecProvider;
import com.fr.third.org.redisson.command.CommandReactiveService;
import com.fr.third.org.redisson.config.Config;
import com.fr.third.org.redisson.config.ConfigSupport;
import com.fr.third.org.redisson.connection.ConnectionManager;
import com.fr.third.org.redisson.eviction.EvictionScheduler;
import com.fr.third.org.redisson.pubsub.SemaphorePubSub;
import com.fr.third.org.redisson.reactive.RedissonAtomicDoubleReactive;
import com.fr.third.org.redisson.reactive.RedissonAtomicLongReactive;
import com.fr.third.org.redisson.reactive.RedissonBatchReactive;
import com.fr.third.org.redisson.reactive.RedissonBitSetReactive;
import com.fr.third.org.redisson.reactive.RedissonBlockingQueueReactive;
import com.fr.third.org.redisson.reactive.RedissonBucketReactive;
import com.fr.third.org.redisson.reactive.RedissonDequeReactive;
import com.fr.third.org.redisson.reactive.RedissonHyperLogLogReactive;
import com.fr.third.org.redisson.reactive.RedissonKeysReactive;
import com.fr.third.org.redisson.reactive.RedissonLexSortedSetReactive;
import com.fr.third.org.redisson.reactive.RedissonListMultimapReactive;
import com.fr.third.org.redisson.reactive.RedissonListReactive;
import com.fr.third.org.redisson.reactive.RedissonLockReactive;
import com.fr.third.org.redisson.reactive.RedissonMapCacheReactive;
import com.fr.third.org.redisson.reactive.RedissonMapReactive;
import com.fr.third.org.redisson.reactive.RedissonPatternTopicReactive;
import com.fr.third.org.redisson.reactive.RedissonQueueReactive;
import com.fr.third.org.redisson.reactive.RedissonReadWriteLockReactive;
import com.fr.third.org.redisson.reactive.RedissonScoredSortedSetReactive;
import com.fr.third.org.redisson.reactive.RedissonScriptReactive;
import com.fr.third.org.redisson.reactive.RedissonSemaphoreReactive;
import com.fr.third.org.redisson.reactive.RedissonSetCacheReactive;
import com.fr.third.org.redisson.reactive.RedissonSetMultimapReactive;
import com.fr.third.org.redisson.reactive.RedissonSetReactive;
import com.fr.third.org.redisson.reactive.RedissonTopicReactive;
/**
* Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server.
*
* @author Nikita Koksharov
*
*/
public class RedissonReactive implements RedissonReactiveClient {
protected final EvictionScheduler evictionScheduler;
protected final CommandReactiveService commandExecutor;
protected final ConnectionManager connectionManager;
protected final Config config;
protected final ReferenceCodecProvider codecProvider;
protected final UUID id = UUID.randomUUID();
protected final SemaphorePubSub semaphorePubSub = new SemaphorePubSub();
protected RedissonReactive(Config config) {
this.config = config;
Config configCopy = new Config(config);
connectionManager = ConfigSupport.createConnectionManager(configCopy);
commandExecutor = new CommandReactiveService(connectionManager);
evictionScheduler = new EvictionScheduler(commandExecutor);
codecProvider = config.getReferenceCodecProvider();
}
@Override
public RSemaphoreReactive getSemaphore(String name) {
return new RedissonSemaphoreReactive(commandExecutor, name, semaphorePubSub);
}
@Override
public RReadWriteLockReactive getReadWriteLock(String name) {
return new RedissonReadWriteLockReactive(commandExecutor, name);
}
@Override
public RLockReactive getLock(String name) {
return new RedissonLockReactive(commandExecutor, name);
}
@Override
public <K, V> RMapCacheReactive<K, V> getMapCache(String name, Codec codec) {
return new RedissonMapCacheReactive<K, V>(evictionScheduler, codec, commandExecutor, name, null);
}
@Override
public <K, V> RMapCacheReactive<K, V> getMapCache(String name) {
return new RedissonMapCacheReactive<K, V>(evictionScheduler, commandExecutor, name, null);
}
@Override
public <V> RBucketReactive<V> getBucket(String name) {
return new RedissonBucketReactive<V>(commandExecutor, name);
}
@Override
public <V> RBucketReactive<V> getBucket(String name, Codec codec) {
return new RedissonBucketReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> List<RBucketReactive<V>> findBuckets(String pattern) {
RFuture<Collection<String>> r = commandExecutor.readAllAsync(RedisCommands.KEYS, pattern);
Collection<String> keys = commandExecutor.get(r);
List<RBucketReactive<V>> buckets = new ArrayList<RBucketReactive<V>>(keys.size());
for (Object key : keys) {
if(key != null) {
buckets.add(this.<V>getBucket(key.toString()));
}
}
return buckets;
}
@Override
public <V> RHyperLogLogReactive<V> getHyperLogLog(String name) {
return new RedissonHyperLogLogReactive<V>(commandExecutor, name);
}
@Override
public <V> RHyperLogLogReactive<V> getHyperLogLog(String name, Codec codec) {
return new RedissonHyperLogLogReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> RListReactive<V> getList(String name) {
return new RedissonListReactive<V>(commandExecutor, name);
}
@Override
public <V> RListReactive<V> getList(String name, Codec codec) {
return new RedissonListReactive<V>(codec, commandExecutor, name);
}
@Override
public <K, V> RListMultimapReactive<K, V> getListMultimap(String name) {
return new RedissonListMultimapReactive<K, V>(id, commandExecutor, name);
}
@Override
public <K, V> RListMultimapReactive<K, V> getListMultimap(String name, Codec codec) {
return new RedissonListMultimapReactive<K, V>(id, codec, commandExecutor, name);
}
@Override
public <K, V> RSetMultimapReactive<K, V> getSetMultimap(String name) {
return new RedissonSetMultimapReactive<K, V>(id, commandExecutor, name);
}
@Override
public <K, V> RSetMultimapReactive<K, V> getSetMultimap(String name, Codec codec) {
return new RedissonSetMultimapReactive<K, V>(id, codec, commandExecutor, name);
}
@Override
public <K, V> RMapReactive<K, V> getMap(String name) {
return new RedissonMapReactive<K, V>(commandExecutor, name, null);
}
@Override
public <K, V> RMapReactive<K, V> getMap(String name, Codec codec) {
return new RedissonMapReactive<K, V>(codec, commandExecutor, name, null);
}
@Override
public <V> RSetReactive<V> getSet(String name) {
return new RedissonSetReactive<V>(commandExecutor, name);
}
@Override
public <V> RSetReactive<V> getSet(String name, Codec codec) {
return new RedissonSetReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> RScoredSortedSetReactive<V> getScoredSortedSet(String name) {
return new RedissonScoredSortedSetReactive<V>(commandExecutor, name);
}
@Override
public <V> RScoredSortedSetReactive<V> getScoredSortedSet(String name, Codec codec) {
return new RedissonScoredSortedSetReactive<V>(codec, commandExecutor, name);
}
@Override
public RLexSortedSetReactive getLexSortedSet(String name) {
return new RedissonLexSortedSetReactive(commandExecutor, name);
}
@Override
public <M> RTopicReactive<M> getTopic(String name) {
return new RedissonTopicReactive<M>(commandExecutor, name);
}
@Override
public <M> RTopicReactive<M> getTopic(String name, Codec codec) {
return new RedissonTopicReactive<M>(codec, commandExecutor, name);
}
@Override
public <M> RPatternTopicReactive<M> getPatternTopic(String pattern) {
return new RedissonPatternTopicReactive<M>(commandExecutor, pattern);
}
@Override
public <M> RPatternTopicReactive<M> getPatternTopic(String pattern, Codec codec) {
return new RedissonPatternTopicReactive<M>(codec, commandExecutor, pattern);
}
@Override
public <V> RQueueReactive<V> getQueue(String name) {
return new RedissonQueueReactive<V>(commandExecutor, name);
}
@Override
public <V> RQueueReactive<V> getQueue(String name, Codec codec) {
return new RedissonQueueReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> RBlockingQueueReactive<V> getBlockingQueue(String name) {
return new RedissonBlockingQueueReactive<V>(commandExecutor, name);
}
@Override
public <V> RBlockingQueueReactive<V> getBlockingQueue(String name, Codec codec) {
return new RedissonBlockingQueueReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> RDequeReactive<V> getDeque(String name) {
return new RedissonDequeReactive<V>(commandExecutor, name);
}
@Override
public <V> RDequeReactive<V> getDeque(String name, Codec codec) {
return new RedissonDequeReactive<V>(codec, commandExecutor, name);
}
@Override
public <V> RSetCacheReactive<V> getSetCache(String name) {
return new RedissonSetCacheReactive<V>(evictionScheduler, commandExecutor, name);
}
@Override
public <V> RSetCacheReactive<V> getSetCache(String name, Codec codec) {
return new RedissonSetCacheReactive<V>(codec, evictionScheduler, commandExecutor, name);
}
@Override
public RAtomicLongReactive getAtomicLong(String name) {
return new RedissonAtomicLongReactive(commandExecutor, name);
}
@Override
public RAtomicDoubleReactive getAtomicDouble(String name) {
return new RedissonAtomicDoubleReactive(commandExecutor, name);
}
@Override
public RBitSetReactive getBitSet(String name) {
return new RedissonBitSetReactive(commandExecutor, name);
}
@Override
public RScriptReactive getScript() {
return new RedissonScriptReactive(commandExecutor);
}
@Override
public RBatchReactive createBatch() {
RedissonBatchReactive batch = new RedissonBatchReactive(evictionScheduler, connectionManager);
if (config.isReferenceEnabled()) {
batch.enableRedissonReferenceSupport(this);
}
return batch;
}
@Override
public RKeysReactive getKeys() {
return new RedissonKeysReactive(commandExecutor);
}
@Override
public Config getConfig() {
return config;
}
@Override
public ReferenceCodecProvider getCodecProvider() {
return codecProvider;
}
@Override
public NodesGroup<Node> getNodesGroup() {
return new RedisNodes<Node>(connectionManager);
}
@Override
public NodesGroup<ClusterNode> getClusterNodesGroup() {
if (!connectionManager.isClusterMode()) {
throw new IllegalStateException("Redisson not in cluster mode!");
}
return new RedisNodes<ClusterNode>(connectionManager);
}
@Override
public void shutdown() {
connectionManager.shutdown();
}
@Override
public boolean isShutdown() {
return connectionManager.isShutdown();
}
@Override
public boolean isShuttingDown() {
return connectionManager.isShuttingDown();
}
protected void enableRedissonReferenceSupport() {
this.commandExecutor.enableRedissonReferenceSupport(this);
}
@Override
public <K, V> RMapCacheReactive<K, V> getMapCache(String name, Codec codec, MapOptions<K, V> options) {
return new RedissonMapCacheReactive<K, V>(evictionScheduler, codec, commandExecutor, name, options);
}
@Override
public <K, V> RMapCacheReactive<K, V> getMapCache(String name, MapOptions<K, V> options) {
return new RedissonMapCacheReactive<K, V>(evictionScheduler, commandExecutor, name, options);
}
@Override
public <K, V> RMapReactive<K, V> getMap(String name, MapOptions<K, V> options) {
return new RedissonMapReactive<K, V>(commandExecutor, name, options);
}
@Override
public <K, V> RMapReactive<K, V> getMap(String name, Codec codec, MapOptions<K, V> options) {
return new RedissonMapReactive<K, V>(codec, commandExecutor, name, options);
}
}