- 1.1、Spring Data Redis 1.8 新特性
- 1.2、Spring Data Redis 1.7 新特性
- 1.3、Spring Data Redis 1.6 新特性
- 1.4、Spring Data Redis 1.5 新特性
- 5.1、Redis要求
- 5.2、Redis支持高級視圖
- 5.3、連接到Redis
- 5.4、Redis Sentinel支持
- 5.5、使用RedisTemplate操作Objects
- 5.6、聚焦String的便捷類
- 5.7、序列化器 Serializers
- 5.8、Hash映射
- 5.9、Redis 消息/發布訂閱
- 5.10、Redis事務
- 5.11、Pipelining 管道
- 5.12、Redis 腳本
- 5.13、支持類
- 7.1、使用
- 7.2、Object to Hash Mapping
- 7.3、Keyspaces
- 7.4、Secondary Indexes
- 7.5、Time To Live 存活時間
- 7.6、持久化參考
- 7.7、Persisting Partial Updates
- 7.8、查詢和查詢方法
- 7.9、運行在Cluster上的Redis Repositories
- 7.10、CDI集成
前言
1、新功能
1.1、Spring Data Redis 1.8 新特性
- Jedis升級到2.9。
- Lettuce升級到4.2。(注意,Lettuce 4.2要求Java8)。
- 支持Redis GEO 命令。
- 使用Spring Data Repository抽象來支持Geo索引。
- 基於HashMapper實現的MappingRedisConverter。
- 在repository支持中支持PartialUpdate。
- 對於連接到Redis cluster的SSL支持。
- 當使用Jedis時,支持通過ConnectionFactory來設置client name。
1.2、Spring Data Redis 1.7 新特性
- 支持RedisCluster。
- 支持Spring Data Repository抽象。
1.3、Spring Data Redis 1.6 新特性
- Lettuce Redis驅動,由wg/lettuce切換到mp911de/lettuce。
- 支持ZRANGEBYLEX.
- 增強了ZSET的range操作,包括 +inf 、 -inf。
- RedisCache的性能改進,更早釋放連接。
- Generic Jackson2 RedisSerializer,利用了Jackson的多態反序列化
1.4、Spring Data Redis 1.5 新特性
- 添加對Redis HyperLogLog命令的支持:PFADD、PFCOUNT、PFMERGE。
- 可配置的JavaType查找,用於基於RedisSerializers的Jackson。
- 基於PropertySource的配置,用於連接到Redis Sentinel。
介紹
2、為什么選擇Spring Data Redis?
3、要求
- SDR 1.x要求JDK 6.0及以上,要求Spring框架4.3.9.RELEASE及以上。
- Redis 2.6.x及以上。
4、開始
4.1、第一步
雖然本文檔的每一部分都提供了相關資源的連接,但最好還是提前熟悉下。
Spring Data嚴重依賴Spring框架的核心功能,例如IoC容器、資源抽象、或者AOP。重要的不是掌握Spring的APIs,而是理解它們背后的概念。至少,應該熟悉IoC。簡單的說,你對Spring了解的越多,越容易上手SDR。
4.1.2、了解NoSQL和鍵值存儲
4.1.3、嘗試案例
4.2、需要幫助?
4.2.1、社區幫助
4.2.2、專業幫助
4.3、跟隨開發
參考文檔
5、Redis支持
5.1、Redis要求
5.2、Redis支持高級視圖
5.3、連接到Redis
5.3.1、RedisConnection 和 RedisConnectionFactory
活動的RedicConnection由RedisConnectionFactory創建。另外,該工廠還扮演了PersistenceExceptionTranslator,就是說,一旦聲明了,它們會允許用戶進行透明的異常翻譯。例如,通過使用@Repository和AOP的異常翻譯。更多信息,見Spring框架的相關部分。
注意:依賴於底層的配置,工廠會返回一個新的連接 或者 一個現有的連接(使用pool或者shared native connection時)。
使用RedisConnectionFactory最簡單的方式,是通過IoC容器配置相應的connector,並將其注入使用類中。
重要:不幸的是,目前,不是所有的connector都支持所有的Redis功能。當調用底層庫不支持的API時,會拋出UnsupportedOperationException。這種情況在將來可能被解決,視不同的connector的成熟情況。
5.3.2、配置Jedis connector
<?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 connector(自1.7起廢棄)
5.3.4、配置SRP connector(自1.7起廢棄)
5.3.5、配置Lettuce connector
<?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:hostname="server" p:port="6379"/> </beans>
注意:LettuceConnectionFactory 也可以為pooling blocking和事務連接配置一個LettucePool,或者,為所有連接配置一個LettucePool -- 如果shareNativeConnection設為false的話。
5.4、Redis Sentinel支持
/** * jedis */ @Bean public RedisConnectionFactory jedisConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration().master("mymaster").sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380); return new JedisConnectionFactory(sentinelConfig); } /** * lettuce */ @Bean public RedisConnectionFactory lettuceConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration().master("mymaster").sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380); return new LettuceConnectionFactory(sentinelConfig); }
- spring.redis.sentinel.master: master節點的名字
- spring.redis.sentinel.nodes: 以逗號間隔的host:port列表
5.5、使用RedisTemplate操作Objects
Interface | Description |
---|---|
Key Type Operations |
|
ValueOperations |
Redis string (or value) operations |
ListOperations |
Redis list operations |
SetOperations |
Redis set operations |
ZSetOperations |
Redis zset (or sorted set) operations |
HashOperations |
Redis hash operations |
HyperLogLogOperations |
Redis HyperLogLog operations like (pfadd, pfcount,…) |
GeoOperations |
Redis geospatial operations like |
Key Bound Operations |
|
BoundValueOperations |
Redis string (or value) key bound operations |
BoundListOperations |
Redis list key bound operations |
BoundSetOperations |
Redis set key bound operations |
BoundZSetOperations |
Redis zset (or sorted set) key bound operations |
BoundHashOperations |
Redis hash key bound operations |
BoundGeoOperations |
Redis key bound geospatial operations. |
<?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 { // inject the actual template @Autowired private RedisTemplate<String, String> template; // inject the template as ListOperations -- 自動轉換 @Resource(name="redisTemplate") private ListOperations<String, String> listOps; public void addLink(String userId, URL url) { listOps.leftPush(userId, url.toExternalForm()); } }
5.6、聚焦String的便捷類
<?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"/> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/> ... </beans>
public class Example { @Autowired private StringRedisTemplate redisTemplate; public void addLink(String userId, URL url) { redisTemplate.opsForList().leftPush(userId, url.toExternalForm()); } }
public void useCallback() { redisTemplate.execute(new RedisCallback<Object>() { public Object doInRedis(RedisConnection connection) throws DataAccessException { Long size = connection.dbSize(); // Can cast to StringRedisConnection if using a StringRedisTemplate ((StringRedisConnection)connection).set("key", "value"); } }); }
5.7、序列化器 Serializers
5.8、Hash映射
- 使用HashOperations和一個序列化器,直接映射。
- 使用Redis Repositories。
- 使用HashMapper和HashOperations。
5.8.1、Hash mappers 哈希映射器
- BeanUtilsHashMapper,使用Spring的BeanUtils。
- ObjectHashMapper,使用Object to Hash Mapping。
- Jackson2HashMapper,使用FasterXML Jackson。
public class Person { String firstname; String lastname; // … } public class HashMapping { @Autowired HashOperations<String, byte[], byte[]> hashOperations; HashMapper<Object, byte[], byte[]> mapper = new ObjectHashMapper(); public void writeHash(String key, Person person) { Map<byte[], byte[]> mappedHash = mapper.toHash(person); hashOperations.putAll(key, mappedHash); } public Person loadHash(String key) { Map<byte[], byte[]> loadedHash = hashOperations.entries("key"); return (Person) mapper.fromHash(loadedHash); } }
5.8.2、Jackson2HashMapper
public class Person { String firstname; String lastname; Address address; } public class Address { String city; String country; }
Hash Field | Value |
---|---|
firstname |
|
lastname |
|
address |
|
Hash Field | Value |
---|---|
firstname |
|
lastname |
|
address.city |
|
address.country |
|
5.9、Redis 消息/發布訂閱
5.10、Redis事務
5.10.1、@Transactional 支持5.11、Pipelining 管道
5.12、Redis 腳本
5.13、支持類
5.13.1、對於Spring Cache抽象的支持6、Redis Cluster
6.1、啟用Redis Cluster
6.2、Redis Cluster連接
6.3、使用RedisTemplate 和 ClusterOperations