Spring-data-redis 第三天(Pipeline)
1.我们首先来说下pipeline(我只是 一个想自学的小人物,很多地方说的不对 请一定要一定要说一下,谢谢)Pipeline 也就是管道,也是一种设计思想,在很多web 服务器设计上都有体现,比如很出名的Netty(这是下一个学习任务) tomcat 还有Struts2 框架。所以说Pipeline 是什么呢,
在我看来,我觉得用一个词很容易描述,工作流,一个request进来,通过管道,管道中就有很多网(filter,handler)然后从另一个口出来, 这是我理解的,但这看起来是同步的呀,也就是一条道走到黑那种,那Netty 这种是异步的呀,想了想,结合Redis中得pipeline,同样 我们先设定管道,然后把一条条request 也就是set get 操作放入管道中,然后一起执行,这个过程中 和普通的没有Redis Pipeline有什么区别,
非pipleline模式:
Request---->执行
---->Response
Request---->执行
---->Response
Pipeline模式下:
Request---->执行,Server将响应结果队列化
Request---->执行,Server将响应结果队列化
---->Response
---->Response
Client端根据Redis的数据协议,将响应结果进行解析,并将结果做类似于“队列化”的操作。
这个异步的过程看起来就很明白了,也明白了为什么要使用Pipeline 的效率高了。
2 Jedis 中Pipeline的操作:
Pipeline p1 = jedis.pipelined();
p1.incr(key);
System.out.println("Request incr");
p1.incr(key);
System.out.println("Request incr");
//结束pipeline,并开始从相应中获得数据
List<Object> responses = p1.syncAndReturnAll();
if(responses == null || responses.isEmpty()){
throw new RuntimeException("Pipeline error: no response...");
}
for(Object resp : responses){
System.out.println("Response:" + resp.toString());//注意,此处resp的类型为Long
}
那么Spring-data-redis 中是怎么支持Pipeline的呢?
这个我也想问!!!
我看了最新的源码,最后还是发现还是要用和事务的调用方法一样,要使用redisTemplate 中的 rediscallback 中的connection 也就是 我们在Spring 配置文件中注入的redisconnection,这货不是我们原生态的Jedis操作么。Spring-data-redis 也就是提供了一个配饰器吧。 把不同的Redis 的Java API 给整合在一起,有没有大牛教教我我这么理解对不对,要是错的话应该是什么,把这个帖子发到技术,应该能有大牛给解释下。先跪谢了。
更新:
我用的版本和github版本不一样! 最新的spring-data-redis 中 有支持pipeline的方法
public List<Object> executePipelined(final SessionCallback<?> session, final RedisSerializer<?> resultSerializer) {
Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
Assert.notNull(session, "Callback object must not be null");
RedisConnectionFactory factory = getConnectionFactory();
// bind connection
RedisConnectionUtils.bindConnection(factory);
try {
return execute(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
boolean pipelinedClosed = false;
try {
Object result = executeSession(session);
if (result != null) {
throw new InvalidDataAccessApiUsageException(
"Callback cannot return a non-null value as it gets overwritten by the pipeline");
}
List<Object> closePipeline = connection.closePipeline();
pipelinedClosed = true;
return deserializeMixedResults(closePipeline, resultSerializer, hashKeySerializer, hashValueSerializer);
} finally {
if (!pipelinedClosed) {
connection.closePipeline();
}
}
}
});
} finally {
RedisConnectionUtils.unbindConnection(factory);
}
}
还有昨天的事务,最新的springdataredis中 也对multi方法做了处理,还是要看最新的东西,,旧的还是不全
页:
[1]