在上一篇学习了操作mysql数据库,今天在上一篇的基础上加入redis缓存
1.pom文件添加redis的依赖
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.application.properties文件添加redis服务器的配置
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.redis.host=127.0.0.1
spring.redis.port=7001
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=60000
3.redis服务器搭建,这里还是使用docker,
a.docker pull redis 下载redis官方镜像
b.docker run -d -p7001:6379 --name redis redis 运行容器,映射到本机的7001端口
c.docker exec -it redis /bin/sh 进入容器,使用redis-cli命令测试,
4.使用RedisTemplate,编写个userRedisDao.java类
/**
* Copyright EXPRESS PAY 2017, Inc. All rights reserved.
*/
/**
*
*/
package lujia.springboot.hello.reids;
import javax.annotation.Resource;
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.Repository;
import lujia.springboot.hello.User;
/**
* <p>
* TODO。
* </p>
*
* @author lujia
* @version 1.0
* @date 2017年7月13日
*/
@Repository
public class UserRedisDao {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Resource(name="redisTemplate")
private ValueOperations<Object, Object> valueOperations;
public void save(User user){
valueOperations.set(user.getId(), user);
}
public User get(long id){
User object = (User) valueOperations.get(id);
return object;
}
}
5.修改userController.java类,在查询的时候使用redis缓存
/**
* Copyright EXPRESS PAY 2017, Inc. All rights reserved.
*/
/**
*
*/
package lujia.springboot.hello;
import org.hibernate.sql.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import lujia.springboot.hello.reids.UserRedisDao;
/**
* <p>
* TODO。
* </p>
*
* @author lujia
* @version 1.0
* @date 2017年7月12日
*/
@Controller
@RequestMapping("/user")
public class UserController {
private Logger longer=LoggerFactory.getLogger(UserController.class);
@Autowired
private UserRepository userRepository;
@Autowired
private UserRedisDao userRedisDao;
@RequestMapping("/add")
@ResponseBody
public String addUser(@RequestParam String name,@RequestParam String sex){
longer.debug("添加user");
longer.info("add user");
User user=new User();
user.setName(name);
user.setSex(sex);
userRepository.save(user);
long id = user.getId();
return "add user id="+id;
}
@RequestMapping("/getUserById")
public @ResponseBody User getUserById(long id){
User user = userRedisDao.get(id);
if(user==null){
longer.info("getUserById 查询数据库");
user=userRepository.findOne(id);
if(user!=null){
userRedisDao.save(user);
}
}else {
longer.info("getUserById 使用redis缓存");
}
return user;
}
public void deltete(long id){
userRepository.delete(id);
}
@RequestMapping("/getAllUsers")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}
6.测试,可以看到第一次查询会保存到redis,第二次查询就会直接读取redis里面的value