1. 2.1.1 Jedis基本应用

1. 创建maven项目, 具体操作步骤省略
2. 添加jedis maven依赖
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.2</version>
</dependency>
3. 创建Jedis对象
// 1. 使用host和port参数创建Jedis对象
Jedis jedis = new Jedis("localhost", 6379);
4. 使用Redis命令对应的方法操作Redis, 如对于Redis Connection组命令:
// 2. Redis Connection组命令测试
// jedis.auth("password"); //如果服务器需要密码, 验证密码
String ping = jedis.ping();
String echo = jedis.echo("hello world.");
String select = jedis.select(0);
LOGGER.info("ping:{} echo:{} select:{}", ping, echo, select);
5. 关闭连接
// 4. 关闭连接
jedis.close();
6. 全部代码与输出结果

main方法:

public class JedisStartApp {

    public static final Logger LOGGER = LoggerFactory.getLogger(JedisStartApp.class);

    public static void main(String[] args){
        // 1. 使用host和port参数创建Jedis对象
        Jedis jedis = new Jedis("localhost", 6379);
        // 2. Redis Connection组命令测试
//        jedis.auth("password"); //如果服务器需要密码, 验证密码
        String ping = jedis.ping();
        String echo = jedis.echo("hello world.");
        String select = jedis.select(0);
        LOGGER.info("ping:{} echo:{} select:{}", ping, echo, select);

        // 3. Redis Keys命令测试
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            String value = jedis.get(key);
            LOGGER.info("redis key:{} value:{}", key, value);
        }

        // 4. 关闭连接
        jedis.close();
    }
}

输出结果:

Connected to the target VM, address: '127.0.0.1:53251', transport: 'socket'
19:37:18.185 [main] INFO samples.cache.jedis.JedisStartApp - ping:PONG echo:hello world. select:OK
Disconnected from the target VM, address: '127.0.0.1:53251', transport: 'socket'
19:37:18.204 [main] INFO samples.cache.jedis.JedisStartApp - redis key:db_number value:0
Copyright © wychuan.com 2017 all right reserved,powered by Gitbook该文件修订时间: 2017-10-17 02:48:52

results matching ""

    No results matching ""