Redis-12Redis 流水线( pipeline )

版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/82871614

概述

Redis 的事务的各类问题,在事务中 Redis 提供了队列, 这是一个可以批量执行任务的队列,这样性能就比较高,但是使用 multi…exec 事务命令是有系统开销的,因为它会检测对应的锁和序列化命令。

有时候我们希望在没有任何附加条件的场景下去使用队列批量执行一系列的命令,从而提高系统性能,这就是 Redis 的流水线( pipelined )技术。

而现实中 Redis 执行读/写速度十分快,而系统的瓶颈往往是在网络通信中的延时,如下

在这里插入图片描述

为了解决这个问题,可以使用 Redis 的流水线 , 但是 Redis 的流水线是一种通信协议没有办法通过客户端演,不过我们可以通过 JavaAPI 或者使用 Spring 操作它.


前置条件: reids中的各种属性JavaAPI或者是Spring操作的时候保持一致,一边观察性能。

JavaAPI 操作Redis Pipeline

package com.artisan.redis.pipelined;

import java.util.List;

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

public class PipelineWithJavaDemo {

	public static void main(String[] args) {

		// 实例化jedisPoolConfig并设置相关参数
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(6);
		jedisPoolConfig.setMaxTotal(20);
		jedisPoolConfig.setMaxWaitMillis(3000);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(300000);
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
		// 使用jedisPoolConfig初始化redis的连接池
		JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.31.66", 6379);
		// 从jedisPool中获取一个连接
		Jedis jedis = jedisPool.getResource();
		jedis.auth("artisan");

		long beginTime = System.currentTimeMillis();

		// 开启流水线
		Pipeline pipeline = jedis.pipelined();

		// 测试10 万条的读/写 2 个操作
		for (int i = 0; i < 100000; i++) {
			int j = i + 1 ;
			jedis.set("pipeline_key" + j , "pipeline_value" + j);
			jedis.get("pipeline_key" + j);
		}

		// 这里只执行同步,但是不返回结果
		// pipeline.sync();

		// 将返回执行过的命令返回的 List 列表结果
		List list = pipeline.syncAndReturnAll();

		long endTime = System.currentTimeMillis();
		System.out.println("10 万条的读/写 2 个操作 ,耗时:" + (endTime - beginTime) + "毫秒");

	}

}


输出

10 万条的读/写 2 个操作 ,耗时:87030毫秒


Spring操作Redis Pipeline

在 Spring 中使用 RedisTemplate提供的 executePipelined 方法即可行流水线

package com.artisan.redis.pipelined;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

public class PipelineWithSpringDemo {

	@SuppressWarnings({ "rawtypes", "unchecked", "unused", "resource" })
	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/spring-redis-string.xml");
		RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);
		
		SessionCallback sessionCallback = (SessionCallback) (RedisOperations ops) -> {
			
			for (int i = 0; i < 100000; i++) {
				int j = i + 1 ;
				ops.boundValueOps("pipeline_key" + j).set("pipeline_value" + j);
				ops.boundValueOps("pipeline_key" + j).get();
			}
			
			return null;
		};
		
		long beginTime = System.currentTimeMillis();
		// 执行 Redis 的流水线命令
		List list = redisTemplate.executePipelined(sessionCallback);

		long endTime = System.currentTimeMillis();
		System.out.println("10 万条的读/写 2 个操作 ,耗时:" + (endTime - beginTime));
	}

}


INFO : org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@73a8dfcc: startup date [Thu Sep 27 19:55:06 CST 2018]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/spring-redis-string.xml]
10 万条的读/写 2 个操作 ,耗时:2161

这里只是为了测试性能而己,当你要执行很多的命令并返回结果的时候 , 需要考虑 List 对象的大小,因为它会“吃掉”服务器上许多的内存空间 , 严重时会导致内存不足,引发 JVM 溢出异常.


代码

代码托管到了 https://github.com/yangshangwei/redis_learn

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/82871614