快取解決方案—Redis
阿新 • • 發佈:2018-11-24
一、Redis,Jedis,Spring Data Redis
1.1 Redis
redis是一款開源的Key-value資料庫,執行在記憶體中,由ANSIC編寫。企業開發通常使用Redis來實現快取。同類產品還有memcache、memcached、MongoDB等。
1.2 Jedis
Jedis是Redis官方退出的一款面向Java的客戶端,提供了很多借口供Java語言呼叫。可以在Redis官網下載。還有一些開源愛好者提供的客戶端,如Jredis、SRP等等。
1.3 Spring Data Redis
Spring Data Redis是spring家族的而一部分,提供了在spring應用中通過簡單的配置訪問Redis服務,對Redis底層開發包(Jedis、Jredis、RJC)進行了高度封裝,RedisTeamplate提供了redis各種操作,異常處理及序列化,支援釋出訂閱,並對spring3.1cache進行了實現。
spring-data-redis針對Jedis提供瞭如下功能:
1.連線池自動管理,提供了一個高度封裝的RedisTemplate類
2.針對Jedis客戶端中大量api進行了歸類封裝,將同一型別操作封裝為operation介面
ValueOperation:簡單K-V操作
SetOperation:set型別資料操作
ZSetOperation:zset型別資料操作
HashOperation:針對map型別資料操作
ListOperation:針對list型別的資料操作
二、Spring Data Redis入門小Demo
2.1 準備工作
- 構建Maven工程,SpringDataRedisDemo
- 引入Spring、JUnit相關座標依賴
- 引入Jedis和SpringDataRedis依賴
1 <!-- spring與Junit相關依賴 --> 2 略 3 4 <!-- 快取 --> 5 <dependency> 6 <groupId>redis.clients</groupId> 7 <artifactId>jedis</artifactId> 8 <version>2.8.1</version> 9 </dependency> 10 11 <dependency> 12 <groupId>org.springframework.data</groupId> 13 <artifactId>spring-data-redis</artifactId> 14 <version>1.7.2.RELEASE</version> 15 </dependency>
- 建立redis-config.properties檔案
1 redis.host=127.0.0.1 2 3 redis.port=6379 4 5 redis.password= // 密碼,這裡沒有設定 6 7 redis.database=0 8 9 redis.maxIdle=300 // 最大空閒數 10 11 redis.maxWait=3000 // 連線時的最大等待毫秒數 12 13 redis.testOnBorrow=true // 在提取一個Jedis例項時,是否提前進行驗證操作;true代表是得到的Jedis例項均是可用的
- 建立applicationContext-redis.xml檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd 13 http://www.springframework.org/schema/cache 14 http://www.springframework.org/schema/cache/spring-cache.xsd"> 15 16 <context:property-placeholder location="classpath*:properties/*.properties" /> 17 18 <!-- redis 相關配置 --> 19 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 20 <property name="maxIdle" value="${redis.maxIdle}" /> 21 <property name="maxWaitMillis" value="${redis.maxWait}" /> 22 <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 23 </bean> 24 25 26 <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 27 p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> 28 29 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 30 <property name="connectionFactory" ref="JedisConnectionFactory" /> 31 </bean> 32 33 </beans>
2.2 RedisTemplate相關操作
1.值型別操作
1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") 3 public class TestValue { 4 5 @Autowired 6 private RedisTemplate redisTemplate; 7 8 @Test 9 public void setValue(){ 10 redisTemplate.boundValueOps("name").set("buwei"); 11 } 12 13 @Test 14 public void getValue(){ 15 String str = (String) redisTemplate.boundValueOps("name").get(); 16 System.out.println(str); 17 } 18 19 @Test 20 public void deleteValue(){ 21 redisTemplate.delete("name");; 22 } 23 }
2.Set型別操作
1 /** 2 * 存值 3 */ 4 @Test 5 public void setValue(){ 6 redisTemplate.boundSetOps("nameset").add("曹操"); 7 redisTemplate.boundSetOps("nameset").add("劉備"); 8 redisTemplate.boundSetOps("nameset").add("孫權"); 9 } 10 11 /** 12 * 取值 13 */ 14 @Test 15 public void getValue(){ 16 Set members = redisTemplate.boundSetOps("nameset").members(); 17 System.out.println(members); 18 } 19 20 /** 21 * 刪除集合中的某一個值 22 */ 23 @Test 24 public void deleteValue(){ 25 redisTemplate.boundSetOps("nameset").remove("孫權"); 26 } 27 28 /** 29 * 刪除整個集合 30 */ 31 @Test 32 public void deleteAllValue(){ 33 redisTemplate.delete("nameset"); 34 }
3.List型別操作
- 右壓棧
1 /** 2 * 右壓棧:後新增的物件排在後邊 3 */ 4 @Test 5 public void testSetValue1(){ 6 redisTemplate.boundListOps("namelist1").rightPush("劉備"); 7 redisTemplate.boundListOps("namelist1").rightPush("關羽"); 8 redisTemplate.boundListOps("namelist1").rightPush("張飛"); 9 } 10 11 /** 12 * 顯示右壓棧集合 13 */ 14 @Test 15 public void testGetValue1(){ 16 List list = redisTemplate.boundListOps("namelist1").range(0, 10); 17 System.out.println(list); 18 }
- 左壓棧
1 /** 2 * 左壓棧:後新增的物件排在前邊 3 */ 4 @Test 5 public void testSetValue2(){ 6 redisTemplate.boundListOps("namelist2").leftPush("劉備"); 7 redisTemplate.boundListOps("namelist2").leftPush("關羽"); 8 redisTemplate.boundListOps("namelist2").leftPush("張飛"); 9 } 10 11 /** 12 * 顯示左壓棧集合 13 */ 14 @Test 15 public void testGetValue2(){ 16 List list = redisTemplate.boundListOps("namelist2").range(0, 10); 17 System.out.println(list); 18 }
- 根據索引查詢元素
1 /** 2 * 查詢集合中的元素 3 */ 4 @Test 5 public void testSearchByIndex(){ 6 String s = (String) redisTemplate.boundListOps("namelist1").index(1); 7 System.out.println(s); 8 }
- 移除元素
1 /** 2 * 移除集合某個元素 3 */ 4 @Test 5 public void testRemoveByIndex(){ 6 redisTemplate.boundListOps("namelist1").remove(1, "關羽"); 7 }
4.Hash型別操作
- 存值
1 @Test 2 public void testSetValue(){ 3 redisTemplate.boundHashOps("namehash").put("a", "唐僧"); 4 redisTemplate.boundHashOps("namehash").put("b", "悟空"); 5 redisTemplate.boundHashOps("namehash").put("c", "八戒"); 6 redisTemplate.boundHashOps("namehash").put("d", "沙僧"); 7 }
- 提取所有的KEY
1 @Test 2 public void testGetKeys(){ 3 Set s = redisTemplate.boundHashOps("namehash").keys(); 4 System.out.println(s); 5 }
- 提取所有的值
1 @Test 2 public void testGetValues(){ 3 List values = redisTemplate.boundHashOps("namehash").values(); 4 System.out.println(values); 5 }
- 根據KEY提取值
1 @Test 2 public void testGetValueByKey(){ 3 Object object = redisTemplate.boundHashOps("namehash").get("b"); 4 System.out.println(object); 5 }
- 根據kEY移除值
1 @Test 2 public void testRemoveValueByKey(){ 3 redisTemplate.boundHashOps("namehash").delete("c"); 4 }
當我們的業務中有需要用到redis的時候我們就可以考慮使用Spring Data Redis來實現我們的需求啦!:)