Jedis操作redis

对于Redis的部署模式有两种,单机模式 和 集群模式。因此,本文的介绍也从这两个方面进行介绍。众所周知,Jedis是最著名的Redis java客户端操作类库,几乎支持所有的Redis操作。本文就是要介绍Jedis API如何操作两种模式下的Redis数据库,以及相关的操作技巧。

 

      本文介绍的全都是基于maven的管理方式建立的Java项目。首先,为了Java程序中使用Jedis API,在项目的pom文件中填加如下所示的maven依赖:

 

<!-- Jedis -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.9.0</version>

</dependency>

1、Jedis操作单机Redis

       对于单机版的Redis,Jedis的操作相当简单。

 

1.1 利用Jedis构造器

仅限用于测试,在实际项目中肯定是用JedisPool。

 

import redis.clients.jedis.Jedis;

 

import java.util.Map;

import java.util.Set;

 

/**

* Created by Administrator on 2017/6/5.

*/

public class JedisTest {

public static void main(String[] args) {

 

// 连接Redis

Jedis client = new Jedis("10.1.8.242", 6379);

 

// String 类型

client.set("key1", "value1");

System.out.println("-------------- String 类型 -------------");

System.out.println(client.get("key1"));

 

// Hash 类型

 

System.out.println("-------------- Hash 类型 -------------");

System.out.println(client.hget("key2", "field1"));

System.out.println(client.hget("key2", "field2"));

 

Map<String, String> map = client.hgetAll("key2");

System.out.println(map.get("field1"));

System.out.println(map.get("field2"));

 

// List 类型

client.lpush("key3", "value3_1", "value3_2");

System.out.println("-------------- List 类型 -------------");

System.out.println(client.lpop("key3"));

System.out.println(client.lpop("key3"));

 

// Set 类型

client.sadd("key4", "value4_1", "value4_2");

Set<String> set = client.smembers("key4");

System.out.println("-------------- Set 类型 -------------");

for (String val : set) {

System.out.println(val);

}

}

}

 

输出结果:

 

-------------- String 类型 -----------

value1

-------------- Hash 类型 -------------

value2_1

value2_2

value2_1

value2_2

-------------- List 类型 -------------

value3_2

value3_1

-------------- Set 类型 -------------

value4_2

value4_1

 

1.2 利用JedisPool

     JedisPool有N多个构造器,创建GenericObjectPoolConfig对象时我们一般用其子类JedisPoolConfig (redis.clients.jedis.JedisPoolConfig),其中的参数 timeout 是连接redis服务器的超时时间,以毫秒为单位,一般设置为0,如果不设为0,则不可设置太小,如果设成1、2,那么可能因为网络原因在1毫秒、2毫秒之内没有连上服务器而报错。见下例:

 

public static void main(String[] args) {

JedisPoolConfig poolConfig = new JedisPoolConfig();

// 最大连接数

poolConfig.setMaxTotal(2);

// 最大空闲数

poolConfig.setMaxIdle(2);

// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:

// Could not get a resource from the pool

poolConfig.setMaxWaitMillis(1000);

JedisPool pool = new JedisPool(poolConfig, "192.168.83.128", 6379, 0, "123");

Jedis jedis = null;

try {

for (int i = 0; i < 5; i++) {

jedis = pool.getResource();

jedis.set("foo" + i, "bar" + i);

System.out.println("第" + (i + 1) + "个连接, 得到的值为" + jedis.get("foo" + i));

// 用完一定要释放连接

jedis.close();

}

} finally {

pool.close();

}

}

 

12

如上,创建出一个JedisPool对象,然后调用其getResource()方法获取redis连接即可,之后就可以调用Jedis API操作redis了。jedis连接用完要释放即close,如果不close,则产生的连接会越来越多,当达到了最大连接数,再想获得连接,就会等待,当超过了最大等待时间后就会报异常。

 

2、Jedis操作Redis集群

集群状态下用Jedis获取redis连接,是得到JedisCluster对象,之后对redis进行操作都是用此对象的方法进行的:

 

public static void main(String[] args) {

JedisPoolConfig poolConfig = new JedisPoolConfig();

// 最大连接数

poolConfig.setMaxTotal(1);

// 最大空闲数

poolConfig.setMaxIdle(1);

// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:

// Could not get a resource from the pool

poolConfig.setMaxWaitMillis(1000);

Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();

nodes.add(new HostAndPort("192.168.83.128", 6379));

nodes.add(new HostAndPort("192.168.83.128", 6380));

nodes.add(new HostAndPort("192.168.83.128", 6381));

nodes.add(new HostAndPort("192.168.83.128", 6382));

nodes.add(new HostAndPort("192.168.83.128", 6383));

nodes.add(new HostAndPort("192.168.83.128", 6384));

JedisCluster cluster = new JedisCluster(nodes, poolConfig);

String name = cluster.get("name");

System.out.println(name);

cluster.set("age", "18");

System.out.println(cluster.get("age"));

try {

cluster.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

3、使用pipeline批量操作

       一般情况下,大家使用Redis去put/get都是先拿到一个jedis实例,然后操作,然后释放连接;这种模式是:请求-响应,请求-响应……。 这种模式,下一次请求必须得等第一次请求响应回来之后才可以,因为redis是单线程的,按部就班,一步一步来。而pipeline管道改变了这种请求模式,客户端可以一次发送多个命令,无须等待服务器的返回,请求,请求,请求,响应,响应,响应 这种模式。这就大大减少了影响性能的关键因素-网络往返时间。

 

public class RedisDemo {

 

public static void main(String[] args) {

String redisIP = "10.117.146.16";

int redisPort = 6379;

Jedis jedis;

try {

jedis = new Jedis(redisIP, redisPort);

jedis.select(8);

}

catch (Exception e) {

e.printStackTrace();

System.out.printf("初始化Redis连接错误:%s, %d", redisIP, redisPort);

return;

}

 

jedis.flushDB();

long start = System.currentTimeMillis();

notusePipeline(jedis);

long end = System.currentTimeMillis();

System.out.printf("不使用Pipeline的方式用时:%d毫秒", end-start);

 

jedis.flushDB();

start = System.currentTimeMillis();

usePipeline(jedis);

end = System.currentTimeMillis();

System.out.printf("使用Pipeline的方式用时:%d毫秒", end-start);

 

}

 

private static void notusePipeline(Jedis jedis) {

Map<String, String> mp = new HashMap<String, String>();

try {

for (int i=0; i<10000; i++) {

mp.clear();

mp.put("k"+i, "v"+i);

jedis.hmset("keys"+i, mp);

}

}

catch (Exception e) {

e.printStackTrace();

}

}

 

private static void usePipeline(Jedis jedis) {

Map<String, String> mp = new HashMap<String, String>();

try {

Pipeline pl = jedis.pipelined();

for (int i=0; i<10000; i++) {

mp.clear();

mp.put("k"+i, "v"+i);

pl.hmset("keys"+i, mp);

}

pl.sync();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

 

17

49

不用Pipeline的方式,跑了72秒多,平均每秒操作十多条数据。

 

而采用Pipeline的方式,只运行了173毫秒,吞吐量提高非常多。

 

这也说明了:大量的时间是在网络交互上,Redis本身处理能力是很强的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值