詳解Spring Data操作Redis數(shù)據(jù)庫
Redis是一種NOSQL數(shù)據(jù)庫,Key-Value形式對數(shù)據(jù)進行存儲,其中數(shù)據(jù)可以以內(nèi)存形式存在,也可以持久化到文件系統(tǒng)。Spring data對Redis進行了很好的封裝,用起來也是十分的得心應(yīng)手。Redis 是一個開源(BSD許可)的,內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)存儲系統(tǒng),它可以用作數(shù)據(jù)庫、緩存和消息中間件。 它支持多種類型的數(shù)據(jù)結(jié)構(gòu),如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與范圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內(nèi)置了 復(fù)制(replication),LUA腳本(Lua scripting), LRU驅(qū)動事件(LRU eviction),事務(wù)(transactions) 和不同級別的 磁盤持久化(persistence), 并通過 Redis哨兵(Sentinel)和自動 分區(qū)(Cluster)提供高可用性(high availability)。
1. 系統(tǒng)配置,如果使用Maven進行開發(fā),只需要在pom.xml文件中添加如下配置。
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
</dependencies>
為了方面起見可以將Spring Data模板配置成 bean 方便在直接使用的地方直接注入。
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>
2. Redis Template針對不同的需求分類封裝了如下操作。
opsForValue() - Operations for working with entries having simple values opsForList() - Operations for working with entries having list values opsForSet() - Operations for working with entries having set values opsForZSet() - Operations for working with entries having ZSet (sorted set) values opsForHash() - Operations for working with entries having hash values boundValueOps(K) - Operations for working with simple values bound to a given key boundListOps(K) - Operations for working with list values bound to a given key boundSetOps(K) - Operations for working with set values bound to a given key boundZSet(K) - Operations for working with ZSet (sorted set) values bound to a given key boundHashOps(K) - Operations for working with hash values bound to a given key
3. 典型操作示例
3.1 Redis Template注入,可以直接模板注入,也可以以ops形式注入,如下示例中對兩種方式都進行了說明。
public class Example {
// inject the actual template
@Autowired
private RedisTemplate<String, String> template;
// inject the template as ListOperations
// can also inject as Value, Set, ZSet, and HashOperations
@Resource(name="redisTemplate")
private ListOperations<String, String> listOps;
public void addLink(String userId, URL url) {
listOps.leftPush(userId, url.toExternalForm());
// or use template directly
template.boundListOps(userId).leftPush(url.toExternalForm());
}
}
3.2 Bound系列操作示例,Bound系列操作的優(yōu)勢在于只需要綁定一次,然后可以進行一個系列的操作,代碼十分精煉。
BoundListOperations<String, Product> mangoOps = redis.boundListOps("solidmango");
Product popped = mangoOps.rightPop();
mangoOps.rightPush(product1);
mangoOps.rightPush(product2);
mangoOps.rightPush(product3);
3.3 Serializer配置示例,通常情況下Key和Value都采用不同的方式進行持久化,如下示例中Key使用String進行持久化,Value使用Jackson格式進行持久化。
@Bean
public RedisTemplate<String, Cart> redisTemplate(RedisConnectionFactory rcf) {
RedisTemplate<String, Cart> redis =
new RedisTemplate<String, Cart>();
redis.setConnectionFactory(rcf);
redis.setKeySerializer(new StringRedisSerializer());
redis.setValueSerializer(
new Jackson2JsonRedisSerializer<Product>(Product.class));
return redis;
}
總結(jié)
本文對Spring Data操作Redis的配置和開發(fā)方式進行了詳細的分析說明,配置部分給出了具體的配置方式,代碼示例部分分三種情況給出了具體的解決方案,希望對大家有所幫助。
相關(guān)文章
詳解spring cloud hystrix 請求合并collapsing
這篇文章主要介紹了詳解spring cloud hystrix 請求合并collapsing,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
spring源碼學(xué)習之bean的初始化以及循環(huán)引用
這篇文章主要給大家介紹了關(guān)于spring源碼學(xué)習之bean的初始化以及循環(huán)引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-10-10
解決@PathVariable參數(shù)接收不完整的問題
這篇文章主要介紹了解決@PathVariable參數(shù)接收不完整的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringCloud?Gateway讀取Request?Body方式
這篇文章主要介紹了SpringCloud?Gateway讀取Request?Body方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

