use of io.lettuce.core.RedisFuture 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();
}
}
use of io.lettuce.core.RedisFuture in project austin by ZhongFuCheng3y.
the class LettuceRedisUtils method pipeline.
/**
* 封装pipeline操作
*/
public static void pipeline(RedisPipelineCallBack pipelineCallBack) {
StatefulRedisConnection<byte[], byte[]> connect = redisClient.connect(new ByteArrayCodec());
RedisAsyncCommands<byte[], byte[]> commands = connect.async();
List<RedisFuture<?>> futures = pipelineCallBack.invoke(commands);
commands.flushCommands();
LettuceFutures.awaitAll(10, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()]));
connect.close();
}
use of io.lettuce.core.RedisFuture in project ballcat by ballcat-projects.
the class AbstractRedisModuleHelper method execute.
@SuppressWarnings("unchecked")
protected <T> Optional<T> execute(String key, ProtocolKeyword type, CommandOutput<byte[], byte[], T> output, String... args) {
List<byte[]> extraArgs = Arrays.stream(args).filter(StringUtils::hasLength).map(arg -> valueSerializer.serialize(arg)).collect(Collectors.toList());
CommandArgs<byte[], byte[]> commandArgs = new CommandArgs<>(codec).addKey(keySerializer.serialize(key)).addValues(extraArgs);
try (LettuceConnection connection = (LettuceConnection) connectionFactory.getConnection()) {
RedisFuture<T> future = connection.getNativeConnection().dispatch(type, output, commandArgs);
return Optional.ofNullable(future.get());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("[execute] 执行异常, KEY: [{}], type: [{}], args: [{}]", key, type.name(), args, e);
}
return Optional.empty();
}
use of io.lettuce.core.RedisFuture in project lettuce-core by lettuce-io.
the class ClusterFutureSyncInvocationHandler method handleInvocation.
/**
* @see AbstractInvocationHandler#handleInvocation(Object, Method, Object[])
*/
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (method.isDefault()) {
return methodHandleCache.computeIfAbsent(method, ClusterFutureSyncInvocationHandler::lookupDefaultMethod).bindTo(proxy).invokeWithArguments(args);
}
if (method.getName().equals("getConnection") && args.length > 0) {
return getConnection(method, args);
}
if (method.getName().equals("readonly") && args.length == 1) {
return nodes((Predicate<RedisClusterNode>) args[0], ClusterConnectionProvider.Intent.READ, false);
}
if (method.getName().equals("nodes") && args.length == 1) {
return nodes((Predicate<RedisClusterNode>) args[0], ClusterConnectionProvider.Intent.WRITE, false);
}
if (method.getName().equals("nodes") && args.length == 2) {
return nodes((Predicate<RedisClusterNode>) args[0], ClusterConnectionProvider.Intent.WRITE, (Boolean) args[1]);
}
Method targetMethod = apiMethodCache.computeIfAbsent(method, key -> {
try {
return asyncApi.getClass().getMethod(key.getName(), key.getParameterTypes());
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}
});
Object result = targetMethod.invoke(asyncApi, args);
if (result instanceof RedisFuture) {
RedisFuture<?> command = (RedisFuture<?>) result;
if (!method.getName().equals("exec") && !method.getName().equals("multi")) {
if (connection instanceof StatefulRedisConnection && ((StatefulRedisConnection) connection).isMulti()) {
return null;
}
}
return Futures.awaitOrCancel(command, getTimeoutNs(command), TimeUnit.NANOSECONDS);
}
return result;
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
Aggregations