gpt4 book ai didi

redis - 使用 JRedis 客户端的 Hello World 示例

转载 作者:可可西里 更新时间:2023-11-01 11:14:05 25 4
gpt4 key购买 nike

我想在 Java 中使用 Redis,所以我从 this 开始简单的例子(见下面的代码)。

第一次运行将键值对插入数据库,第二次运行应该获取该值并将其打印到屏幕上。有两个问题我不明白:

  1. 当我插入 key 时,它会打印出一个错误,指出该值不是命令:“PING 的错误响应 => ERR 未知命令“再次问候!”。这对我来说没有意义 - 为什么它认为字符串值是命令吗?

  2. 当我第二次运行它来打印键的值时,它只打印值的长度,但我希望看到“Hello Again!”打印出来。

感谢您的帮助!

代码如下:

import org.jredis.ClientRuntimeException;
import org.jredis.JRedis;
import org.jredis.ProviderException;
import org.jredis.RedisException;
import org.jredis.protocol.Command;
import org.jredis.ri.alphazero.JRedisClient;
import static org.jredis.ri.alphazero.support.DefaultCodec.*;

/**
* [TODO: document me!]
*
* @author Joubin Houshyar (alphazero@sensesay.net)
* @version alpha.0, Apr 15, 2009
* @since alpha.0
*
*/

public class HelloAgain {
public static final String key = "jredis::examples::HelloAgain::message";
public static void main(String[] args) {
String password = "";
if(args.length > 0) password = args[0];
new HelloAgain().run(password);
}

private void run(String password) {
try {
JRedis jredis = new JRedisClient("localhost", 6379, "jredis", 0);
jredis.ping();

if(!jredis.exists(key)) {
jredis.set(key, "Hello Again!");
System.out.format("Hello! You should run me again!\n");
}
else {
String msg = toStr ( jredis.get(key) );
System.out.format("%s\n", msg);
}
jredis.quit();
}
catch (RedisException e){
if (e.getCommand()==Command.PING){
System.out.format("I'll need that password! Try again with password as command line arg for this program.\n");
}
}
catch (ProviderException e){
System.out.format("Oh no, an 'un-documented feature': %s\nKindly report it.", e.getMessage());
}
catch (ClientRuntimeException e){
System.out.format("%s\n", e.getMessage());
}
}
}

最佳答案

好吧,我没有得到任何答案,也尝试了这个例子的作者,但没有运气。我做了一些阅读,发现 jedis是一个更好的 Redis Java 客户端。我试过了,效果很好;使用起来非常简单。

关于redis - 使用 JRedis 客户端的 Hello World 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5269700/

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