Spring Data Redis ---笔记2

官方文档:http://docs.spring.io/spring-data/data-redis/docs/current/reference/html/

概述:

接着上笔记1

文档结构:

这个部分将介绍Spring Data Redis 核心功能。

Redis support 将介绍Redis 模型的特性

5、Redis support (Redis 支持)

Redis 是Spring Data支持键值存储系统。下面引用工程首页的话:

“Redis 是一个先进的键值存储系统,它与memcached非常类似,但是dataset(数据库单位)不是volatile”(volatile防止编译器优化),它的值可以字符串。不仅仅是 lists 、sets、 有序 sets, 所有数据类型的操作都是原子性的,例如添加元素、移除元素,服务端Set的并集、交集,差异等等,Redis支持不同类型的排序能力。


Spring Data Redis提供简单配置,从应用很好操作Redis。它也提供与Redis交互低水平和高水平抽象操作,把用户最基础事务中解脱出来。

5.1. Redis Requirements(Redis 的先决条件)

Spring Redis 要求Redis 2.6及以上,jdk1.6及以上。这个语言绑定(连接器),Spring Redis 整合JedisJRedis(从1.7遗弃),SRP(从1.7遗弃)和Lettuce等4个流行的开源java库。如果你关心其它的connector(连接器),认为我们应该整合的进去,请给我们反馈。

5.2.Redis 支持高级8视图

Redis 支持提供几种组件(就是一种顺序的依赖):

对于多数任务,使用高水平抽象化链接Redis来支持你的应用服务是最好的选择,注意在任何一个点,可以在层级之间移动切换,例如,可以很容易得到一个低水平的链接(或者甚至是原生的库)来直接与Redis交互。

5.3. 链接Redis

第一个任务就是通过IOC容器使得Spring 与Redis存储相关连。为了做到这点,java connector连接器(或绑定)是有必要的。不管你选择哪个连接器,这里只是一系列的Spring API,对于所有的连接器的操作行为是一致的,在org.springframework.data.redis.connection包下,RedisConnectionRedisConnectionFactory 就是连接Redis所要用到的接口。

5.3.1 RedisConnection RedisConnectionFactory

RedisConnection 提供为Redis通讯构建一个块。它用于处理与Redis端的通讯。它也能自动转换潜在连接库异常来和Spring的DAO异常一致。所以可以切换连接器而不需要修改任何代码。

提示:对于极端情况,原生库API是需要的,RedisConnection提供了一个专有的方法getNativeConnection,这个方法将会返回一个原生通讯对象。


激活RedisConnection是通过RedisConnectionFactory创建的。除此之外,这个工厂类也可以做为PersistenceExceptionTranslator(持久化异常转换)的角色。举个例子:异常转换通常是通过使用注解@Repository和AOP。更多的详情请参考专门介绍的章节。

提示:依赖于潜在的配置,工厂类可以返回一个新的连接对象或现有的连接对象(在使用连接池或分享原生连接)

最简单的方式就是使用RedisConnectionFactory类去配置一个合适的连接器,然后通过IOC容器注入到需要的类当中。

警告:不幸的是,当前并不是所有的连接器都支持所有Redis特性。当调用一个连接API方法,如果它不支持的话将会抛出一个UnsupportedOperationException异常,这种情况将会在将来进行修复。完善众多的连接器。

5.3.2. 配置 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>

如果是生产环境,可能需要稍稍调整一下,例如设置主机和密码:

<?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>


5.3.3. 配置JRedis连接器(从1.7被废弃了)

JRedis是另个有名、开源的连接器。它是通过包org.springframework.data.redis.connection.jredis 对JRedis支持

典型配置文件如下

<?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="jredisConnectionFactory" class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory" p:host-name="server" p:port="6379"/>

</beans>

这个配置非常类似与Jedis,有一个值得注意的意外就是。在默认情况下,JedisConnectionFactory采用连接池进行连接。为了使用JRedis的连接池,需要配置JredisConnectionFactory实例化一个JredisPool.例如:

