SpringMVC项目中如何使用redis缓存技术
1:电脑上必须安装过Redis,然后启动它。
C:\Users\Administrator>e:
E:\>cd redsi
系统找不到指定的路径。
E:\>cd redis
E:\redis>redis-server.exe redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1104
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[1104] 27 Sep 11:14:52.263 # Server started, Redis version 2.8.9
[1104] 27 Sep 11:14:52.310 * DB loaded from disk: 0.047 seconds
[1104] 27 Sep 11:14:52.310 * The server is now ready to accept connections on port 6379
2:配置Redis的jar包和Spring整合redis的jar包。jar包如下。建议使用下面两个版本的jar包。因为其他版本可能会有版本冲突,导致一系列不能使用问题。
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
3:配置redis.properties以及在Spring-bean.xml中做相关配置
redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=""
redis.maxIdle=50
redis.maxTotal=100
redis.maxWaitMillis=1000
redis.blockWhenExhausted=true
redis.testOnBorrow=true
redis.timeout=10000
defaultCacheExpireTime=60
spring-bean.xml,如果在本文件中,已经通过<context:property-placeholder location="redis.properties"/>这种方式导入过其他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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 导入spring-redis配置文件 -->
<context:property-placeholder location="redis.properties"/>
<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<!-- 连接超时时是否阻塞,false时报异常,true阻塞到直到超时,默认true -->
<property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
<!-- 返回连接时,检测连接是否成功 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- Spring整合Jedis -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 指定连接地址 -->
<property name="hostName" value="127.0.0.1"/>
<!-- 指定端口号 -->
<property name="port" value="6379"/>
<!-- 自定义连接池的配置 -->
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- RedisTempLate -->
<bean class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
4:使用redis缓存
package com.yc.controller;
import com.yc.po.Hotel;
import com.yc.zip.impl.HotelBizImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author Administrator
*/
@Controller
@RequestMapping("/hotel")
public class HotelController {
@Autowired
private HotelBizImpl biz;
/**
* 自动装配RedisTemplate
*/
@Autowired
RedisTemplate redisTemplate = new RedisTemplate();
@RequestMapping("/finds")
@ResponseBody
//查询前最后6条酒店信息
public List<Hotel> finds() {
return biz.finds();
}
@RequestMapping("/findAll")
@ResponseBody
//查看所有酒店信息
public List<Hotel> findAll() {
return biz.findAll();
}
@RequestMapping("/findByStar")
@ResponseBody
//根据星级查询酒店信息
public List<Hotel> findByStart(String start) {
int star = Integer.parseInt(start);
//获取模板中的缓存数据
ValueOperations<Integer,List> string = redisTemplate.opsForValue();
//判断缓存中是否有对应的键,若果有就从redis中查询,没有就从数据库中查询,然后设置到redis中
if (redisTemplate.hasKey(star)){
System.out.println("从redis中查询");
return string.get(star);
}else {
System.out.println("从数据库中查询");
string.set(star,biz.findByStart(start));
return biz.findByStart(start);
}
}
}