SpringCache + Redis 缓存
- 正确使用缓存,不仅能够减少数据库的压力,而且可以加快程序运行的速度。
//启动类需加上@EnableCaching注解
@EnableCaching
@SpringBootApplication()
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
1. @Cacheable注解
① 用于查询方法上,表示将一个方法的返回值缓存起来
② value属性与cacheNames属性,只能取其一
③ key 属性,默认情况下,缓存的key就是方法的参数
@Cacheable(value = "stu:list")
public List<Student> getStuList() {
return mapper.selectList(new QueryWrapper<Student>());
}
2. @CachePut注解
① 用于更新方法上,当数据库中的数据更新后,缓存中的数据也随之更新
② 配合@Cacheable使用,注意value值或cacheNames值保持一致
@CachePut(value = "stu:age")
public Integer updateStuAge(Long id) {
Student student = new Student();
student.setAge(24);
mapper.update(student, new QueryWrapper<Student>().lambda()
.eq(Student::getId, id));
return student.getAge();
}
3. @CacheEvict注解
① 用于删除方法上或者新增方法上,
② value属性可以是通常是一个字符串数组
③ allEntries属性为true,表示删除当前value值下所有缓存
@CacheEvict(value = {
"stu:name",
"stu:age"
}, allEntries = true)
public void addStu() {
mapper.insert(new Student().setName("aa").setAge(22));
logger.info("{}", "添加成功");
}
4. RedisCacheManager配置类
@Configuration
public class RedisConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
//设置过期时间
.entryTtl(Duration.ofHours(1))
//设置key序列化
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
//设置value序列化
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(config).build();
}
}
5. 环境配置
spring:
redis:
database: 1
host: 127.0.0.1
password: 123456
port: 6379
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
url: jdbc:mysql://localhost:3306/redis_cache?useSSL=false&characterEncoding=utf-8
username: root
password: 123456
6. pom依赖文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>