使用redis的java客户端-----jedis

时间:2020-12-04 20:36:01

单机版:

public void testJedisSingle()throws Exception {

           //创建一个Jedis对象

           Jedis jedis = new Jedis("127.0.0.1", 6379);

           jedis.set("test","hello jedis");

           String string = jedis.get("test");

           System.out.println(string);

           jedis.close();

      }

连接池版:

@Before
public void setUp(){
           JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
Jedis  jedis = jedisPool.getResource();
}

集群版:

       Set<HostAndPort> ports = new HashSet<>();
ports.add(new HostAndPort("127.0.0.1", 7001));
ports.add(new HostAndPort("127.0.0.1", 7002));
ports.add(new HostAndPort("127.0.0.1", 7003));
ports.add(new HostAndPort("127.0.0.1", 7004));
ports.add(new HostAndPort("127.0.0.1", 7005));
ports.add(new HostAndPort("127.0.0.1", 7006));
JedisCluster jedisCluster = new JedisCluster(ports);
jedisCluster.set("cluster", "clustervalue");
System.out.println(jedisCluster.get("cluster"));
jedisCluster.close();