配置
org.springframework.boot
spring-boot-starter-data-redis
spring:
redis:
host: x.x.x.x
port: 6379
password: xxx
database: 0
lettuce:
pool:
min-idle: 0 #连接池最小空闲连接
max-idle: 8 #连接池最大空闲连接
max-active: 20 #连接池最大连接数
max-wait: -1 #连接池耗尽时抛出异常的最长待时间,负值表示无限等待,单位ms
shutdown-timeout: 100 #关闭客户端连接前,等待任务处理完的最长时间,单位ms
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.CacheManager;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
@Configuration
public class RedisConfig {
private static final RedisSerializer KEY_SERIALIZER = new StringRedisSerializer();
private static final RedisSerializer
注解释义
@Cacheable(value = "xxx", key = "#sysUserDTO.id", unless = "#result==null")
解释:
如果redis中没有该缓存,就将返回值缓存到redis中
value是缓存名字,与cacheNames同义
key是缓存名字里的key,一个缓存名字里可以包含多个key
unless为是否缓存的条件,里面是SpEL表达式.当表达式为true时不缓存,为false时缓存
unless与condition不同,unless中的表达式在方法被调用后计算,因此可以引用result,而condition中的表达式在方法被条用之前计算的,不能引用result
在redis中表现形式:
缓存名::key1
缓存名::key2
备注:
这三个参数都可以不写,value默认是方法名,key默认是参数名,unless默认是""
@CachePut(参数同@Cacheable)
解释:
无论redis中有没有缓存,都将返回值缓存到redis中,并重置缓存时效,常用于更新场景
@CacheEvict(value="xxx", allEntries = false, beforeInvocation=false)
解释:
删除缓存,常用于新增场景
value是待删除的缓存名字,allEntries表示是否将该缓存名下的所有key删除,默认为false
beforeInvocation表示是否在方法执行前就删除,默认为false.默认情况下,如果方法执行抛出异常,则不会删除缓存
@Caching(cacheable={} ,put={} ,evict={})
解释:
@Caching(
put = {
@CachePut(value = "valueName", key = "#user.id"),
@CachePut(value = "valueName", key = "#user.name"),
@CachePut(value = "valueName", key = "#user.address")
}
)
@CacheConfig(cacheNames = "CacheConfigName")
解释:
该注解只能注解在类或接口上,表示共享一组缓存名字
SpEL上下文数据
| 名称 | 位置 | 描述 | 示例 |
|---|---|---|---|
| methodName | root对象 | 当前被调用的方法名 | #root.methodname |
| method | root对象 | 当前被调用的方法 | #root.method.name |
| target | root对象 | 当前被调用的目标对象实例 | #root.target |
| targetClass | root对象 | 当前被调用的目标对象的类 | #root.targetClass |
| args | root对象 | 当前被调用的方法的参数列表 | #root.args[0] |
| caches | root对象 | 当前方法调用使用的缓存列表 | #root.caches[0].name |
| Argument Name | 执行上下文 | 当前被调用的方法的参数,如findArtisan(Artisan artisan),可以通过#artsian.id获得参数 | #artsian.id |
| result | 执行上下文 | 方法执行后的返回值(仅当方法执行后的判断有效,如 unless cacheEvict的beforeInvocation=false) | #result |
注意:
1.当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为Spring默认使用的就是root对象的属性。 如
@Cacheable(key = "targetClass + methodName +#p0")
2.使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。 如:
@Cacheable(value="users", key="#id")
@Cacheable(value="users", key="#p0")
SpEL运算符
| 类型 | 运算符 |
|---|---|
| 关系 | <,>,<=,>=,==,!=,lt,gt,le,ge,eq,ne |
| 算术 | +,- ,* ,/,%,^ |
| 逻辑 | &&,||,!,and,or,not,between,instanceof |
| 条件 | ?: (ternary),?: (elvis) |
| 正则表达式 | matches |
| 其他类型 | ?.,?[…],![…],^[…],$[…] |