<?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">

  <bean id="jredisConnectionFactory" class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory">
    <constructor-arg>
      <bean class="org.springframework.data.redis.connection.jredis.DefaultJredisPool">
        <constructor-arg value="localhost" />
        <constructor-arg value="6379" />
      </bean>
    </constructor-arg>
  </bean>

</beans>

5.3.4.配置SRP连接器(从1.7开始废弃了)

SRP(Sam's Redis Protocol的缩写)是第三方开源的连接器。通过Spring包下 org.springframework.data.redis.connection.srp

到目前为止,它们的配置很容易猜的到什么样子的

<?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="srpConnectionFactory" class="org.springframework.data.redis.connection.srp.SrpConnectionFactory" p:host-name="server" p:port="6379"/>

</beans>

不必说,这个配置非常类似如上其他的连接器。

5.3.5. 配置Lettuce连接器

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>
这里有一些Lettuce特有的连接参数需要稍作修改。所有LettuceConnection都是通过LettuceConnectionFactory创建的,对于所有非阻塞式非事务操作的情况,它们分享相同线程安全的原生连接。可以设置shareNativeConnection设置为false来每次都使用特定的连接。

LettuceConnectionFactory也可以配置一个连接池LettucePool,它用来连接池块和事务连接。或是所有连接共享线程,设置shareNativeConnection设置为false。

5.5.RedisTemplate该上场工作了

大多数用户都喜欢使用RedisTemplate类和它所对应的包——org.springframework.data.redis.core,事实上这个template是Redis 模块的中心类,它具有丰富的操作集。这个template是对于Redis交互的高度抽象化。RedisConnection提供一个低水平的方法接受和返回一个二进制值(字节数组),template把序列化和连接管理考虑在内,使用用户从这些细节中脱离出来。

更多的是, template提供操作视图(可以参考如下Redis的命令),它提供丰富的、抽象化的接口来操作具体不同类型key(通过KeyBound接口),如下所示

表格1.操作视图
接口描述
Key Type Operations
ValueOperationsRedis字符串(或值)操作
ListOperationsRedis 列表操作
SetOperationsRedis set集合操作
ZSetOperationsRedis zset(有序 set集合)操作
HashOperationsRedis hash操作
HyperLogLogOperationsRedis HyperLogLog操作例如(pfadd,pfcount,..)
GeoOperationsRedis空间索引操作例如(GEOADD,GEORADIUS,..)
Key Bound Operations(键绑定操作)
BoundValueOperationsRedis字符串(或者值)键绑定操作
BoundListOperationsRedis 列表键绑定操作
BoundSetOperationsRedis set 集合键绑定操作
BoundZSetOperationsRediszset(有序set集合)键绑定操作
BoundHashOperationsRedis hash 键绑定操作
BoundGeoOperationsRedis 键绑定空间索引操作

一旦配置好,这个template将会是线程安全的,它可以被多个实例对象重复使用。

遵循开箱即用原则,RedisTemplate 使用基于java序列化完成大多数的操作。这就意味着任何对象的读写都可以使用这个template实现序列化/反序列化。这个序列化机制很容易在template进行改变,Redis模块提供几个序列化的实现类,在org.springframework.data.redis.serializer包下-详情请参考Serializer。你也可以设置任何序列为null,而使用RedisTemplate原生的字节数组。这时候你需要设置enableDefaultSerializer属性为false。注意:template要求所有的key为非null,只要序列化接受值是可以为null,可以阅读javadoc获取更多序列化的信息。

在很多情况下,一个特定的template视图是必要的。声明一个视图作为一个依赖,然后注入这个template:容器将会自动转换来消除opsFor[X]调用:

<?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:use-pool="true"/>
  <!-- redis template definition -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
  ...

</beans>

public class Example {

  // 注入一个实际 template
  @Autowired
  private RedisTemplate<String, String> template;

  // 把template转为 ListOperations 操作视图
  @Resource(name="redisTemplate")
  private ListOperations<String, String> listOps;

  public void addLink(String userId, URL url) {
    listOps.leftPush(userId, url.toExternalForm());
  }
}

待续。。。。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值