gpt4 book ai didi

java - 如何使用 Micronaut 框架将 POJO 存储在 Redis 中?

转载 作者:可可西里 更新时间:2023-11-01 11:23:32 26 4
gpt4 key购买 nike

我是 Micronaut 的新手。我正在尝试将项目移植到 Micronaut (v1.1.1),但我发现 Redis 存在问题。

我只是想在 Redis 中保存一个简单的 POJO,但是当我尝试“保存”它时出现以下错误:

io.lettuce.core.RedisException: io.netty.handler.codec.EncoderException: Cannot encode command. Please close the connection as the connection state may be out of sync.

代码很简单(HERE可以找到完整的测试。):

class DummyTest {
@Test
public void testIssue() throws Exception {

final Date now = Date.from(Instant.now());
CatalogContent expectedContentOne = CatalogContent.builder()
.contentId(1)
.status(ContentStatus.AVAILABLE)
.title("uno")
.streamId(1)
.available(now)
.tags(Set.of("tag1", "tag2"))
.build();
repository.save(expectedContentOne);
}
}
/.../

class CatalogContentRepository {
private StatefulRedisConnection<String, CatalogContent> connection;

public CatalogContentRepository(StatefulRedisConnection<String, CatalogContent> connection) {
this.connection = connection;
}
public void save(CatalogContent content) {
RedisCommands<String, CatalogContent> redisApi = connection.sync();
redisApi.set(String.valueOf(content.getContentId()),content); //Error here!
}
}

欢迎任何想法。

提前致谢。

最佳答案

作为记录,我将回答我自己的问题:

现在 (20190514) Micronaut 只生成 StatefulRedisConnection<String,String>使用硬编码的 UTF8 字符串编解码器。要更改它,您必须替换 DefaultRedisClientFactory并定义一个返回 StatefulRedisConnection 的方法你需要,使用您首选的编解码器。

以我为例:

@Requires(beans = DefaultRedisConfiguration.class)
@Singleton
@Factory
@Replaces(factory = DefaultRedisClientFactory.class)
public class RedisClientFactory extends AbstractRedisClientFactory {

@Bean(preDestroy = "shutdown")
@Singleton
@Primary
@Override
public RedisClient redisClient(@Primary AbstractRedisConfiguration config) {
return super.redisClient(config);
}


@Bean(preDestroy = "close")
@Singleton
@Primary
public StatefulRedisConnection<String, Object> myRedisConnection(@Primary RedisClient redisClient) {
return redisClient.connect(new SerializedObjectCodec());
}

@Bean(preDestroy = "close")
@Singleton
@Primary
@Override
public StatefulRedisConnection<String, String> redisConnection(@Primary RedisClient redisClient) {
throw new RuntimeException("puta mierda");
}


@Override
@Bean(preDestroy = "close")
@Singleton
public StatefulRedisPubSubConnection<String, String> redisPubSubConnection(@Primary RedisClient redisClient) {
return super.redisPubSubConnection(redisClient);
}
}

编解码器取自Redis Lettuce wiki

public class SerializedObjectCodec implements RedisCodec<String, Object> {
private Charset charset = Charset.forName("UTF-8");

@Override
public String decodeKey(ByteBuffer bytes) {
return charset.decode(bytes).toString();
}

@Override
public Object decodeValue(ByteBuffer bytes) {
try {
byte[] array = new byte[bytes.remaining()];
bytes.get(array);
ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(array));
return is.readObject();
} catch (Exception e) {
return null;
}
}

@Override
public ByteBuffer encodeKey(String key) {
return charset.encode(key);
}

@Override
public ByteBuffer encodeValue(Object value) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bytes);
os.writeObject(value);
return ByteBuffer.wrap(bytes.toByteArray());
} catch (IOException e) {
return null;
}
}
}

关于java - 如何使用 Micronaut 框架将 POJO 存储在 Redis 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56125476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com