Redis Lettuce: Transaction with different codecs

72 Views Asked by At

I'm working with Lettuce (the Java Redis Client) and I need to update several keys within one transaction, some of which are Strings and some are ByteArrays.

Without transactions, I'm just using RedisClient.connect() for the String keys and RedisClient.connect(ByteArrayCodec.INSTANCE) for the ByteArray keys. However, when I want to use transactions I can only use one of them, so I can't create a transaction where I can update both key types.

MWE:

RedisClient redisClient = RedisClient.create("...");
RedisCommands<String, String> stringCommands = redisClient.connect().sync();
RedisCommands<byte[], byte[]> byteArrayCommands = redisClient.connect(ByteArrayCodec.INSTANCE).sync();

stringCommands.multi();
byteArrayCommands.multi();
stringCommands.set("Hello", "World");
byteArrayCommands.set("Hello2".getBytes(StandardCharsets.UTF_8), "World2".getBytes(StandardCharsets.UTF_8));
        
// I would need one exec here, otherwise there is some period of time where the string keys are updated,
// but the byte array keys are not (or the other way round)
stringCommands.exec();
byteArrayCommands.exec();

Is there a dedicated way to deal with this problem in Lettuce? I can obviously write my own codecs implementation which somehow combines String and ByteArray, but that seems somwhat cumbersome. I tried to write my own codes implementation to combine the two cases, but that does not seem to be trivial, since I don't know what kind of key I have in the decodeValue function.

Also, I have no influence on the Redis instance, so I can't change how keys are stored.

0

There are 0 best solutions below