Redis实践(三)基于jedis开发java应用:redis的8种方式设置key,value

前面已经进行了Redis的部署实践,下面要进行redisJava开发实践

一、目标

  搭建java开发环境,采用eclipse开发工具

 验证redis的写数据的8种方式的效率

二、环境准备

开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java。 Java的安装配置可以参考我们的 Java开发环境配置 接下来让我们安装 java redis 驱动:
        首先你需要下载驱动包,下载 jedis.jar,我在本次实践中用的jedis的jar包是2.8.0 版本的。同时,要保证java工程可以正常运行,还需要下载commons-pool2-2.3.jar以及 
hamcrest-core-1.3.jar。

        然后,在redis服务上,配置好6379,6380 两个端口的配置文件,启动redis(因为后面需要用分布式的调用,所以需要启动两个redis实例)

三、java工程

在elipse中创建工程,并且将上面三个jar包放到lib目录中,并且添加到buildpath

 

    下面,对jedis关于事务、管道和分布式的调用方式做一个简单的介绍和对比,代码如下:(主要从网上获取,少量改动)

[java]  view plain  copy
  1. <span style="font-family:Microsoft YaHei;font-size:14px;">package com.cwqsolo.redis.test;  
  2.   
  3. import java.util.Arrays;   
  4. import java.util.List;   
  5.   
  6. import redis.clients.jedis.Jedis;   
  7. import redis.clients.jedis.JedisPoolConfig;   
  8. import redis.clients.jedis.JedisShardInfo;   
  9. import redis.clients.jedis.Pipeline;   
  10. import redis.clients.jedis.ShardedJedis;   
  11. import redis.clients.jedis.ShardedJedisPipeline;   
  12. import redis.clients.jedis.ShardedJedisPool;   
  13. import redis.clients.jedis.Transaction;   
  14.   
  15.   
  16. //RedisDemo  用来测试redis的使用接口  
  17. public class RedisDemo {   
  18.   
  19.     private static Jedis jedis;   
  20.     private static ShardedJedis sharding;   
  21.     static ShardedJedisPool pool;  
  22.   
  23.     public static void init() throws Exception {   
  24.         List<JedisShardInfo> shards = Arrays.asList(   
  25.                 new JedisShardInfo("192.168.136.144",6379),  
  26.                 new JedisShardInfo("192.168.136.144",6380)  
  27.                 ); //使用相同的ip:port,仅作测试   
  28.   
  29.   
  30.         jedis = new Jedis("192.168.136.144");   
  31.         sharding = new ShardedJedis(shards);   
  32.         JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置  
  33.         pool = new ShardedJedisPool(config, shards);   
  34.     }   
  35.   
  36.   
  37.     public static void CleanUp() throws Exception {   
  38.         jedis.disconnect();   
  39.         sharding.disconnect();   
  40.         pool.destroy();   
  41.     }   
  42.   
  43.   
  44.     //测试普通同步方式, 设置10w个key,value,看用时多少。  
  45.     public void test1Normal() {   
  46.         long start = System.currentTimeMillis();   
  47.         for (int i = 0; i < 100000; i++) {   
  48.             String result = jedis.set("1n" + i, "1n" + i);   
  49.         }   
  50.         long end = System.currentTimeMillis();   
  51.         System.out.println("普通同步方式::Simple SET: " + ((end - start)/1000.0) + " seconds");   
  52.     }   
  53.   
  54.   //测试事务方式Transactions, 设置10w个key,value,看用时多少。  
  55.     public void test2Trans() {   
  56.         long start = System.currentTimeMillis();   
  57.         Transaction tx = jedis.multi();   
  58.         for (int i = 0; i < 100000; i++) {   
  59.             tx.set("1t" + i, "1t" + i);   
  60.         }   
  61.         //System.out.println(tx.get("t1000").get());   
  62.   
  63.         List<Object> results = tx.exec();   
  64.         long end = System.currentTimeMillis();   
  65.         System.out.println("事务方式::Transaction SET: " + ((end - start)/1000.0) + " seconds");   
  66.     }   
  67.   
  68.   
  69.     //采用管道方式,异步方式,一次发送多个指令,  
  70.     public void test3Pipelined() {   
  71.         Pipeline pipeline = jedis.pipelined();   
  72.         long start = System.currentTimeMillis();   
  73.         for (int i = 0; i < 100000; i++) {   
  74.             pipeline.set("p" + i, "p" + i);   
  75.         }   
  76.         //System.out.println(pipeline.get("p1000").get());   
  77.         List<Object> results = pipeline.syncAndReturnAll();   
  78.         long end = System.currentTimeMillis();   
  79.         System.out.println("管道方式异步::Pipelined SET: " + ((end - start)/1000.0) + " seconds");   
  80.     }   
  81.   
  82.     //管道中调用事务,   
  83.     public void test4combPipelineTrans() {   
  84.         long start = System.currentTimeMillis();   
  85.         Pipeline pipeline = jedis.pipelined();   
  86.         pipeline.multi();   
  87.         for (int i = 0; i < 100000; i++) {   
  88.             pipeline.set("" + i, "" + i);   
  89.         }   
  90.         //与管道的区别在这里  
  91.         pipeline.exec();   
  92.         List<Object> results = pipeline.syncAndReturnAll();   
  93.         long end = System.currentTimeMillis();   
  94.         System.out.println("管道中调用事务::Pipelined transaction: " + ((end - start)/1000.0) + " seconds");   
  95.     }   
  96.   
  97.     //分布式直连同步调用,用到了分片  
  98.     public void test5shardNormal() {   
  99.         long start = System.currentTimeMillis();   
  100.         for (int i = 0; i < 100000; i++) {   
  101.             String result = sharding.set("sn" + i, "n" + i);   
  102.         }   
  103.         long end = System.currentTimeMillis();   
  104.         System.out.println("分布式直连同步::Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds");   
  105.     }   
  106.   
  107.     //分布式直连异步调用  
  108.     public void test6shardpipelined() {   
  109.     //采用sharding 对象  
  110.         ShardedJedisPipeline pipeline = sharding.pipelined();   
  111.         long start = System.currentTimeMillis();   
  112.         for (int i = 0; i < 100000; i++) {   
  113.             pipeline.set("sp" + i, "p" + i);   
  114.         }   
  115.         List<Object> results = pipeline.syncAndReturnAll();   
  116.         long end = System.currentTimeMillis();   
  117.         System.out.println("分布式直连异步::Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");   
  118.     }   
  119.   
  120.     //分布式连接池同步调用, 线程安全  
  121.     public void test7shardSimplePool() {   
  122.         ShardedJedis one = pool.getResource();   
  123.   
  124.         long start = System.currentTimeMillis();   
  125.         for (int i = 0; i < 100000; i++) {   
  126.             String result = one.set("spn" + i, "n" + i);   
  127.         }   
  128.         long end = System.currentTimeMillis();   
  129.         pool.returnResource(one);   
  130.         System.out.println("分布式连接池同步调用::Simple@Pool SET: " + ((end - start)/1000.0) + " seconds");   
  131.     }   
  132.   
  133.     //分布式连接池异步调用  
  134.     public void test8shardPipelinedPool() {   
  135.         ShardedJedis one = pool.getResource();   
  136.   
  137.         ShardedJedisPipeline pipeline = one.pipelined();   
  138.   
  139.         long start = System.currentTimeMillis();   
  140.         for (int i = 0; i < 100000; i++) {   
  141.             pipeline.set("sppn" + i, "n" + i);   
  142.         }   
  143.         List<Object> results = pipeline.syncAndReturnAll();   
  144.         long end = System.currentTimeMillis();   
  145.         pool.returnResource(one);   
  146.         System.out.println("分布式连接池异步调用::Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");   
  147.     }   
  148.       
  149.       
  150.     
  151.     public static void main(String[] args) {  
  152.            
  153.     RedisDemo testDemo = new RedisDemo();  
  154.     try {  
  155.     testDemo.init();  
  156.     }catch (Exception Exc) {  
  157.           
  158.         Exc.printStackTrace();  
  159.         System.exit(0);  
  160.     }  
  161.       
  162.     System.out.println("init complete sucessfully!!!");   
  163.   
  164.     //测试普通同步方式  
  165.     testDemo.test1Normal();  
  166.     //测试事务方式Transactions  
  167.     testDemo.test2Trans();  
  168.     //采用管道方式,异步方式  
  169.     testDemo.test3Pipelined();  
  170.     //管道中调用事务,  
  171.     testDemo.test4combPipelineTrans();  
  172.     //分布式直连同步调用,用到了分片  
  173.     testDemo.test5shardNormal();  
  174.     //分布式直连异步调用  
  175.     testDemo.test6shardpipelined();  
  176.     //分布式连接池同步调用, 线程安全  
  177.     testDemo.test7shardSimplePool();  
  178.     //分布式连接池异步调用, 线程安全  
  179.     testDemo.test8shardPipelinedPool();  
  180.       
  181.   
  182.     try {  
  183.         testDemo.CleanUp();  
  184.         }catch (Exception Exc) {  
  185.               
  186.             Exc.printStackTrace();  
  187.             System.exit(0);  
  188.         }  
  189.          
  190.     }  
  191.       
  192.       
  193. } </span>  
整个代码,都是对redis进行key,value的设定,数据大小为10w,通过8种不同的接口进行测试,比较一下调用的效率

下面是运行的结果。


从图中可以看出,异步调用的效率非常高。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值