Spring Data Redis (Redis Support)

本文介绍如何使用 Spring Data Redis 连接 Redis 数据库,包括配置 Jedis 和 Lettuce 连接器的方法,以及如何利用高级抽象封装简化与 Redis 的交互。

Redis support

Redis是Spring Data支持的 kv 存储中的一种。引用Redis官网首页内容如下:

Redis是一个先进的key-value存储。它很像 MemcacheD,但是它的数据集是稳定的,存储的值可以是像MemcacheD中的string字符串类型,也可以是lists、sets和排序的sets。所有这些类型都能通过 push/pop或add/remove命令进行原子操作,还能在服务端执行sets类型的并集、交集和差集,等等。Redis还支持不同类型的排序功能。

Spring Data Redis通过简单的配置就可使Spring应用访问Redis。它提供了底层的和高级的抽象封装,来完成与存储的交互,使使用者不再关注底层。

1. Redis Requirements基础要求
Spring Data Redis要求 Redis2.6及以上版本,Java SE6.0及以上版本。依据多种语言绑定(连接器),Spring Data Redis集成了Jedis、JRedis、SRP、Lettuce四种流行的Java开源库来使用Redis,其中JRedis和SRP从1.7开始不赞成使用。如果你觉得应该有另外的连接器应该集成进来,请反馈给我们。

2. Redis Support High Level View 支持高级别的视图
Redis support提供了多种组件(按依赖的顺序排序):
多大多少任务来说,使用高级的抽象封装和支持的服务是最好的选择。注意,有时候你可能会在这些层次之间来选择。例如,很简单的使用底层连接来直接和Redis进行交互。

3. Connecting to Redis 连接Redis
使用Redis和Spring的第一步是建立连接,可以通过IoC容器来建立。这时我们需要一个Java连接器(绑定)。无论选择哪个库,这里只有一套统一操作各种连接器的API,就是位于org.springframework.data.redis.connection 目录下的RedisConnection 和RedisConnectionFactory 接口,使用他们来检索和Redis的连接状态。

3.1. RedisConnection and RedisConnectionFactory
RedisConnection 提供了与Redis通讯的构件来处理与Redis后端的通讯。它会自动的将底层的连接库异常转化到Spring一致的DAO异常层,所以你可以自由的切换连接器而不用修改任何代码,并保证一样的操作语义。

对一些特例来说,可能需要原生的库API。RedisConnection提供了一个专用的方法getNativeConnection来返回需要的连接,底层的对象使用它来进行通讯。

通过RedisConnectionFactory来创建多个活跃的RedisConnection。另外这个工厂类还可以作为PersistenceExceptionTranslator ,意味着一旦声明了你就可以进行透明的异常转化。比如,可以通过使用@Repository 注解和AOP来实现异常转化。获取更多的信息,请查看Spring框架文档的专用部分。

依赖底层基础的配置,这个工厂类可以返回一个新的连接或一个已经存在的连接(通过使用池或共享的本地连接)。

The easiest way to work with a RedisConnectionFactory is to configure the appropriate connector through the IoC container and inject it into the using class.
使用RedisConnectionFactory 的最简单的方式是,通过IoC容器配置合适的连接器,然后注入到使用类中。

不幸地是,当前并不是所有的连接器都支持所有的Redis特性。当使用连接API调用一个底层库不支持的方法时,会抛出一个UnsupportedOperationException 异常。随着各种连接器成熟后,这种情况在未来将会被修复。

3.2. Configuring Jedis connector 配置Jedis连接器

Jedis 是 Spring Data Redis 模块支持的连接器中的一种,位于org.springframework.data.redis.connection.jedis目录下。使用最简单的形式,Jedis的配置如下:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- Jedis ConnectionFactory -->
  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>

</beans>

For production use however, one might want to tweak the settings such as the host or password:
在生成环境使用时,你可能希望调整一下设置,像 host或 password。

<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="server" p:port="6379" />

</beans>

3.3. Configuring JRedis connector (Deprecated since 1.7)

3.4. Configuring SRP connector (Deprecated since 1.7)

3.5. Configuring Lettuce connector
Lettuce是第四个被Spring Data Redis支持的开源连接器,它位于org.springframework.data.redis.connection.lettuce目录下。

它的配置很容易猜到:

<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" p:host-name="server" p:port="6379"/>

</beans>

There are also a few Lettuce-specific connection parameters that can be tweaked. By default, all LettuceConnection s created by the LettuceConnectionFactory share the same thread-safe native connection for all non-blocking and non-transactional operations. Set shareNativeConnection to false to use a dedicated connection each time. LettuceConnectionFactory can also be configured with a LettucePool to use for pooling blocking and transactional connections, or all connections if shareNativeConnection is set to false.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值