1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-data-redis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<!--springboot启动类依赖导入-->
<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>
</dependencies>
</project>
2、config配置
package org.example.config;
import org.springframework.beans.factory.annotation.Autowired;
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.StringRedisSerializer;
// 在这里只序列化key,序列化值可能会造成存入Redis中时发生类型转换错误
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//默认的Key序列化器为:JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
3、application.yml
spring:
redis:
database: 0
host: localhost
port: 6379
password:
cache:
redis:
time-to-live: 1800000 # 设置缓存过期时间 30分钟
4、注解解释
1、@EnableCaching #开启缓存注解支持
2、@Cacheable #如果缓存中存在数据直接返回,不执行方法,没有便查询数据库,同时存储在缓存
属性 | 说明 |
value | 缓冲名称;可以用户确定目标缓冲,与指定bean定义的限定符的值或者bean名称相匹配。根据这个特性,我们就可以设置一些策略,对不同的缓冲对象设置各自的缓冲方式。(为缓冲注解设置缓冲有效时间就是使用这个特性做的一个策略) |
key | 这个是key键,只不过这里是spring表达式语言(spel),默认值是‘’(spel表达式将大大方便key值得设置)。如果设置了keyGenerator属性,这个属性将会失效。 |
condition | 缓冲条件,采用spring表达式语言(spel)。默认值是‘’,表示直接缓冲。条件为true时进行缓冲,false不缓冲 |
unless | spring表达式语言(spel)用于否决方法缓存的表达式。与condition()不同,此表达式在调用方法后计算,因此可以引 用结果。true不缓冲,false缓冲,其实和condition正好相反 |
3、@Cacheput #保证方法能执行,同时更新数据库与添加至缓存,一般用于更新操作
属性 | 说明 |
value | 缓冲名称;可以用户确定目标缓冲,与指定bean定义的限定符的值或者bean名称相匹配。根据这个特性,我们就可以设置一些策略,对不同的缓冲对象设置各自的缓冲方式。(为缓冲注解设置缓冲有效时间就是使用这个特性做的一个策略) |
cacheNames | 和value是一样的,它们两个互为别名 |
key | 这个是key键,只不过这里是spring表达式语言(spel),默认值是‘’(spel表达式将大大方便key值得设置)。如果设置了keyGenerator属性,这个属性将会失效。 |
condition | 缓冲条件,采用spring表达式语言(spel)。默认值是‘’,表示直接缓冲。条件为true时进行缓冲,false不缓冲 |
unless | spring表达式语言(spel)用于否决方法缓存的表达式。与condition()不同,此表达式在调用方法后计算,因此可以引 用结果。true不缓冲,false缓冲,其实和condition正好相反 |
4、@CacheEvict #用于从缓存中删除一个或者一类数据
属性 | 说明 |
value | 缓冲名称 |
cacheNames | 和value是一样的,它们两个互为别名 |
key | 这个是key键,只不过这里是spring表达式语言(spel),默认值是‘’(spel表达式将大大方便key值得设置)。如果设置了keyGenerator属性,这个属性将会失效。 |
keyGenerator | 自定义键值生成器对象的名称,相当于自己定义一个redis键的解析器 |
cacheManager | 这里是设置缓冲管理器的。不设置采用默认定义的缓冲管理器;这里我们可以自己顶一个多个缓冲管理器,然后在缓冲指定数据的时候可以使用指定的缓冲管理器。 |
cacheResolver | 自定义缓冲解析程序 |
condition | 缓冲条件,采用spring表达式语言(spel)。默认值是‘’,表示直接缓冲。条件为true时进行缓冲,false不缓冲 |
allEntries | 是否清除缓冲中的所有数据。如果这个值设置为true,那么key的设置将失去意义(因为所有数据都会被删除),所以true的情况下就不要设置key属性了。 |
beforeInvocation | 是否在方法执行之前就执行缓冲清除操作。 true 表示,在方法执行前就执行缓冲清除操作。 false 表示在方法执行后执行缓冲清除操作,如果方法执行异常则不进行缓冲清除操作。 |