spring data redis源码框架分析
2012-02-07 16:07
417 查看
redis是由Salvatore Sanfilippo用C语言编写的一个缓存系统,与memcached相比,提供了更多的处理复杂数据结构的方法;性能也非常的突出。
由于项目需要,自己简单地看了下spring新加入的模块spring data redis,spring data redis对jedis, jredis, rjc等redis的java客户端接口进行了进一部的抽象,类似于jdbcTemplate的实现。具体spring配置方式如下:
connectionFactory功能类似于spring 数据库连接的datasource,提供对远程redis server的连接访问; redisTemplate类似于SqlMapClientTemplate,提供对redis server访问的模板方法。
下面是spring data redis class diagram :
结合下面的代码我们进行分析:
spring data redis根据数据的类型进行了接口方法的拆分,如ValueOperations,ListOperations,SetOperations,ZSetOperations。template调用opsForList拿到具体的对哪种数据结构进行操作的对象,进而调用相应的操作方法。DefaultListOperations等对象使用回调函数的方法向redis server进行请求。
在RedisTemplate中,有一个重要的方法execute对connection进行预处理,包括是否使用pipeline,是否expose(暴露)connection等,对server进行请求后的返回结果进行后续的处理等。
本文出自 “黑白灰” 博客,请务必保留此出处http://leonmau.blog.51cto.com/2202260/774156
由于项目需要,自己简单地看了下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); } } }
本文出自 “黑白灰” 博客,请务必保留此出处http://leonmau.blog.51cto.com/2202260/774156
相关文章推荐
- spring-data-redis源码分析以及代码漏洞
- Spring源码追踪1——doGetBean(为什么org.springframework.data.redis.core.RedisTemplate的实例可以注入为ListOperations)
- redis客户端jedis&spring-data-redis源码赏析
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- Spring源码学习-spring data jpa源码分析
- 【Redis】spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- spring 框架整合mybatis的源码分析
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- Spring源码分析【0】-框架的基础:继承和接口调用链
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- spring-data-redis源码解读二
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- Spring的事物管理框架源码分析
- Spring集成Spring-data-redis RedisCacheManager缓存源码分析
- RPC框架(五)dubbo源码分析--Spring解析dubbo标签
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- Spring mvc Data Redis—Pub/Sub(附Web项目源码)