Redis实践(三)基于jedis开发java应用:redis的8种方式设置key,value,redisjedis
分享于 点击 20220 次 点评:69
Redis实践(三)基于jedis开发java应用:redis的8种方式设置key,value,redisjedis
前面已经进行了Redis的部署实践,下面要进行redis的Java开发实践
一、目标
搭建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
- <span style="font-family:Microsoft YaHei;font-size:14px;">package com.cwqsolo.redis.test;
- import java.util.Arrays;
- import java.util.List;
- 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;
- //RedisDemo 用来测试redis的使用接口
- public class RedisDemo {
- private static Jedis jedis;
- private static ShardedJedis sharding;
- static ShardedJedisPool pool;
- public static void init() throws Exception {
- List<JedisShardInfo> shards = Arrays.asList(
- new JedisShardInfo("192.168.136.144",6379),
- new JedisShardInfo("192.168.136.144",6380)
- ); //使用相同的ip:port,仅作测试
- jedis = new Jedis("192.168.136.144");
- sharding = new ShardedJedis(shards);
- JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
- pool = new ShardedJedisPool(config, shards);
- }
- public static void CleanUp() throws Exception {
- jedis.disconnect();
- sharding.disconnect();
- pool.destroy();
- }
- //测试普通同步方式, 设置10w个key,value,看用时多少。
- public void test1Normal() {
- long start = System.currentTimeMillis();
- for (int i = 0; i < 100000; i++) {
- String result = jedis.set("1n" + i, "1n" + i);
- }
- long end = System.currentTimeMillis();
- System.out.println("普通同步方式::Simple SET: " + ((end - start)/1000.0) + " seconds");
- }
- //测试事务方式Transactions, 设置10w个key,value,看用时多少。
- public void test2Trans() {
- long start = System.currentTimeMillis();
- Transaction tx = jedis.multi();
- for (int i = 0; i < 100000; i++) {
- tx.set("1t" + i, "1t" + i);
- }
- //System.out.println(tx.get("t1000").get());
- List<Object> results = tx.exec();
- long end = System.currentTimeMillis();
- System.out.println("事务方式::Transaction SET: " + ((end - start)/1000.0) + " seconds");
- }
- //采用管道方式,异步方式,一次发送多个指令,
- public void test3Pipelined() {
- Pipeline pipeline = jedis.pipelined();
- long start = System.currentTimeMillis();
- for (int i = 0; i < 100000; i++) {
- pipeline.set("p" + i, "p" + i);
- }
- //System.out.println(pipeline.get("p1000").get());
- List<Object> results = pipeline.syncAndReturnAll();
- long end = System.currentTimeMillis();
- System.out.println("管道方式异步::Pipelined SET: " + ((end - start)/1000.0) + " seconds");
- }
- //管道中调用事务,
- public void test4combPipelineTrans() {
- 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");
- }
- //分布式直连同步调用,用到了分片
- public void test5shardNormal() {
- 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("分布式直连同步::Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds");
- }
- //分布式直连异步调用
- public void test6shardpipelined() {
- //采用sharding 对象
- 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("分布式直连异步::Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");
- }
- //分布式连接池同步调用, 线程安全
- public void test7shardSimplePool() {
- 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("分布式连接池同步调用::Simple@Pool SET: " + ((end - start)/1000.0) + " seconds");
- }
- //分布式连接池异步调用
- public void test8shardPipelinedPool() {
- 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("分布式连接池异步调用::Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");
- }
- public static void main(String[] args) {
- RedisDemo testDemo = new RedisDemo();
- try {
- testDemo.init();
- }catch (Exception Exc) {
- Exc.printStackTrace();
- System.exit(0);
- }
- System.out.println("init complete sucessfully!!!");
- //测试普通同步方式
- testDemo.test1Normal();
- //测试事务方式Transactions
- testDemo.test2Trans();
- //采用管道方式,异步方式
- testDemo.test3Pipelined();
- //管道中调用事务,
- testDemo.test4combPipelineTrans();
- //分布式直连同步调用,用到了分片
- testDemo.test5shardNormal();
- //分布式直连异步调用
- testDemo.test6shardpipelined();
- //分布式连接池同步调用, 线程安全
- testDemo.test7shardSimplePool();
- //分布式连接池异步调用, 线程安全
- testDemo.test8shardPipelinedPool();
- try {
- testDemo.CleanUp();
- }catch (Exception Exc) {
- Exc.printStackTrace();
- System.exit(0);
- }
- }
- } </span>
下面是运行的结果。
从图中可以看出,异步调用的效率非常高。
相关文章
- 暂无相关文章
用户点评