redis缓存未生效
pom依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
properties:
server.port=8083 spring.datasource.name=imooc_mall spring.datasource.url=jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 mybatis.mapper-locations=classpath:mappers/*.xml spring.redis.host=192.168.23.144 spring.redis.port=6379 spring.redis.password=123456
启动类:
@SpringBootApplication
//basePackages没有双引号
@MapperScan(basePackages="com.imooc.mall.model.dao")
@EnableSwagger2
@EnableCaching
//启动类必须得放在包的最外面
public class ImoocshopApplication {serviceimpl:
@Cacheable(value = "listCategoryForcustomer") //在哪个方法上使用这个缓存
public List<CategoryVo> listCategoryForcustomer(){
ArrayList<CategoryVo> categoryVos=new ArrayList<>();
//递归调用
recursionfindcaterories(categoryVos,0);
return categoryVos; //返回一个列表
}redis配置文件:
package com.imooc.mall.model.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import java.time.Duration;
/**
* redis主要配置缓存时间
*/
@Configuration
@EnableCaching
public class REdisConfig {
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory){
//固定配置格式:
RedisCacheWriter redisCacheWriter=RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
//配置延迟时间30s
redisCacheConfiguration.entryTtl(Duration.ofSeconds(30));
RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
return redisCacheManager;
}
}实体类:
package com.imooc.mall.model.pojo;
import java.io.Serializable;
import java.util.Date;
public class Category implements Serializable {
private Integer id;
private String name;
private Integer type;
private Integer parentId;
private Integer orderNum;
private Date createTime;
private Date updateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getOrderNum() {
return orderNum;
}
public void setOrderNum(Integer orderNum) {
this.orderNum = orderNum;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}虚拟机redis也启动了:

打断点调试每次都会访问到serviceimpl里面的实现方法:redis缓存未生效是为啥
不明白为啥,感觉像是没启动redis缓存一样
控制台未报任何错误
11
收起
正在回答 回答被采纳积分+1
1回答
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星