package com; import java.util.Arrays; import java.util.List; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Pipeline; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.ShardedJedisPool; import redis.clients.jedis.Transaction; /** * redis的几种调用方式 * @author lenovo * */ public class Demo1 { //-------------------------------------------------------单机实例---------------------------------------------------- /** * 普通同步写入操作,写入10万行字符串,大约需要40秒 */ @Test public void test1Normal() { Jedis jedis = new Jedis("192.168.3.134",6379); //需要在LINUX防火墙public里增加6379端口 // jedis.auth("admin"); //redis服务没有设置密码,如何加上这行会抛出异常 long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = jedis.set("n" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); //关闭连接 } /** * 普通批量事物提交,10万条字符串写入大约0.5秒。 * REDIS事物不支持回滚 */ @Test public void test2Trans() { Jedis jedis = new Jedis("192.168.3.134",6379); long start = System.currentTimeMillis(); Transaction tx = jedis.multi(); for (int i = 0; i < 100000; i++) { tx.set("t" + i, "t" + i); } List<Object> results = tx.exec(); long end = System.currentTimeMillis(); System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } /** * 普通异步管道提交。不必等待执行完成返回结果 */ @Test public void test3Pipelined() { Jedis jedis = new Jedis("192.168.3.134",6379); Pipeline pipeline = jedis.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("p" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } /** * 在管道中使用事物.但是经测试,发现其效率和单独使用事务差不多 */ @Test public void test4combPipelineTrans() { Jedis jedis = new Jedis("192.168.3.134",6379); long start = System.currentTimeMillis(); Pipeline pipeline = jedis.pipelined(); pipeline.multi(); for (int i = 0; i < 100000; i++) { pipeline.set("" + i, "" + i); } pipeline.exec(); List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } //----------------------------分布式---------分布式直连不支持事物,但可以使用管道----------------------------- /** * 分布式直连,普通同步操作 */ @Test public void test5shardNormal() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.3.134",6379), new JedisShardInfo("192.168.3.134",6379)); //这里在生产环境里换成不同服务器IP。测试发现其中一台机器宕机,将会抛出异常 ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = sharding.set("sn" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } /** * 分布式直连,管道.用junit运行异常,但没有异常信息。将代码放入main方法可正常运行。具体原因不明 */ public void test6shardpipelined() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.3.134",6379), new JedisShardInfo("192.168.3.134",6379)); //这里在生产环境里换成不同服务器IP。测试发现其中一台机器宕机,将会抛出异常 ShardedJedis sharding = new ShardedJedis(shards); ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sp" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } //-----------------------------------------分布式连接池-----适合多线程。直连方式不是线程安全的---------------------------------------------- /** * 连接池 同步调用 */ @Test public void test7shardSimplePool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.3.134",6379), new JedisShardInfo("192.168.3.134",6379)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); //需要加入commons-pool2-2.4.2包 ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = one.set("spn" + i, "n" + i); } long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } /** * 连接池 管道调用 */ @Test public void test8shardPipelinedPool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.3.134",6379), new JedisShardInfo("192.168.3.134",6379)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); ShardedJedisPipeline pipeline = one.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sppn" + i, "n" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("[email protected] SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } public static void main(String[] args) { Demo1 demo = new Demo1(); demo.test1Normal(); } @Test public void test() { System.out.println("test"); } }
时间: 2024-11-02 11:25:29