spring boot项目集成redis实战
前言
spring boot 集成redis,实现缓存数据库数据,减少数据库访问次数,在效率和性能上而言,都是一个很好的优化。本文着重,操作步骤流程实现spring boot项目redis集成实战,并没有很多原理的讲解。
步骤一:
application.yml
众所周知,spring boot项目的配置文件可以是application.yml或application.properties两种,个人更倾向application.yml
spring:
#...other config
redis:
# 超时时间
timeout: 10000ms
# 服务器地址(如果服务器,请把服务器地址贴在这里)
host: 127.0.0.1
# 服务器端口
port: 6379
# 数据库
database: 0
# redis如果设置了密码,需要把密码填好
# password:root
lettuce:
pool:
# 最大连接数
max-active: 1024
# 最大连接阻塞等待时间,默认-1
max-wait: 10000ms
# 最大空闲连接
max-idle: 200
# 最小空闲连接
min-idle: 5
步骤二:
pom.xml
在依赖文件下,添加相关依赖:
<!--spring data redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--commons-pool2对象池依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
步骤一二,是需要在应用里手动添加的一些配置,接下来的配置是需要在应用中添加相关的代码了。
步骤三:
RedisConfig.java
//这个配置的文件的路径是:src/main/java/com.xxx.server/config/RedisConfig.java
package com.xxx.server.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//Redis配置类
@Configuration
public class RedisConfig {
// 主要是为了序列化
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
// String类型key序列器
redisTemplate.setKeySerializer(new StringRedisSerializer());
// String类型value序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// Hash类型value序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// Hash类型value序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
配置好了redis的序列化之后,就可以去访问数据库的接口实现类那里写相关的逻辑了(这里,我以根据用户id查询菜单作为例子贴代码,省略控制器等其他几个模块的接口编写,为你造成阅读不便,请谅解哈,欢迎一起讨论)
步骤四:
MenuServiceImpl.java
package com.xxx.server.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.server.mapper.MenuMapper;
import com.xxx.server.pojo.Admin;
import com.xxx.server.pojo.Menu;
import com.xxx.server.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* <p>
* 菜单表 服务实现类
* </p>
*
* @author 黄华康
* @since 2021-08-03
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Autowired
private MenuMapper menuMapper;
@Autowired
private RedisTemplate redisTemplate;
// 通过用户Id查询菜单列表
@Override
public List<Menu> getMenusByAdminId() {
// 获取上下文,从上下文中获取用户id(用户已登录的前提下)
Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
// 定义获取操作redis的api
ValueOperations<String,Object> valueOperations=redisTemplate.opsForValue();
// 从redis获取菜单数据
List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + adminId);
// 如果为空,则去数据库获取
if(CollectionUtils.isEmpty(menus)){
menus= menuMapper.getMenusByAdminId(adminId);
// 将数据设置到Redis中
valueOperations.set("menu_"+adminId,menus);
}
return menus;
}
}
步骤到目前为止呢,集成redis应用到查询菜单接口,已经完成一大半了,也就是说需要贴的代码就是以上这些,接下来的步骤就是下载redis和启动redis了。
步骤五:
下载redis(我现用的版本是:redis-6.2.4)
根据自己的需要去官网下载redis即可。windows系统和linux系统的详细操作步骤可以去菜鸟教程看:菜鸟教程-redis
//mac ios的朋友操作如下;
//找到文件夹下的src目录,在该位置打开终端,键入如下命令行并回车
./redis-server
//紧接着,不要关闭终端,重新打开新的终端,键入如下命令,并回车
redis-cli
//备注:我没有设置并且是使用本地服务器,
到这里,只需要重启sring boot项目,测试一下,获取菜单接口,然后到redis终端输入:key *
就可以查看,缓存在redis的数据了,到这里一个描述的不够细的集成redis实战就完成了