Search in sources :

Example 6 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class TestRedisCache2 method testPipeline.

@Test
public void testPipeline() {
    BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("friend", new LettuceConnectSourceConfig());
    // while (true){
    List<PipelineCmd> commands = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        commands.add(new PipelineGet("test"));
    }
    commands.add(new PipelineZremRangeByScore("test111", 0, System.currentTimeMillis()));
    // 同步方式
    List<Object> resList = baseCacheExecutor.pSync(commands);
    System.out.println(JSON.toJSONString(resList));
    // 异步方式
    CompletableFuture<List<Object>> listCompletableFuture = baseCacheExecutor.pAsync(commands);
// }
}
Also used : PipelineCmd(com.lcache.extend.handle.pipeline.PipelineCmd) PipelineZremRangeByScore(com.lcache.extend.handle.pipeline.PipelineZremRangeByScore) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) PipelineGet(com.lcache.extend.handle.pipeline.PipelineGet) Test(org.junit.Test)

Example 7 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceClusterPoolConnection.

/**
 * 获取Lettuce集群连接池
 *
 * @param lettuceClusterConnectSourceConfig
 * @return
 */
public GenericObjectPool getLettuceClusterPoolConnection(LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(lettuceClusterConnectSourceConfig.getHosts().toString(), 0, lettuceClusterConnectSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), lettuceClusterConnectSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    List<RedisURI> nodeConfigs = new ArrayList<>(lettuceClusterConnectSourceConfig.getNodes().size());
    for (LettuceConnectSourceConfig redisSourceConfig : lettuceClusterConnectSourceConfig.getNodes()) {
        nodeConfigs.add(this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    }
    RedisClusterClient client = RedisClusterClient.create(res, nodeConfigs);
    client.setDefaultTimeout(Duration.ofMillis(lettuceClusterConnectSourceConfig.getSoTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClusterClientOptions(lettuceClusterConnectSourceConfig));
    return (GenericObjectPool) this.execute(() -> {
        return ConnectionPoolSupport.createGenericObjectPool(() -> client.connect(), CacheConfigBuildUtils.getJedisPoolConfig(lettuceClusterConnectSourceConfig.getCommonCacheConfig()));
    });
}
Also used : RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Example 8 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceClusterPubSubConnection.

/**
 * 获取集群方式发布订阅连接
 *
 * @param lettuceClusterConnectSourceConfig
 * @return
 */
public StatefulRedisClusterPubSubConnection<String, String> getLettuceClusterPubSubConnection(LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(lettuceClusterConnectSourceConfig.getHosts().toString(), 0, lettuceClusterConnectSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), lettuceClusterConnectSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    List<RedisURI> nodeConfigs = new ArrayList<>(lettuceClusterConnectSourceConfig.getNodes().size());
    for (LettuceConnectSourceConfig redisSourceConfig : lettuceClusterConnectSourceConfig.getNodes()) {
        nodeConfigs.add(this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    }
    RedisClusterClient client = RedisClusterClient.create(res, nodeConfigs);
    client.setDefaultTimeout(Duration.ofMillis(lettuceClusterConnectSourceConfig.getSoTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClusterClientOptions(lettuceClusterConnectSourceConfig));
    return (StatefulRedisClusterPubSubConnection<String, String>) this.execute(() -> {
        return client.connectPubSub();
    });
}
Also used : RedisURI(io.lettuce.core.RedisURI) StatefulRedisClusterPubSubConnection(io.lettuce.core.cluster.pubsub.StatefulRedisClusterPubSubConnection) ArrayList(java.util.ArrayList) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Example 9 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class LettuceConnectionFactory method getLettuceClusterConnection.

/**
 * 获取Lettuce集群
 *
 * @param lettuceClusterConnectSourceConfig
 * @return
 */
public StatefulRedisClusterConnection<String, String> getLettuceClusterConnection(LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig) {
    // 设置线程
    DefaultClientResources res = this.getDefaultClientResources(lettuceClusterConnectSourceConfig.getHosts().toString(), 0, lettuceClusterConnectSourceConfig.getCommonCacheConfig().getIoThreadPoolSize(), lettuceClusterConnectSourceConfig.getCommonCacheConfig().getComputationThreadPoolSize());
    List<RedisURI> nodeConfigs = new ArrayList<>(lettuceClusterConnectSourceConfig.getNodes().size());
    for (LettuceConnectSourceConfig redisSourceConfig : lettuceClusterConnectSourceConfig.getNodes()) {
        nodeConfigs.add(this.getRedisUri(redisSourceConfig.getHost(), redisSourceConfig.getPort(), redisSourceConfig.getPwd(), redisSourceConfig.getDatabase(), redisSourceConfig.getTimeout()));
    }
    RedisClusterClient client = RedisClusterClient.create(res, nodeConfigs);
    client.setDefaultTimeout(Duration.ofMillis(lettuceClusterConnectSourceConfig.getSoTimeout()));
    client.setOptions(CacheConfigBuildUtils.getClusterClientOptions(lettuceClusterConnectSourceConfig));
    return (StatefulRedisClusterConnection<String, String>) this.execute(() -> {
        return client.connect();
    });
}
Also used : StatefulRedisClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection) RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient) DefaultClientResources(io.lettuce.core.resource.DefaultClientResources)

Example 10 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class RedissonClientManager method getLettuceRedissonConfig.

/**
 * Lettuce配置转Redisson配置
 *
 * @param cacheConfigModel
 * @param cacheConfig
 * @return
 */
private static Config getLettuceRedissonConfig(CacheConfigModel cacheConfigModel, BaseCacheConfig cacheConfig) {
    Config config = new Config();
    switch(cacheConfigModel.getConnectTypeEnum()) {
        case SIMPLE:
            LettuceConnectSourceConfig simpleConfig = (LettuceConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + simpleConfig.getHost() + ":" + simpleConfig.getPort()).setPassword(StringUtils.isNotBlank(simpleConfig.getPwd()) ? simpleConfig.getPwd() : null);
            break;
        case POOL:
            LettuceConnectSourceConfig poolConfig = (LettuceConnectSourceConfig) cacheConfig;
            config.useSingleServer().setAddress("redis://" + poolConfig.getHost() + ":" + poolConfig.getPort()).setPassword(StringUtils.isNotBlank(poolConfig.getPwd()) ? poolConfig.getPwd() : null);
            break;
        case SHARDED:
            return null;
        case CLUSTER:
            LettuceClusterConnectSourceConfig clusterConfig = (LettuceClusterConnectSourceConfig) cacheConfig;
            String pwd = "";
            for (LettuceConnectSourceConfig hostAndPort : clusterConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
                pwd = hostAndPort.getPwd();
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(pwd) ? pwd : null);
            break;
        case CLUSTER_POOL:
            LettuceClusterConnectSourceConfig clusterPoolConfig = (LettuceClusterConnectSourceConfig) cacheConfig;
            String pwd1 = "";
            for (LettuceConnectSourceConfig hostAndPort : clusterPoolConfig.getNodes()) {
                config.useClusterServers().addNodeAddress("redis://" + hostAndPort.getHost() + ":" + hostAndPort.getPort());
                pwd1 = hostAndPort.getPwd();
            }
            config.useClusterServers().setScanInterval(2000).setPassword(StringUtils.isNotBlank(pwd1) ? pwd1 : null);
            break;
        default:
            return null;
    }
    return config;
}
Also used : BaseCacheConfig(com.lcache.config.BaseCacheConfig) JedisClusterConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Config(org.redisson.config.Config) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) LettuceClusterConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceClusterConnectSourceConfig)

Aggregations

LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)17 Test (org.junit.Test)13 BaseCacheExecutor (com.lcache.core.BaseCacheExecutor)12 ExecutionException (java.util.concurrent.ExecutionException)8 ArrayList (java.util.ArrayList)6 JedisConnectSourceConfig (com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 CacheConfigModel (com.lcache.core.model.CacheConfigModel)3 PipelineCmd (com.lcache.extend.handle.pipeline.PipelineCmd)3 PipelineGet (com.lcache.extend.handle.pipeline.PipelineGet)3 PipelineZremRangeByScore (com.lcache.extend.handle.pipeline.PipelineZremRangeByScore)3 RedisURI (io.lettuce.core.RedisURI)3 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)3 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)3 List (java.util.List)3 Map (java.util.Map)3 RLock (org.redisson.api.RLock)3 JSON (com.alibaba.fastjson.JSON)2 CacheClientFactory (com.lcache.client.CacheClientFactory)2 LcacheCaffeineLocalCache (com.lcache.core.cache.localcache.LcacheCaffeineLocalCache)2