Spring Data操做Redis詳解redis
Redis是一種NOSQL數據庫,Key-Value形式對數據進行存儲,其中數據能夠之內存形式存在,也能夠持久化到文件系統。Spring data對Redis進行了很好的封裝,用起來也是十分的駕輕就熟。Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它能夠用做數據庫、緩存和消息中間件。 它支持多種類型的數據結構,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與範圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內置了 複製(replication),LUA腳本(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不一樣級別的 磁盤持久化(persistence), 並經過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)。spring
1. 系統配置,若是使用Maven進行開發,只須要在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. 典型操做示例url
3.1 Redis Template注入,能夠直接模板注入,也能夠以ops形式注入,以下示例中對兩種方式都進行了說明。spa
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系列操做的優點在於只須要綁定一次,而後能夠進行一個系列的操做,代碼十分精煉。code
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格式進行持久化。orm
@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; }
總結
本文對Spring Data操做Redis的配置和開發方式進行了詳細的分析說明,配置部分給出了具體的配置方式,代碼示例部分分三種狀況給出了具體的解決方案,但願對你們有所幫助。xml