Search in sources :

Example 6 with RedisFuture

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

Example 7 with RedisFuture

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();
}
Also used : ByteArrayCodec(io.lettuce.core.codec.ByteArrayCodec) RedisFuture(io.lettuce.core.RedisFuture)

Example 8 with RedisFuture

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();
}
Also used : Setter(lombok.Setter) Arrays(java.util.Arrays) ByteArrayCodec(io.lettuce.core.codec.ByteArrayCodec) LettuceConnection(org.springframework.data.redis.connection.lettuce.LettuceConnection) Getter(lombok.Getter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) ProtocolKeyword(io.lettuce.core.protocol.ProtocolKeyword) Collectors(java.util.stream.Collectors) CommandOutput(io.lettuce.core.output.CommandOutput) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CommandArgs(io.lettuce.core.protocol.CommandArgs) RedisFuture(io.lettuce.core.RedisFuture) Optional(java.util.Optional) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) StringUtils(org.springframework.util.StringUtils) CommandArgs(io.lettuce.core.protocol.CommandArgs) StringUtils(org.springframework.util.StringUtils) LettuceConnection(org.springframework.data.redis.connection.lettuce.LettuceConnection)

Example 9 with RedisFuture

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();
    }
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisFuture(io.lettuce.core.RedisFuture) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection)

Aggregations

RedisFuture (io.lettuce.core.RedisFuture)9 StatefulConnection (io.lettuce.core.api.StatefulConnection)2 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)2 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)2 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)2 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2 SimpleAnchorInfo (com.java3y.austin.common.domain.SimpleAnchorInfo)1 ConnectResource (com.lcache.connect.ConnectResource)1 CacheConfigModel (com.lcache.core.model.CacheConfigModel)1 LettuceConnectResource (com.lcache.extend.handle.redis.lettuce.connect.LettuceConnectResource)1 LettucePipelineExecutor (com.lcache.extend.handle.redis.lettuce.pipeline.LettucePipelineExecutor)1 Range (io.lettuce.core.Range)1 RedisCommandExecutionException (io.lettuce.core.RedisCommandExecutionException)1 io.lettuce.core.api.async (io.lettuce.core.api.async)1 io.lettuce.core.api.sync (io.lettuce.core.api.sync)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 RedisAdvancedClusterAsyncCommands (io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands)1 RedisAdvancedClusterCommands (io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands)1