日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC整合Redis2.9.0

發布時間:2025/3/16 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC整合Redis2.9.0 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近學習使用Spring + Redis存取數據,版本是2.9.0。String,Map都可存入redis,并設置時效性。

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 #超時,單位毫秒 redis.timeout=10000 #最大空閑數 redis.maxIdle=300 #最大連接數 redis.maxTotal=600 #最大空閑數 redis.minIdle=1 #最大建立連接等待時間 redis.maxWait=1000 #指明是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接并嘗試取出另一個 redis.testOnBorrow=false #使用redis塊分區,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,讀取時再進行反序列化 --><!-- 對key的默認序列化器 --><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><!-- 對value的默認序列化器 --><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><!-- 對hash結構數據的hashkey的默認序列化器 --><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property><!-- 對hash結構數據的hashvalue的默認序列化器 --><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);}/*** 根據key獲取value*/public String get(String key){String value = redisTemplate.opsForValue().get(key);return value;}/*** 將key和value存入redis,并設置有效時間,單位:天*/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,并設置時效*/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

個人使用Redis感覺有以下幾點好處:?
1、基于內存可高速讀取,高并發特性,能抗住大型系統訪問的峰值;?
2、存取一些過渡數據,設置數據有效時間,超時數據失效;?
3、設置分區,可供多個小型系統使用;?
4、redis集群實現讀寫分離。

總結

以上是生活随笔為你收集整理的SpringMVC整合Redis2.9.0的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。