- 浏览: 103107 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
qiushuzhao:
';lll
spring data redis源码框架分析 -
qiushuzhao:
[color=yellow][/color]lll
spring data redis源码框架分析 -
jiuyuehe:
博主你好,用了你方法以后果然没有阻塞了,但是原理还是不太明白, ...
[转载]Java Process.exitValue & Process.waitFor()
redis是由Salvatore Sanfilippo用C语言编写的一个缓存系统,与memcached相比,提供了更多的处理复杂数据结构的方法;性能也非常的突出。
由于项目需要,自己简单地看了下spring新加入的模块spring data redis,spring data redis对jedis, jredis, rjc等redis的java客户端接口进行了进一部的抽象,类似于jdbcTemplate的实现。具体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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"/> <!-- Configurer that replaces ${...} placeholders with values from a properties file --> <context:property-placeholder location="classpath:redis.properties"/> <context:annotation-config /> <context:component-scan base-package="org.springframework.data.redis.samples"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="connectionFactory"/> </beans>
connectionFactory功能类似于spring 数据库连接的datasource,提供对远程redis server的连接访问; redisTemplate类似于SqlMapClientTemplate,提供对redis server访问的模板方法。
下面是spring data redis class diagram
结合下面的代码我们进行分析:
public class Example{ @autowired private RedisTemplate<String,String> template; public void addLink(String userId, URL url){ template.opsForList.leftPush(userId, url.toExternalForm); } }
spring data redis根据数据的类型进行了接口方法的拆分,如ValueOperations,ListOperations,SetOperations,ZSetOperations。template调用opsForList拿到具体的对哪种数据结构进行操作的对象,进而调用相应的操作方法。DefaultListOperations等对象使用回调函数的方法向redis server进行请求。
public Long leftPush(K key, V value) { final byte[] rawKey = rawKey(key); final byte[] rawValue = rawValue(value); return execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) { return connection.lPush(rawKey, rawValue); } }, true); }
在RedisTemplate中,有一个重要的方法execute对connection进行预处理,包括是否使用pipeline,是否expose(暴露)connection等,对server进行请求后的返回结果进行后续的处理等。
/** * Executes the given action object within a connection that can be exposed or not. Additionally, the connection * can be pipelined. Note the results of the pipeline are discarded (making it suitable for write-only scenarios). * * @param <T> return type * @param action callback object to execute * @param exposeConnection whether to enforce exposure of the native Redis Connection to callback code * @param pipeline whether to pipeline or not the connection for the execution * @return object returned by the action */ public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) { Assert.notNull(action, "Callback object must not be null"); RedisConnectionFactory factory = getConnectionFactory(); //由spring中配置的JedisConnectionFactory创建连接 RedisConnection conn = RedisConnectionUtils.getConnection(factory); boolean existingConnection = TransactionSynchronizationManager.hasResource(factory); preProcessConnection(conn, existingConnection);//调用StringRedisTemplate的preProcessConnection方法 //拿到了DefaultStringRedisConnection boolean pipelineStatus = conn.isPipelined();//判断是否打开pipeline if (pipeline && !pipelineStatus) { conn.openPipeline(); } try { RedisConnection connToExpose = (exposeConnection ? conn : createRedisConnectionProxy(conn)); T result = action.doInRedis(connToExpose); // TODO: any other connection processing? // 对后续结果进行处理,但postProcessResult还是直接返回result的,不知为何? return postProcessResult(result, conn, existingConnection); } finally { try { if (pipeline && !pipelineStatus) { conn.closePipeline(); } } finally { RedisConnectionUtils.releaseConnection(conn, factory); } } }
发表评论
-
Java Enum
2011-10-08 20:31 1029public enum Operation { ... -
Java内部类访问
2011-10-08 17:37 900当内部类为非静态时: public class In ... -
JVM Method Table
2011-10-07 16:10 1232方法表 为了提高访问效率,必须仔细的设计存储在方法区中的 ... -
JVM Symbolic refer 2 Direct Refer
2011-10-06 17:31 2Method Area: Symbolic Referen ... -
【转载】JVM中方法区
2011-09-29 14:17 1086原文链接:http://hi.baidu. ... -
JVM初探
2011-09-29 13:49 845概念: JAVA的JVM的内存可分为5个区:堆(heap ... -
java ClassLoader
2011-09-28 17:02 545JVM规范定义了两种类型的类装载器:启动内装载器 (bo ... -
【转载】java 回调函数
2011-09-18 16:01 1041java“回调函数” 所谓 ... -
[转载]Java Process.exitValue & Process.waitFor()
2011-09-17 13:14 15298Process.exitValue() 采用非阻塞的方式返回, ... -
Java读取classpath中的文件
2011-08-29 11:06 7725/** * 类AppsImport.java的实现描 ... -
【转载】jvm的内存调优
2011-08-23 17:30 1365原文链接:http://k ... -
【转载】Java 垃圾回收策略调优,实践篇
2011-08-23 17:13 889转载自:http://www.douban.c ... -
[转载]线程sleep()和wait()的区别
2011-07-01 15:07 843线程sleep()和wait()的区别 ... -
[转载] dbcp配置 原文链接http://www.blogjava.net/aoxj/archive/2008/02/19/180704.html
2011-06-15 10:42 849dbcp配置中文版本,翻译自apache 官方文档,原文请见 ... -
checked Exception and RuntimeException
2011-05-29 15:25 9191. RuntimeException 虚拟机执行程序时,如果 ... -
【转载】编程式事务 声明式事务区别
2011-05-09 19:08 978原文链接:http://topic.csdn.net/u/20 ... -
【转载】事务隔离性
2011-05-04 19:44 955原文:http://www.rsky.com.cn/Artic ... -
[转载]java回调函数
2011-04-28 16:55 899转载自:http://www.blogjava.net/ ... -
【转载】jms topic与jms queue区别
2011-02-18 17:29 10913作者longdick http://longdick.i ... -
【转载】JAVA NIO 简介
2011-02-18 11:30 891原文链接:http://www.iteye.com/topic ...
相关推荐
spring和redis集成有很多方式,看到网上很多都是使用redistemplate自己去做redis 的一些操作,但是对于我们开发来说,肯定是使用越方便越好,于是乎就有了spring的对redis或者memcahe这些换成框架的封装,只需要引入...
spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()
spring-data-redis至redis练习源码
赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...
Spring-data-redis 1.7.6 & source源码
本次课程以SpringData为中心,重点讲解了其JPA组件,扩展讲解了redis,mongDB,ES组件,并且对部分组件做了必要的源码分析。而且在课程的最后部分加入了一个综合案例,可以将前面章节所学知识点应用到一个项目中,帮助...
主要介绍了SpringBoot整合SpringDataRedis的过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://zhaoqilong3031.iteye.com/blog/1483294
spring和redis缓存工具类包
本篇文章主要介绍了java之redis篇,主要详细的介绍了spring-data-redis整合,有兴趣的可以了解一下。
NULL 博文链接:https://kanpiaoxue.iteye.com/blog/1995424
春天-redis 带有@Cacheable Spring 3 和 4 的 Redis 示例。
spring-data-redis-1.0.2开发包+源码
spring-data-redis.jar(包含源码包)
shiro+spring+data+session+redis实现单点登录,这是一个不错的案例,学习和参考都是很不错的
springboot整合redis集群(三种方式)源码
spring-data不必介绍,附件中包括spring-data一些例子,包括整合jpa,mongodb,neo4j,redis的应用,有兴趣的朋友可以参考一下。
NULL 博文链接:https://fxzcollege6.iteye.com/blog/2210381
使用Spring Data集成缓存中间件Redis,加快访问速度。 使用Spring Data集成全文搜索搜索引擎ElasticSearch,实现文章信息的快速搜索和关键字的高亮显示。 前台前端使用HTTP客户端Axios进行异步请求,使用Vue完成数据...