项目中用到redis,redis缓存是其中一个很重要的使用场景,如果读写数据的时候,读取数据的时候可直接走内存的,这样的高并发访问数据的时候,和查询数据库想比,redis读取数据的高效性、快速的优势。
1.搭建redis环境
以下地址包括下载搭建和使用
redis搭建
2.安装mysql
想必做java后台开发的这个并不陌生,用数据工具,我用的是Navicat,我的文章中有相关的下载破解方法可以参考(Mac上Navicat破解与使用)
在本地连接的数据库创建一个表,这里用的是User
3.准备工作,搭建工程
pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
application.properties配置文件中添加redis配置
#Redis
spring.redis.database=1
#Redis服务器地址,可根据环境不同进行配置,此处是本机的
spring.redis.host=127.0.0.1
#Redis服务器连接端口,可根据自己服务器地址配置相应端口,此处是本机的
spring.redis.port=6379
#Redis服务器连接密码
spring.redis.password=123456
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=
spring.redis.jedis.pool.max-idle=4
spring.redis.jedis.pool.min-idle=2
spring.redis.timeout=1200
接下来User和数据库中的user表对应,注意这里一定要序列化,因为redis保存对象的时候要求对象是序列化的;
User实体:
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "name")
private String name;
@ApiModelProperty(value = "password")
private String password;
}
Service层:
具体是实现方法代码如下:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
public User find(int id){
String key = "user_"+id;
ValueOperations<String,User>operations = redisTemplate.opsForValue();
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey){
User user = operations.get(key);
System.out.println("从缓存中获取的===="+ user.getName());
return user;
}else {
User user = userMapper.find(id);
System.out.println("从数据表中获取的===="+ user.getName());
operations.set(key, user, 5, TimeUnit.HOURS);
return user;
}
}
public List<User>queryAll(){
return userMapper.queryAll();
}
public int updateUser(User user){
ValueOperations<String,User>operations = redisTemplate.opsForValue();
int result = userMapper.updateUser(user);
if(result != 0){
String key = "user"+user.getId();
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey){
redisTemplate.delete(key);
System.out.println("删除缓存中的数据");
}
//再更新新的数据到缓存中
User userNew = userMapper.find(user.getId());
if (userNew != null){
operations.set(key,userNew,3,TimeUnit.HOURS);
}
}
return result;
}
public int deleteUser(int id){
int result = userMapper.deleteUser(id);
String key = "user"+ id;
if(result != 0){
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey){
redisTemplate.delete(key);
System.out.println("删除缓存中的");
}
}
return result;
}
}
Controller层:
@RestController
public class UserController {
@Autowired
UserService userService;
@ApiOperation(value = "查找",nickname = "findUser")
@RequestMapping(value = "/users/find",method = RequestMethod.POST)
public User find(@RequestBody User user){
return userService.find(user.getId());
}
@ApiOperation(value = "查找所有",nickname = "queryAll")
@RequestMapping(value = "/users/queryAll",method = RequestMethod.POST)
public List<User> queryAll(){
return userService.queryAll();
}
@ApiOperation(value = "更新某个用户",nickname = "updateUser")
@RequestMapping(value = "/users/updateUser",method = RequestMethod.POST)
public int updateUser(User user){
return userService.updateUser(user);
}
@ApiOperation(value = "删除某个用户",nickname = "deleteUser")
@RequestMapping(value = "/users/deleteUser",method = RequestMethod.POST)
public int deleteUser(int id){
return userService.deleteUser(id);
}
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.redisdemo.demo.mapper.UserMapper">
<!--id对应接口中的方法,名字要一样,parameterType是方法的参数类型,
resultType是查询返回的类型,需要注意的是,这里的SQL语句后面不能加分号,变量不能加引号-->
<resultMap id="BaseResultMap" type="com.redisdemo.demo.entity.User" >
<result column="id" property="id" />
<result column="name" property="name" />
<result column="password" property="password" />
</resultMap>
<select id="find" resultMap="BaseResultMap">
select * from user where id = #{id}
</select>
<select id="queryAll" resultMap="BaseResultMap">
select * from user
</select>
<update id="updateUser" parameterType="com.redisdemo.demo.entity.User">
update user set name = #{user.name},password = #{user.password} where id = #{user.id}
</update>
<delete id = "deleteUser">
delete from user where id = #{id}
</delete>
</mapper>
首次缓存中没有数据,从数据表中查询,打印日志如下:
当第二次缓存中有数据,直接取出缓存中的数据,打印日志如下:
同样用RDM redis可视化工具看一下缓存中的数据,具体如下: