gpt4 book ai didi

spring-boot - Spring数据redis对模块的支持?

转载 作者:行者123 更新时间:2023-12-03 06:41:16 24 4
gpt4 key购买 nike

我正在使用带有生菜驱动程序的 Spring data Redis(版本 2.1.1RELEASE)(版本 5.1.3RELEASE)
我想使用这个模块:https://oss.redislabs.com/redisjson/但司机似乎不支持它。

我尝试使用 execute方法:

Object response = redisTemplate.execute((RedisCallback<String>) connection -> {
return (String) connection.execute("JSON.GET foo");
});

并得到一个错误:

java.lang.IllegalArgumentException: No enum constant io.lettuce.core.protocol.CommandType.JSON.GET FOO



有没有办法做到这一点?如何使用 Redis 模块?

最佳答案

好的,所以我设法做到了:
先扩展ProtocolKeyword并用“。”替换所有“_”字符:

public interface ModuleCommand extends ProtocolKeyword {

@Override
default byte[] getBytes() {
return name().replaceAll("_", ".").getBytes(LettuceCharsets.ASCII);
}
}
其次,使用您希望实现的命令类型创建一个枚举:
public enum JsonCommand implements ModuleCommand {

JSON_GET,
JSON_MGET,
JSON_SET,
JSON_ARRAPPEND,
JSON_DEL,
// other commands you wish to implement
;
}
现在这是有趣的部分,我已经集成了更多的模块,所以我必须概括执行部分:
public abstract class ModuleAbstractManager {


protected ByteArrayCodec codec = ByteArrayCodec.INSTANCE;

@Autowired
private LettuceConnectionFactory connectionFactory;

protected <T> Optional<T> execute(String key, ModuleCommand jsonCommand, CommandOutput<byte[], byte[], T> output, String... args) {

List<byte[]> extraArgs = Arrays.stream(args)
.filter(arg -> !StringUtils.isEmpty(arg))
.map(String::getBytes)
.collect(Collectors.toList());

CommandArgs<byte[], byte[]> commandArgs = new CommandArgs<>(codec)
.addKey(key.getBytes())
.addValues(extraArgs);

LettuceConnection connection = (LettuceConnection) connectionFactory.getConnection();

try {
RedisFuture<T> future = connection.getNativeConnection().dispatch(jsonCommand, output, commandArgs);
return Optional.ofNullable(future.get());
}
catch (InterruptedException | ExecutionException e) {
return Optional.empty();
}
finally {
connection.close();
}
}
}
最后是执行本身:
@Service
public class RedisJsonManager extends ModuleAbstractManager {

public static final String ROOT_PATH = ".";
public static final String OK_RESPONSE = "OK";
public static final String SET_IF_NOT_EXIST_FLAG = "NX";


public Optional<String> getValue(String key) {
return getValue(key, ROOT_PATH);
}

public Optional<String> getValue(String key, String path) {

if (StringUtils.isEmpty(path)) {
return Optional.empty();
}

return execute(key, JsonCommand.JSON_GET, new ValueOutput<>(codec), path)
.map(String::new);
}

public Optional<String> getValue(String key, List<String> multiPath) {

if (CollectionUtils.isEmpty(multiPath)) {
return Optional.empty();
}

String[] args = multiPath.toArray(new String[0]);
return execute(key, JsonCommand.JSON_GET, new ValueOutput<>(codec), args)
.map(String::new);
}

public boolean setValueIfNotExist(String key, String json) {
return setValue(key, json, ROOT_PATH, true);
}

public boolean setValueIfNotExist(String key, String json, String path) {
return setValue(key, json, path, true);
}

public boolean setValue(String key, String json) {
return setValue(key, json, ROOT_PATH, false);
}

public boolean setValue(String key, String json, String path) {
return setValue(key, json, path, false);
}

private boolean setValue(String key, String json, String path, boolean setIfNotExist) {
return execute(key, JsonCommand.JSON_SET, new StatusOutput<>(codec), path, json, setIfNotExist ? SET_IF_NOT_EXIST_FLAG : "")
.map(OK_RESPONSE::equals)
.orElse(false);
}

public Long addToArray(String key, String json) {
return addToArray(key, json, ROOT_PATH);
}

public Long addToArray(String key, String json, String path) {
return execute(key, JsonCommand.JSON_ARRAPPEND, new IntegerOutput<>(codec), path, json).orElse(0L);
}

public Long delete(String key) {
return delete(key, ROOT_PATH);
}

public Long delete(String key, String path) {
return execute(key, JsonCommand.JSON_DEL, new IntegerOutput<>(codec), path).orElse(0L);
}
}

关于spring-boot - Spring数据redis对模块的支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61062171/

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