Redis给Java语言提供了客户端API,称之为Jedis。Jedis API和Redis命令几乎是一样的。 Jedis API特别简单,基本上都是创建对象调用方法即可。
(1)单机版
public void testStandalone(){ Jedis jedis = new Jedis("192.168.1.129",6379); jedis.set("name" ,"standalone"); String value = jedis.get("name"); System.out.println(value);}
(2)带有连接池
public void testJedisPool(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(20); jedisPoolConfig.setMaxIdle(5); jedisPoolConfig.setMinIdle(3); JedisPool jedisPool = new JedisPool(jedisPoolConfig , "192.168.1.129",6379 ); Jedis jedis = jedisPool.getResource(); jedis.set("name" , "pool"); String poolvalue = jedis.get("name"); System.out.println(poolvalue);}
(3)集群
public void testCluster(){ Set
set = new HashSet (); set.add(new HostAndPort("192.168.1.129",7001)); set.add(new HostAndPort("192.168.1.129",7002)); set.add(new HostAndPort("192.168.1.129",7003)); set.add(new HostAndPort("192.168.1.129",7004)); set.add(new HostAndPort("192.168.1.129",7005)); set.add(new HostAndPort("192.168.1.129",7006)); JedisCluster jedisCluster = new JedisCluster(set); jedisCluster.set("name" , "colony"); String value = jedisCluster.get("name"); System.out.println(value);}