Search in sources :

Example 1 with StatefulConnection

use of io.lettuce.core.api.StatefulConnection in project lcache by long172066912.

the class AbstractLettuceHandleExecutor method getStatefulConnection.

private StatefulConnection getStatefulConnection() {
    StatefulConnection localResource = statefulConnection.get();
    if (null != localResource) {
        return localResource;
    }
    localResource = this.getStatefulConnection(this.connectResource);
    statefulConnection.set(localResource);
    return localResource;
}
Also used : StatefulConnection(io.lettuce.core.api.StatefulConnection)

Example 2 with StatefulConnection

use of io.lettuce.core.api.StatefulConnection in project togglz by togglz.

the class RedisLettuceStateRepositoryTest method testFormatOfExistingFeatureState.

@Test
public void testFormatOfExistingFeatureState() throws Exception {
    // set contents in Redis directly, without using the RedisStateRepository API
    final GenericObjectPool<StatefulConnection<String, String>> lettucePool = createPool();
    try (final StatefulRedisConnection<String, String> connection = (StatefulRedisConnection<String, String>) lettucePool.borrowObject()) {
        final String key = "feature-toggles:A_FEATURE";
        connection.sync().hset(key, "enabled", "true");
        connection.sync().hset(key, "strategy", "TIT_FOR_TAT");
        connection.sync().hset(key, "parameter:MEANING_OF_LIFE", "42");
    }
    final Feature feature = new NamedFeature("A_FEATURE");
    final FeatureState expectedFeatureState = new FeatureState(feature, true);
    expectedFeatureState.setStrategyId("TIT_FOR_TAT");
    expectedFeatureState.setParameter("MEANING_OF_LIFE", "42");
    final FeatureState storedFeatureState = aRedisStateRepository().getFeatureState(feature);
    assertTrue(EqualsBuilder.reflectionEquals(expectedFeatureState, storedFeatureState, true));
}
Also used : NamedFeature(org.togglz.core.util.NamedFeature) StatefulConnection(io.lettuce.core.api.StatefulConnection) NamedFeature(org.togglz.core.util.NamedFeature) Feature(org.togglz.core.Feature) FeatureState(org.togglz.core.repository.FeatureState) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) Test(org.junit.jupiter.api.Test)

Example 3 with StatefulConnection

use of io.lettuce.core.api.StatefulConnection in project lcache by long172066912.

the class TestRedisCache2 method testPubSub.

@Test
public void testPubSub() {
    for (int p = 0; p < 10; p++) {
        int finalP = p;
        new Thread(() -> {
            BaseCacheExecutor baseCacheExecutor = null;
            if (finalP % 2 == 0) {
                baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
            } else {
                baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
            }
            int i = 0;
            while (true) {
                i++;
                System.out.println("Lettuce发布消息:" + i);
                try {
                    baseCacheExecutor.publish("test", "test" + i);
                    Thread.sleep(1000L);
                } catch (Exception e) {
                    e.printStackTrace();
                    StatefulConnection connectResource = (StatefulConnection) baseCacheExecutor.getConnectResource();
                    if (!connectResource.isOpen()) {
                        System.out.println("关闭连接" + connectResource);
                        baseCacheExecutor.close();
                    }
                    baseCacheExecutor.returnConnectResource();
                }
            }
        }).start();
    }
    new Thread(() -> {
        BaseCacheExecutor baseCacheExecutor = null;
        baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
        baseCacheExecutor.subscribe((message) -> {
            System.out.println("Lettuce Local订阅消息:" + message);
        }, "test");
    }).start();
    new Thread(() -> {
        BaseCacheExecutor baseCacheExecutor = null;
        baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
        baseCacheExecutor.subscribe((message) -> {
            System.out.println("Lettuce Dev订阅消息:" + message);
        }, "test");
    }).start();
    while (true) {
    }
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) PipelineZremRangeByScore(com.lcache.extend.handle.pipeline.PipelineZremRangeByScore) Arrays(java.util.Arrays) PipelineGet(com.lcache.extend.handle.pipeline.PipelineGet) StringCodec(org.redisson.client.codec.StringCodec) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) CompletableFuture(java.util.concurrent.CompletableFuture) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) StatefulConnection(io.lettuce.core.api.StatefulConnection) ArrayList(java.util.ArrayList) RLock(org.redisson.api.RLock) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RedissonClient(org.redisson.api.RedissonClient) CacheClientFactory(com.lcache.client.CacheClientFactory) CacheConfigModel(com.lcache.core.model.CacheConfigModel) LcacheCaffeineLocalCache(com.lcache.core.cache.localcache.LcacheCaffeineLocalCache) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) Collectors(java.util.stream.Collectors) PipelineCmd(com.lcache.extend.handle.pipeline.PipelineCmd) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JSON(com.alibaba.fastjson.JSON) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) Assert.assertEquals(org.junit.Assert.assertEquals) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) StatefulConnection(io.lettuce.core.api.StatefulConnection) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with StatefulConnection

