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;
}
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));
}
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) {
}
}
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;
}
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();
}
}
Aggregations