RedisTemplate:execute与executePipelined
1.官网注释
execute @Nullable public <T> T execute(RedisCallback<T> action)
- Description copied from interface: RedisOperations
Executes the given action within a Redis connection. Application exceptions thrown by the action object get propagated to the caller (can only be unchecked) whenever possible. Redis exceptions are transformed into appropriate DAO ones. Allows for returning a result object, that is a domain object or a collection of domain objects. Performs automatic serialization/deserialization for the given objects to and from binary data suitable for the Redis storage. Note: Callback code is not supposed to handle transactions itself! Use an appropriate transaction manager. Generally, callback code must not touch any Connection lifecycle methods, like close, to let the template do its work.
Specified by:
execute in interface RedisOperations
- Type Parameters:
T - return type - Parameters:
-
- callback object that specifies the Redis action. Must not be null.
- Returns:
- result object returned by the action or null
从以上注释可知,该方法执行给定的Action在一次redis连接中。执行完成之后可以返回多个结果(List)。但是需要注意的是,它本身不支持解决事务,如果需要多个操作为一个事务,需要使用专门的TransactionSynchronizationManager。当然了,如果我们一系列操作都是查询,不需要开启事务,使用这个很好。
executePipelined public List<Object> executePipelined(SessionCallback<?> session)
- Description copied from interface: RedisOperations
Executes the given Redis session on a pipelined connection. Allows transactions to be pipelined. Note that the callback cannot return a non-null value as it gets overwritten by the pipeline.
Specified by:
executePipelined in interface RedisOperations
- Parameters:
-
- Session callback
- Returns:
- of objects returned by the pipeline
所以,两者的区别就在注释中一目了然了。executePipelined
是可以允许我们执行事务的。executePipelined
还有一个需要注意的点,就是虽然重写了回调函数,但是回调函数还是有可能返回空值的。注释里说是在被管道覆盖的时候,这是在说什么呢?
2.executePipelined
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, enableTransactionSupport); 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); } }
这里判断了executeSession
的返回结果是否非空,这个函数最终会调用下面这个函数:
/** * Executes all the given operations inside the same session. * * @param operations Redis operations * @return return value */ <K, V> T execute(RedisOperations<K, V> operations) throws DataAccessException;
正如注释中所说的那样,这个函数会执行当前会话中的所有的operation,以判断是否已经有了操作,保证该事务不被其它操作干扰。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Pyhton连接SQL Server数据库解决方案
为了提高工作效率及便利性,拟自主开发一款一键自动化运维小工具;其中主要一项功能用于与SQL Server数据库进行交互,程序可以根据数据库中数据情况,调用对应函数逻辑,做出相应的操作,以达到自动化运维的效果;关于Python连接SQL Server的方法,调研了一些Python模块,最终选取了pyodbc模块: pymysql模块最开始计划选取pymysql模块,安装比较顺利,但连接始终失败,提示异常“pymysql.err.OperationError:[Errorno10054]”;经过排查,连接所需要的信息均正确,但经过确认后发现,pymysql模块仅适用于连接mysql数据库,而无法连接sqlserver数据库,之前也由于调研时意外看错,实际计划选用的Python拓展包实际应为“pymssql”。 pymssql模块pymssql模块在安装时就问题频发,不论是在线直接安装,还是在Python官网下载安装文件离线安装均未成功; 最初安装过程中提示“_mssql.c(266) : fatal error C1083: Cannot open include file: 'sqlfr...
- 下一篇
5行代码秀碾压,比Keras还好用的fastai来了,尝鲜PyTorch 1.0必备伴侣
在今天的F8(Facebook开发者大会)上,深度学习框架PyTorch 1.0 rc1版如期发布。然而在海外的论坛上,另一个开源库的关注度不遑多让。 它就是fastai 1.0。 简单来说,fastai只要一个API,就包含了所有常见的深度学习应用。堪称实用版的“要你命3000”。而且许多性能指标,已经超越了Keras。 比如,5行代码完成Keras用31行才能解决的事情。同时,误差表现也更好。难怪在Hacker News上,fastai的关注度比PyTorch 1.0还高…… 而且fastai基于PyTorch 1.0框架,也被Facebook官方重点宣传。这两个全新的框架同时下载配合使用,疗效更佳。 这个悠长假期,你不试试么? fastai 1.0 fastai的出品方是fast.ai,机构规模不大,但因为提供大量免费机器学习课程,而名声
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7设置SWAP分区,小内存服务器的救世主
- Mario游戏-低调大师作品
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- 2048小游戏-低调大师作品
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库