spring redis 源码分析

栏目: Java · 发布时间: 6年前

内容简介:背景Jedis源码分析下文仅以redis 五种类型的 String和List 数据结构为例:

简介

Spring Data Redis 源代码 spring-projects/spring-data-redis 示例代码

ListOperations<String, Person> listOps = template.listOps();
listOps.rightPush(random, new Person("Jane", "Smith"));
List<Person> peopleOnSecondFloor = listOps.range("users:floor:2", 0, -1);

背景Jedis源码分析

spring-data-redis实现

下文仅以 redis 五种类型的 String和List 数据结构为例:

spring redis 源码分析

RedisTemplate 和ValueOperations、ListOperation 互相持有对方的引用, RedisTemplate 作为用户操作的入口对象, ValueOperations、ListOperation 等负责分担五种数据类型的 操作。

spring redis 源码分析

  1. 数据操作最终由 JedisConnection 来完成
  2. 从spring-data-redis 实现看,spring-data-redis 只是将jedis 作为 redis 访问的 工具 之一,并没有严格绑定
  3. JedisConnection 与 JedisStringCommands、JedisListCommands 等互相持有对方的引用,JedisStringCommands、JedisListCommands 等负责分担五种数据类型的 操作。

spring redis 源码分析

public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
	RedisConnectionFactory factory = getRequiredConnectionFactory();
	RedisConnection conn = null;
	try {
		if (enableTransactionSupport) {
			conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
		} else {
			conn = RedisConnectionUtils.getConnection(factory);
		}
		boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
		RedisConnection connToUse = preProcessConnection(conn, existingConnection);
		boolean pipelineStatus = connToUse.isPipelined();
		if (pipeline && !pipelineStatus) {
			connToUse.openPipeline();
		}
		RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
		T result = action.doInRedis(connToExpose);
		if (pipeline && !pipelineStatus) {
			connToUse.closePipeline();
		}
		return postProcessResult(result, connToUse, existingConnection);
	} finally {
		RedisConnectionUtils.releaseConnection(conn, factory);
	}
}

RedisTemplate使用模板模式,提供了高层调用结构和基本的逻辑实现(execute逻辑就是:建连接,序列化,发请求数据,拿数据,返回)。ValueOperations、ListOperation 对应的命令由它们自己实现,双方的结合点就是callback。这和spring-jdbc中的JdbcTemplate非常相像。

ScriptingCommands

redis中提供对 lua 脚本的支持,jedis和sdr自然也不甘落后,也都提供了支持。

反应在jedis上,就是ScriptingCommands接口,从中可以看出一种感觉,eval命令和set命令并无本质不同,在实现上都是完成参数的传递即可,并没有因为其功能不同,有特别的处理。

public interface ScriptingCommands {
	// 以eval方式执行lua脚本
    Object eval(String script, int keyCount, String... params);
    Object eval(String script, List<String> keys, List<String> args);
    Object eval(String script);
    // 以evalsha方式执行lua脚本
    Object evalsha(String sha1);
    Object evalsha(String sha1, List<String> keys, List<String> args);
    Object evalsha(String sha1, int keyCount, String... params);
    // 判断lua脚本是否存在
    Boolean scriptExists(String sha1);
    List<Boolean> scriptExists(String... sha1);
    // 加载lua脚本
    String scriptLoad(String script);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试