首页数据添加redis缓存

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缓存,把写操作数据在缓存中删除
*再次查询时,走完整更新缓存步骤
相关推荐
FungLeo1 小时前
成为全栈·Vue 管理后台篇·Vue Router如何守住多角色后台边界
前端·javascript·vue.js·rbac·vue router·路由守卫·前端权限
FungLeo1 小时前
成为全栈·Vue 管理后台篇·分页只显示第一页:从接口total 定位到前端修复
前端·vue.js·状态模式·分页·naive ui·服务端分页·前端问题排查
时间的拾荒人1 小时前
Redis 集合类型详解:Set 与 Zset
redis·缓存·面试
一条Go1 小时前
OnCall 时段路由:跨午夜、左闭右开与「Local 不是时区」
后端·架构·github
Moment1 小时前
不会后期的我,发现了一个开源胶片神器😍😍😍
前端·后端·开源
数据库小学妹1 小时前
pgbouncer 事务池:prepared statement、临时表与会话状态
数据库·后端·postgresql
FungLeo2 小时前
成为全栈·Vue管理后台篇·仪表盘不是几张卡片:组合统计接口与角色视图
前端·javascript·vue.js·rbac·仪表盘·管理后台·tanstack query
ruanno_top2 小时前
可搜索的下拉框,键盘按不动:焦点进了下拉,事件还挂在触发器上
vue.js
三小河2 小时前
SWC 凭什么比 Babel 快 ?项目优化迁移踩坑记录
前端·后端
鶴哥只手遮天2 小时前
《Redis 8.10.3 深度剖析》系列 ·第 17 章. 一个迟到十六年的类型
redis·后端·开源