use of io.lettuce.core.api.StatefulConnection in project lcache by long172066912.

the class AbstractLettuceHandleExecutor method getConnectResource.

/**
 * 获取资源
 *
 * @return
 */
@Override
public StatefulConnection getConnectResource() {
    StatefulConnection statefulConnection = null;
    long stamp = connectResource.getStampedLock().tryOptimisticRead();
    statefulConnection = this.getStatefulConnection();
    // 判断是否需要加悲观读锁
    if (!connectResource.getStampedLock().validate(stamp)) {
        stamp = connectResource.getStampedLock().readLock();
        try {
            statefulConnection = this.getStatefulConnection();
        } finally {
            // 释放读锁
            connectResource.getStampedLock().unlockRead(stamp);
        }
    }
    return statefulConnection;
}
Also used : StatefulConnection(io.lettuce.core.api.StatefulConnection)

Example 5 with StatefulConnection

use of io.lettuce.core.api.StatefulConnection in project lcache by long172066912.

the class AbstractLettuceHandleExecutor method pSync.

/**
 * Lettuce管道需要使用新的连接
 *
 * @param commands
 * @return
 */
@Override
public List<Object> pSync(List<PipelineCmd> commands) {
    // 设置使用方式为管道
    CacheConfigModel pipelineCacheConfigModel = new CacheConfigModel(this.getCacheConfigModel(), UseTypeEnum.PIPELINE);
    // 获取管道专用链接
    StatefulConnection statefulConnection = this.getStatefulConnection(new ConnectResource().setConnectResource(this.getPipelineConnectResource(pipelineCacheConfigModel)));
    try {
        List<Object> res = new ArrayList<>();
        List<RedisFuture<?>> redisFutures = new LettucePipelineExecutor(this.async(statefulConnection)).pSync(commands);
        for (int i = 0; i < redisFutures.size(); i++) {
            try {
                res.add(redisFutures.get(i).get());
            } catch (Exception e) {
                CacheExceptionFactory.addErrorLog("AbstractLettuceHandleExecutor->pSync get error ! size:" + i, e);
            }
        }
        return res;
    } finally {
    // 关闭连接
    // connectionResource.close();
    }
}
Also used : RedisFuture(io.lettuce.core.RedisFuture) StatefulConnection(io.lettuce.core.api.StatefulConnection) ArrayList(java.util.ArrayList) LettuceConnectResource(com.lcache.extend.handle.redis.lettuce.connect.LettuceConnectResource) ConnectResource(com.lcache.connect.ConnectResource) LettucePipelineExecutor(com.lcache.extend.handle.redis.lettuce.pipeline.LettucePipelineExecutor) CacheConfigModel(com.lcache.core.model.CacheConfigModel)

Aggregations

StatefulConnection (io.lettuce.core.api.StatefulConnection)9 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 CacheConfigModel (com.lcache.core.model.CacheConfigModel)2 StringCodec (io.lettuce.core.codec.StringCodec)2 CommandLatencyCollector (io.lettuce.core.metrics.CommandLatencyCollector)2 ValueListOutput (io.lettuce.core.output.ValueListOutput)2 Command (io.lettuce.core.protocol.Command)2 CommandHandler (io.lettuce.core.protocol.CommandHandler)2 CommandType (io.lettuce.core.protocol.CommandType)2 Endpoint (io.lettuce.core.protocol.Endpoint)2 RedisCommand (io.lettuce.core.protocol.RedisCommand)2 ClientResources (io.lettuce.core.resource.ClientResources)2 Tracing (io.lettuce.core.tracing.Tracing)2 Unpooled (io.netty.buffer.Unpooled)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 LocalAddress (io.netty.channel.local.LocalAddress)2 ImmediateEventExecutor (io.netty.util.concurrent.ImmediateEventExecutor)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2