Spring Data Redis(Support Classes)

Support Classes

程序包org.springframework.data.redis.support 提供了各种可重复利用的组件,这些组件依赖于Redis作为存储支撑。该程序包包含了多种基于JDK的接口实现,如自动计算器、JDK集合。

自动计算器使得Redis的键增量使用更简单;集合使得更容易管理Redis keys,并使用最少的API和很少的存储操作。
RedisSet 和 RedisZSet接口使得访问集合的操作更加简单,像交集、并集等。
RedisList接口具有List、Queue和Deque(它们共同具有阻塞特性)的特性,实现了像FIFO(先进先出队列)、LIFO(后进先出堆栈)、固定大小的集合功能。
以上的封装对Spring Data Redis来说只需很少的配置即可,配置如下:

<?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="queue" class="org.springframework.data.redis.support.collections.DefaultRedisList">
    <constructor-arg ref="redisTemplate"/>
    <constructor-arg value="queue-key"/>
  </bean>

</beans>
public class AnotherExample {

  // injected
  private Deque<String> queue;

  public void addTag(String tag) {
    queue.push(tag);
  }
}

正如上面示例展示的一样,代码实现了解耦,跟实际的存储实现没有一点关系;实际上并没有迹象指明底层使用的是Redis。这使得应用可以透明的从开发环境转移到生产环境,极大的提高了测试能力(测试时底层Redis的实现可以使用内存代替,不必搭建Redis)。

1. Support for Spring Cache Abstraction

通过org.springframework.data.redis.cache 程序包,Spring Redis实现了Spring cache abstraction。要使用Redis 作为底层支持的实现,只需简单的添加RedisCacheManager 到你的配置即可:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:c="http://www.springframework.org/schema/c"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

  <!-- turn on declarative caching -->
  <cache:annotation-driven />

  <!-- declare Redis Cache Manager -->
  <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/>
</beans>
默认情况下,RedisCacheManager 会懒初始化RedisCache,只有在需要的时候才会进行初始化。这种方式也可以通过预定义一组缓存名称来改变。

默认情况下,RedisCacheManager 是不支持事务的,可以使用setTransactionAware来设置。

默认情况下,RedisCacheManager 不会加前缀来划分缓存区域,这将会导致ZSET 意想不到的猛增长,ZSET 用来维护已知键的。  强烈建议使用前缀,划分多个缓存区域来避免ZSET 的意外增长和潜在的键冲突,

默认情况下,RedisCache 不会缓存任何null values,因为Redis 会丢弃没有value 的keys。然而,你可以通过RedisCacheManager 明确的指定存储null value,这样会使用org.springframework.cache.support.NullValue 作为占位符存储。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值