1、springboot整合redis
(1)在common引入redis依赖
java
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
(2)编写redis配置类,配置redis相关插件
(3)在service_cms模块配置文件中添加redis配置
java
spring.redis.host=你自己的虚拟机ip
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
(4)在具体功能上添加 查询功能,先查询缓存,没有,之后查询数据库,缓存中存储数据
java
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
//前台查询banner
//value::key banner::selectIndexList
@Cacheable(value = "banner",key = "'selectIndexList'")
@Override
public List<Banner> getAllBanner() {
List<Banner> bannerList = baseMapper.selectList(null);
return bannerList;
}
}
java
*数据库写操作时,不需要同步更新redis缓存,把写操作数据在缓存中删除
*再次查询时,走完整更新缓存步骤