當(dāng)前位置:
首頁 >
SpringMVC整合Redis2.9.0
發(fā)布時(shí)間:2025/3/16
27
豆豆
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC整合Redis2.9.0
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近學(xué)習(xí)使用Spring + Redis存取數(shù)據(jù),版本是2.9.0。String,Map都可存入redis,并設(shè)置時(shí)效性。
pom.xml
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-instrument</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.2.5.RELEASE</version> </dependency> <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version> </dependency> <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.6.1.RELEASE</version> </dependency>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
redis.properties
#IP redis.host=127.0.0.1 #端口 redis.port=6379 #密碼 redis.password=0 #超時(shí),單位毫秒 redis.timeout=10000 #最大空閑數(shù) redis.maxIdle=300 #最大連接數(shù) redis.maxTotal=600 #最大空閑數(shù) redis.minIdle=1 #最大建立連接等待時(shí)間 redis.maxWait=1000 #指明是否在從池中取出連接前進(jìn)行檢驗(yàn),如果檢驗(yàn)失敗,則從池中去除連接并嘗試取出另一個(gè) redis.testOnBorrow=false #使用redis塊分區(qū),0-15 redis.database=0- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
Spring-redis.properties
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mongo="http://www.springframework.org/schema/data/mongo"xmlns:util="http://www.springframework.org/schema/util" xmlns:redis="http://www.springframework.org/schema/redis"xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"><description>Redis連接配置文件</description><context:annotation-config /><!-- 注解掃描包 --> <context:component-scan base-package="com.nuanshui" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 引入配置文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="fileEncoding" value="utf-8" /><property name="locations"><list><value>classpath*:/config/properties/*.properties</value></list></property></bean><!-- jedis 配置 --><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="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="poolConfig" ref="poolConfig" /><property name="port" value="${redis.port}" /><property name="hostName" value="${redis.host}" /><property name="password" value="${redis.password}" /><property name="timeout" value="${redis.timeout}" /><property name="database" value="${redis.database}" /></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="redisConnectionFactory" /><!-- 將key和value序列化后存入redis,讀取時(shí)再進(jìn)行反序列化 --><!-- 對(duì)key的默認(rèn)序列化器 --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><!-- 對(duì)value的默認(rèn)序列化器 --><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><!-- 對(duì)hash結(jié)構(gòu)數(shù)據(jù)的hashkey的默認(rèn)序列化器 --><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property><!-- 對(duì)hash結(jié)構(gòu)數(shù)據(jù)的hashvalue的默認(rèn)序列化器 --><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property></bean></beans>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
RedisService.java
package com.test.service.redis;import java.util.Map; import java.util.concurrent.TimeUnit;import javax.annotation.Resource;import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service;@Service public class RedisService {//配置文件中注入SpringRedisTemplate@Resourceprivate StringRedisTemplate redisTemplate;/*** 刪除key和value*/public void delete(String key){redisTemplate.delete(key);}/*** 根據(jù)key獲取value*/public String get(String key){String value = redisTemplate.opsForValue().get(key);return value;}/*** 將key和value存入redis,并設(shè)置有效時(shí)間,單位:天*/public void set(String key, String value, long timeout){redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.DAYS);}/*** 將key和value存入redis*/public void set(String key, String value){redisTemplate.opsForValue().set(key, value);}/*** 從redis中獲取map*/public Map<String, Object> getMap(String key){HashOperations<String, String, Object> hash = redisTemplate.opsForHash();Map<String,Object> map = hash.entries(key);return map;}/*** 將map存入redis,并設(shè)置時(shí)效*/public void set(String key, Map<? extends String, ? extends Object> map, long timeout){redisTemplate.opsForHash().putAll(key, map);redisTemplate.expire(key, timeout, TimeUnit.DAYS);} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
Controller
package com.test.web;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import com.nuanshui.service.redis.RedisService;import redis.clients.jedis.Jedis;@Controller @RequestMapping("/test") public class HelloController {@Resourceprivate RedisService redisService;@RequestMapping(value="/getRedis")@ResponseBodypublic String getJson(String string) throws Exception {redisService.set("test", "test");String value = redisService.get("test");System.out.println(value);return null;}}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
個(gè)人使用Redis感覺有以下幾點(diǎn)好處:?
1、基于內(nèi)存可高速讀取,高并發(fā)特性,能抗住大型系統(tǒng)訪問的峰值;?
2、存取一些過渡數(shù)據(jù),設(shè)置數(shù)據(jù)有效時(shí)間,超時(shí)數(shù)據(jù)失效;?
3、設(shè)置分區(qū),可供多個(gè)小型系統(tǒng)使用;?
4、redis集群實(shí)現(xiàn)讀寫分離。
總結(jié)
以上是生活随笔為你收集整理的SpringMVC整合Redis2.9.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat 软连接问题
- 下一篇: 微信分享JS-SDK示例页面