1.已搭建好Linux環境的redis或者使用Windows的redis
2.爲了方便這裏使用Win版的,解壓上面下載的壓縮包,點擊redis-server
3.啓動
注意:使用時一定要啓動!!
4.SpringDataRedis簡介
4.1項目常見問題思考
我們目前的系統已經實現了廣告後臺管理和廣告前臺展示,但是對於首頁每天有大量的人訪問,對數據庫造成很大的訪問壓力,甚至是癱瘓。那如何解決呢?我們通常的做法有兩種:一種是數據緩存、一種是網頁靜態化。我們今天討論第一種解決方案。
4.2 Redis
redis是一款開源的Key-Value數據庫,運行在內存中,由ANSI C編寫。企業開發通常採用Redis來實現緩存。同類的產品還有memcache 、memcached 、MongoDB等。
4.3 Jedis
Jedis是Redis官方推出的一款面向Java的客戶端,提供了很多接口供Java語言調用。可以在Redis官網下載,當然還有一些開源愛好者提供的客戶端,如Jredis、SRP等等,推薦使用Jedis。
4.4 Spring Data Redis
Spring-data-redis是spring大家族的一部分,提供了在srping應用中通過簡單的配置訪問redis服務,對reids底層開發包(Jedis, JRedis, and RJC)進行了高度封裝,RedisTemplate提供了redis各種操作、異常處理及序列化,支持發佈訂閱,並對spring 3.1 cache進行了實現。
spring-data-redis針對jedis提供瞭如下功能:
1.連接池自動管理,提供了一個高度封裝的“RedisTemplate”類
2.針對jedis客戶端中大量api進行了歸類封裝,將同一類型操作封裝爲operation接口
ValueOperations:簡單K-V操作
SetOperations:set類型數據操作
ZSetOperations:zset類型數據操作
HashOperations:針對map類型的數據操作
ListOperations:針對list類型的數據操作
4.5 Spring Data Redis入門小Demo
4.5.1準備工作
(1)構建Maven工程 SpringDataRedisDemo
(2)引入Spring相關依賴、引入JUnit依賴
(3)引入Jedis和SpringDataRedis依賴
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>lhz.lx</groupId>
<artifactId>SpringDataRedisDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 集中定義依賴版本號 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.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-webmvc</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-jms</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>4.9</version>
</dependency>
<!-- 緩存 -->
<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>
</dependencies>
</project>
(4)在src/main/resources下創建properties文件夾,建立redis-config.properties
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.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.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:連接時的最大等待毫秒數
testOnBorrow:在提取一個jedis實例時,是否提前進行驗證操作;如果爲true,則得到的jedis實例均是可用的;-->
4.5.2值類型操作
@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("root");
}
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
@Test
public void deleteValue(){
redisTemplate.delete("name");;
}
}
4.5.3 Set類型操作
@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");
}
}
4.5.4 List類型操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**
* 右壓棧:後添加的對象排在後邊
*range(0, 10); 從0開始到第幾個,沒有提供查詢全部的方法
*/
@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("張飛");
}
/**
* 顯示左壓棧集合
*range(0, 10); 從0開始到第幾個,沒有提供查詢全部的方法
*/
@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("namelist2").index(2);
System.out.println(s);
}
//結果
//關羽
/**
* 移除集合某個元素
*/
@Test
public void testRemoveByIndex(){
//第一個參數代表移除個數
redisTemplate.boundListOps("namelist2").remove(1, "關羽");
}
//結果
//[張飛, 劉備]
}
4.5.5 Hash類型操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
//(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", "沙僧");
}
//(2)提取所有的KEY
@Test
public void testGetKeys(){
Set s = redisTemplate.boundHashOps("namehash").keys();
System.out.println(s);
}
//運行結果:
//[a, b, c, d]
//(3)提取所有的值
@Test
public void testGetValues(){
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}
//運行結果:
//[唐僧, 悟空, 八戒, 沙僧]
//(4)根據KEY提取值
@Test
public void testGetValueByKey(){
Object object = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(object);
}
//運行結果:
//悟空
//(5)根據KEY移除值
@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}
//運行後再次查看集合內容:
//[唐僧, 悟空, 沙僧]
}
Demo下載:https://download.csdn.net/download/zhuocailing3390/10785961