約定>配置>代碼redis
在pom.xml中導入依賴(redis和jedis以及其餘所需的依賴) > 配置相關配置文件(redis-config.properties 和applicationContext-redis.xml) > 進行代碼的測試spring
新建一臺虛擬機安裝Redis環境(具體安裝步驟參見資源文件(資源\Redis\Redis安裝配置.docx))緩存
(1)構建Maven工程 SpringDataRedisDemoapp
(2)引入Spring相關依賴、引入JUnit依賴 測試
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
(3)引入Jedis和SpringDataRedis依賴spa
<!-- 緩存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
(4)在src/main/resources下建立properties文件夾,創建redis-config.properties code
redis.host=127.0.0.1 redis.port=6379 redis.pass= redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true
(5)在src/main/resources下建立spring文件夾 ,建立applicationContext-redis.xmlxml
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<!-- redis 相關配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
maxIdle :最大空閒數對象
maxWaitMillis:鏈接時的最大等待毫秒數blog
testOnBorrow:在提取一個jedis實例時,是否提早進行驗證操做;若是爲true,則獲得的jedis實例均是可用的;
不要在測試類上添加RunWith和ContextConfiguration註解
@ContextConfiguration Spring整合JUnit4測試時,使用註解引入多個配置文件,多個之間用逗號隔開
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestValue { @Autowired private RedisTemplate redisTemplate;
//添加值 @Test public void setValue(){ redisTemplate.boundValueOps("name").set("youjiuye"); }
//取值 @Test public void getValue(){ String str = (String) redisTemplate.boundValueOps("name").get(); System.out.println(str); }
//刪除值 @Test public void deleteValue(){ redisTemplate.delete("name");; } }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestSet { @Autowired private RedisTemplate redisTemplate; //存入值 @Test public void setValue(){ redisTemplate.boundSetOps("nameset").add("曹操"); redisTemplate.boundSetOps("nameset").add("劉備"); redisTemplate.boundSetOps("nameset").add("孫權"); } //提取值 @Test public void getValue(){ Set members = redisTemplate.boundSetOps("nameset").members(); System.out.println(members); } //刪除集合中的某一個值 @Test public void deleteValue(){ redisTemplate.boundSetOps("nameset").remove("孫權"); } //刪除整個集合 @Test public void deleteAllValue(){ redisTemplate.delete("nameset"); } }
/** * 右壓棧:後添加的對象排在後邊 */ @Test public void testSetValue1(){ redisTemplate.boundListOps("namelist1").rightPush("劉備"); redisTemplate.boundListOps("namelist1").rightPush("關羽"); redisTemplate.boundListOps("namelist1").rightPush("張飛"); } /** * 顯示右壓棧集合 */ @Test public void testGetValue1(){ List list = redisTemplate.boundListOps("namelist1").range(0, 10); System.out.println(list); }
/** * 左壓棧:後添加的對象排在前邊 */ @Test public void testSetValue2(){ redisTemplate.boundListOps("namelist2").leftPush("劉備"); redisTemplate.boundListOps("namelist2").leftPush("關羽"); redisTemplate.boundListOps("namelist2").leftPush("張飛"); } /** * 顯示左壓棧集合 */ @Test public void testGetValue2(){ List list = redisTemplate.boundListOps("namelist2").range(0, 10); System.out.println(list); }
/** * 查詢集合某個元素 */ @Test public void testSearchByIndex(){ String s = (String) redisTemplate.boundListOps("namelist1").index(1); System.out.println(s); }
/** * 移除集合某個元素 */ @Test public void testRemoveByIndex(){ redisTemplate.boundListOps("namelist1").remove(1, "關羽"); }
存值
@Test public void testSetValue(){ redisTemplate.boundHashOps("namehash").put("a", "唐僧"); redisTemplate.boundHashOps("namehash").put("b", "悟空"); redisTemplate.boundHashOps("namehash").put("c", "八戒"); redisTemplate.boundHashOps("namehash").put("d", "沙僧"); }
提取全部的key
@Test public void testGetKeys(){ Set s = redisTemplate.boundHashOps("namehash").keys(); System.out.println(s); }
提取全部的值
@Test public void testGetValues(){ List values = redisTemplate.boundHashOps("namehash").values(); System.out.println(values); }
根據key提取值
@Test public void testGetValues(){ List values = redisTemplate.boundHashOps("namehash").values(); System.out.println(values); }
根據key移除值
@Test public void testRemoveValueByKey(){ redisTemplate.boundHashOps("namehash").delete("c"); }