spring data redis源码框架分析

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源码框架分析_第1张图片

结合下面的代码我们进行分析:

 

 

public class Example{
	@autowired
	private RedisTemplate 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() {
			
			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  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 execute(RedisCallback 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);
			}
		}
	}

你可能感兴趣的:(Database,Spring,Java,redis)