SpringMVC整合Redis2.9.0
来源:互联网 发布:数控铣创意图案编程 编辑:程序博客网 时间:2024/06/03 12:48
最近学习使用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>
redis.properties
#IPredis.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-15redis.database=0
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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://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>
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;@Servicepublic class RedisService { //配置文件中注入SpringRedisTemplate @Resource private 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); }}
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 { @Resource private RedisService redisService; @RequestMapping(value="/getRedis") @ResponseBody public String getJson(String string) throws Exception { redisService.set("test", "test"); String value = redisService.get("test"); System.out.println(value); return null; }}
个人使用Redis感觉有以下几点好处:
1、基于内存可高速读取,高并发特性,能抗住大型系统访问的峰值;
2、存取一些过渡数据,设置数据有效时间,超时数据失效;
3、设置分区,可供多个小型系统使用;
4、redis集群实现读写分离。
阅读全文
0 0
- SpringMVC整合Redis2.9.0
- spring5整合spring-data-redis2
- redis2
- redis2
- spring5整合spring-data-redis2做查询缓存
- springmvc整合
- 整合SpringMVC
- springMVC整合
- SpringMVC-整合Spring整合
- springmvc+mybatis整合springmvc
- SpringMVC-15 Spring整合SpringMVC
- SpringMVC和Hibernate整合
- FreeMarker与SpringMVC整合
- SpringMVC整合DWR
- SpringMVC+Spring+Ibatis整合
- SpringMVC+Spring+Hibernate整合
- FreeMarker与SpringMVC整合
- FREEMARKER与SPRINGMVC整合
- 爬虫与反爬虫之间的战争
- Shrio认证详解+自定义Realm
- 两个好用的chrome拓展程序以及开发chrome拓展程序介绍
- Fragment 简介及应用汇总
- 喷水装置(一) 贪心
- SpringMVC整合Redis2.9.0
- Java中eclipse再安装问题
- 机器学习笔记第二课
- Oracle Flashback技术
- 3-5 三角形判断
- 蓝牙入门代码
- 理解最大似然估计
- 网络接口配置——bonding
- Ajax的优缺点 -- 算是面试题吧