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<Object> syncAndReturnAll = pipeline.syncAndReturnAll();
long end = System.currentTimeMillis();
logger.debug("耗时:{}, 操作读写次数:{}", (end - start), syncAndReturnAll.size());
}
}
spring调用
pom.xml配置 核心依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- slf4j日志统一管理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- spring中的redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
string-redis.xml spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="false">
<!-- 占位符配置文件 -->
<!-- <context:property-placeholder location="classpath:redis.properties" /> -->
<!-- redis 连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- Spring-redis连接池工厂配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="timeout" value="2000" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 序列化 String类型 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!-- 序列化 对象 -->
<!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<!-- redisTemplate 定义 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
测试代码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());
}
}