Java使用Redis--jedis

参考:菜鸟教程 http://www.runoob.com/redis/redis-java.html

1、Java 使用 Redis

开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动(jedis-2.9.0.jar),且你的机器上能正常使用 Java。

1.1、Redis Java String(字符串) 实例

public class RedisStringJava {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        System.out.println("连接成功");
        //设置 redis 字符串数据
        jedis.set("runoobkey", "www.runoob.com");
        // 获取存储的数据并输出
        System.out.println("redis 存储的字符串为:"+ jedis.get("runoobkey"));}
}

  

1.2、Redis Java List(列表) 实例

public class RedisListJava {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        System.out.println("连接成功");
        //存储数据到列表中
        jedis.lpush("site-list", "Runoob");
        jedis.lpush("site-list", "Google");
        jedis.lpush("site-list", "Taobao");
        // 获取存储的数据并输出
        List<String> list = jedis.lrange("site-list", 0 ,2);
        for(int i=0; i<list.size(); i++) {
            System.out.println("列表项为:"+list.get(i));
        }
    }
}

  

1.3、Redis Java Keys 实例

public class RedisKeyJava {
    public static void main(String[] args) {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        System.out.println("连接成功");
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取数据并输出</span>
    Set&lt;String&gt; keys = jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">); 
    Iterator</span>&lt;String&gt; it=<span style="color: rgba(0, 0, 0, 1)">keys.iterator() ;   
    </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(it.hasNext()){   
        String key </span>=<span style="color: rgba(0, 0, 0, 1)"> it.next();   
        System.out.println(key);   
    }
}

}

 

2、Java 使用 Redis

  jedis-2.7.2.jar  和  commons-pool2-2.3.jar

package com.oy.test;

import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

public class Demo1 {
// 单机版测试
@Test
public void fun1() throws Exception {
// 创建一个 Jedis 对象
Jedis jedis = new Jedis("192.168.25.128", 6379);
jedis.set(
"test", "hello jedis");
String string
= jedis.get("test");
System.out.println(string);
jedis.close();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用连接池</span>

@Test
public void testJedisPool() throws Exception {
// 创建一个连接池对象
// 系统中应该是单例的。
JedisPool jedisPool = new JedisPool("192.168.25.128", 6379);
// 从连接池中获得一个连接
Jedis jedis = jedisPool.getResource();
String result
= jedis.get("test");
System.out.println(result);
// jedis 必须关闭
jedis.close();

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 系统关闭时关闭连接池</span>

jedisPool.close();
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接redis集群</span>

@Test
public void testJedisCluster() throws Exception {
// 创建一个 JedisCluster 对象
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(
new HostAndPort("192.168.25.128", 7001));
nodes.add(
new HostAndPort("192.168.25.128", 7002));
nodes.add(
new HostAndPort("192.168.25.128", 7003));
nodes.add(
new HostAndPort("192.168.25.128", 7004));
nodes.add(
new HostAndPort("192.168.25.128", 7005));
nodes.add(
new HostAndPort("192.168.25.128", 7006));
// 在 nodes 中指定每个节点的地址
// jedisCluster 在系统中是单例的。
JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster.set(
"name", "zhangsan");
jedisCluster.set(
"value", "100");
String name
= jedisCluster.get("name");
String value
= jedisCluster.get("value");
System.out.println(name);
System.out.println(value);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 系统关闭时关闭jedisCluster</span>

jedisCluster.close();
}
}