首页 > 其他 > 详细

Test redis

时间:2018-03-14 21:56:59      阅读:226      评论:0      收藏:0      [点我收藏+]

单机测试:

 1 public class RedisClient{
 2     private Jedis jedis;
 3     private JedisPool jedisPool;
 4     private ShardedJedis shardedJedis;
 5     private ShardedJedisPool shardedJedisPool;
 6     public RedisClient() {
 7         
 8     }
 9     /**
10      * 单例链接redis
11      */
12     @Test
13     public void testJedisSingle() {
14         Jedis jedis = new Jedis("localhost",6379);
15         jedis.set("name", "bar");
16         String name= jedis.get("name");
17         System.out.println(name);
18         jedis.close();
19     }
20     /**
21      * 连接池链接redis
22      */
23     @Test
24     public void testPool() {
25     JedisPoolConfig config = new JedisPoolConfig();
26     config.setMaxTotal(30);//最大连接数
27     config.setMaxIdle(15);//最大空闲连接数
28     JedisPool pool = new JedisPool(config,"localhost",6379);
29     Jedis jedis = null;
30     
31     try {
32         jedis = pool.getResource();
33         jedis.set("name1", "wangwu");
34         String name1 = jedis.get("name1");
35         System.out.println(name1);
36     } catch (Exception e) {
37         e.printStackTrace();
38     }finally {
39         jedis.close();
40     }
41     }
42     
43 
44 }
bar
wangwu

 

集群测试:

 1 public class RedisCluster {
 2 @Test
 3 public void testCluster() {
 4     
 5     JedisPoolConfig config = new JedisPoolConfig();
 6     config.setMaxTotal(30);
 7     config.setMaxIdle(15);
 8     
 9     Set<HostAndPort> jedisClusterNode = new HashSet();
10     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7000));
11     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7001));
12     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7002));
13     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7003));
14     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7004));
15     jedisClusterNode.add(new HostAndPort("127.0.0.1", 7005));
16     
17     JedisCluster jc_in = new JedisCluster(jedisClusterNode,config);
18     JedisCluster jc_out = new JedisCluster(jedisClusterNode);
19     
20     jc_in.set("123", "123");
21     String vlaue = jc_out.get("123");
22     System.out.println("value:"+vlaue);
23 }
24 
25 }
value:123

 

Test redis

原文:https://www.cnblogs.com/the-wang/p/8570115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!