redis 管道技术(redis学习九)

Redis 事务命令

      • java调用
        • 测试代码1
      • spring调用
        • pom.xml配置 核心依赖
        • string-redis.xml spring配置文件
        • 测试代码1

java调用

测试代码1

package redis.core;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

public class PipelineTest {
	private static final Logger logger = LoggerFactory.getLogger(PipelineTest.class);

	public static void main(String[] args) {
		JedisPoolConfig config = new JedisPoolConfig();
		 
		//最大空闲连接数, 默认8个
		config.setMaxIdle(50);
		 
		//最大连接数, 默认8个
		config.setMaxTotal(100);
		 
		//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
		config.setMaxWaitMillis(20000);
		 
		JedisPool pool = new JedisPool(config, "localhost");
		
		Jedis jedis = pool.getResource();
		long start = System.currentTimeMillis();
		// 开启流水线
		Pipeline pipeline = jedis.pipelined();
		for (int i = 1; i <= 50000; i++) {
			pipeline.set("pipeline_key_" + i, "pipeline_value_" + i);
			pipeline.get("pipeline_key_" + i);
		}
		
//		pipeline.sync();  //执行同步,但是不返回结果
//		pipeline.syncAndReturnAll(); //执行同步,返回结果
		
		List syncAndReturnAll = pipeline.syncAndReturnAll();
		
		long end = System.currentTimeMillis();
		
		logger.debug("耗时:{}, 操作读写次数:{}", (end - start), syncAndReturnAll.size());
	}
}
 
 
  

spring调用

pom.xml配置 核心依赖

		
			redis.clients
			jedis
			2.9.0
		
		
		
			org.slf4j
			slf4j-log4j12
			1.7.25
		
		
		
		    org.springframework.data
		    spring-data-redis
		    1.8.4.RELEASE
		

string-redis.xml spring配置文件



    
      
    
    
    
    
        
        
        
    

    
    
        
        
        
        
    
    
	
	
	
	

    
    
        
        
        
    
    

测试代码1

package redis.core;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

public class SpringPipelineTest {
	
	private static final Logger logger = LoggerFactory.getLogger(SpringPipelineTest.class);

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		
		long start = System.currentTimeMillis();
		SessionCallback callback = operations -> {
			// 开启流水线
			for (int i = 1; i <= 50000; i++) {
				operations.boundValueOps("pipeline_key_" + i).set("pipeline_value_" + i * 2);
				operations.boundValueOps("pipeline_key_" + i).get();
			}
			
			return null;
		};
		List syncAndReturnAll = redisTemplate.executePipelined(callback);
		long end = System.currentTimeMillis();
		logger.debug("耗时:{}, 操作读写次数:{}", (end - start), syncAndReturnAll.size());
	}

}


你可能感兴趣的:(redis)