Search in sources :

Example 1 with LettuceConnection

use of org.springframework.data.redis.connection.lettuce.LettuceConnection 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)

Aggregations

RedisFuture (io.lettuce.core.RedisFuture)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 CommandOutput (io.lettuce.core.output.CommandOutput)1 CommandArgs (io.lettuce.core.protocol.CommandArgs)1 ProtocolKeyword (io.lettuce.core.protocol.ProtocolKeyword)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 LettuceConnection (org.springframework.data.redis.connection.lettuce.LettuceConnection)1 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)1 RedisSerializer (org.springframework.data.redis.serializer.RedisSerializer)1 StringUtils (org.springframework.util.StringUtils)1