Redis6總綱
文檔
https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache
目錄
1、簡介
2、構(gòu)建項目
3、注解、表達式語法
4、自定義緩存、緩存穿透解決
簡介
1、概述
Spring 從 3.1開始定義了org.springframework.cache.Cache
和org.springframework.cache.CacheManager,接口來統(tǒng)一不同的緩存技術(shù);并支持使用JCache (JSR107)注解簡化我們開發(fā);
Cache接口為緩存的組件規(guī)范定義,包含緩存的各種操作集合;
Cache接口下 Spring 提供了各種xxxCache..的實現(xiàn)﹔如RedisCache.,hCacheCache, ,ConcurrentMapCache等;
每次調(diào)用需要緩存功能的方法時,Spring 會檢查檢查指定參數(shù)的指定的目標方法是否已經(jīng)被調(diào)用過;如果有就直接從緩存中獲取方法調(diào)用后的結(jié)果,如果沒有就調(diào)用方法并緩存結(jié)果后返回給用戶。下次調(diào)用直接從緩存中獲取。
使用Spring緩存抽象時我們需要關(guān)注以下兩點;
(1)確定方法需要被緩存以及他們的緩存策略.
(2)從緩存中讀取之前緩存存儲的數(shù)據(jù)
2、關(guān)系圖
二、構(gòu)建項目
1、引入坐標
<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>
2、寫配置
spring.cache.type=redis
spring.redis.host=101.34.181.131
spring.redis.port=6379
3、開啟緩存功能
@EnableCaching
三、注解
@Cacheable: (觸發(fā)將數(shù)據(jù)保存到緩存的操作 )
@CacheEvict: (觸發(fā)將數(shù)據(jù)從緩存刪除的操作)
@CachePut: (不影響方法執(zhí)行更新緩存)
@Caching: (組合以上多個操作)
@CacheConfig: (在同一個類共享緩存的配置)
1、@Cacheable
代表這個是可以緩存的(在 TestServerImpl)
// 指定一個分區(qū)名字:比如用戶實體,在人類分區(qū)下(也可以指定多個,逗號隔開)
// 表示當前方法返回的結(jié)果需要緩存,如果緩存中有,則不調(diào)用當前被注解聲明的方法。反之。(調(diào)式就知道了)
@Cacheable(("Person"))
@Override
public List<User> getUsers() {
// dao層數(shù)據(jù)。。。。
List<User> userList = new ArrayList<> ();
userList.add (new User("1","小白",18));
userList.add (new User("2","小黑",17));
userList.add (new User("3","小黑",17));
return userList;
}
2、@CacheEvict 清除緩存(失效模式)
// 清空所有關(guān)聯(lián)的數(shù)據(jù)
// 失效模式
//@CacheEvict(value = "Person" ,key = "'getUsers'")// 清楚一個
// @Caching(evict = {
// @CacheEvict(value = "Person" ,key = "'getUsers'"),
// @CacheEvict(value = "Person" ,key = "'getUsers2'")
// })// 清楚多個
@CacheEvict(value = "Person",allEntries = true)// 清除整個分區(qū)下的所有key
@Override
public List<User> deleteList() {
return null;
}
3、@CachePut 更新緩存(雙寫模式)
// 更新緩存,雙寫模式
@CachePut(value = "Person",key = "'getUsers'")
@Override
public List<User> updateList() {
// dao層數(shù)據(jù)。。。。
List<User> userList = new ArrayList<> ( );
userList.add (new User ("1", "小白", 18));
return userList;
}
4、默認行為
(1)如果緩存中有key,方法不被調(diào)用(及時db和內(nèi)存的不一樣),反之
(2)key默認自動生成:Person::SimpleKey []
(3)緩存中value的值,使用 jdk序列化機制,將序列化后的數(shù)據(jù)存到redis
(4)默認時間 -1 表示永不失效(不符合規(guī)范,應(yīng)該有過期時間)
5、自定義緩存生成的 key
(1)指定生成的 key名字
(2)指定過期時間
(3)將數(shù)據(jù)保存為 JSON格式
6、表達式解決 自定義key
// 分區(qū)名 value ,key名 方法名(表達式)
@Cacheable(value = "Person",key = "#root.method.name")
7、配置文件設(shè)置過期時間(寫死的,會有雪崩問題)
#毫秒單位
spring.cache.redis.time-to-live=360000
四、自定義配置
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching// 開啟緩存
public class MyCacheConfig {
// @Autowired
// CacheProperties cacheProperties;
/**
* 自定義 Redis配置文件
*
* @return
*/
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig ( );
//實例化key的序列化器對象
RedisSerializer<String> redisSerializer = new StringRedisSerializer ( );
//實例化value的序列化器對象
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
new Jackson2JsonRedisSerializer<> (Object.class);
//覆蓋key的序列化方式
config = config.serializeKeysWith (RedisSerializationContext.SerializationPair.
fromSerializer (redisSerializer));
//覆蓋value的序列化方式
config = config.serializeValuesWith (RedisSerializationContext.SerializationPair.
fromSerializer (jackson2JsonRedisSerializer));
CacheProperties.Redis redisProperties = cacheProperties.getRedis ( );
// 配置文件生效
if (redisProperties.getTimeToLive ( ) != null) {
Duration timeToLive = redisProperties.getTimeToLive ( );
// 配置文件中設(shè)置的過期時間
long seconds = timeToLive.getSeconds ( );
config = config.entryTtl (redisProperties.getTimeToLive ( ));
}
if (redisProperties.getKeyPrefix ( ) != null) {
config = config.prefixCacheNameWith (redisProperties.getKeyPrefix ( ));
}
if (!redisProperties.isCacheNullValues ( )) {
config = config.disableCachingNullValues ( );
}
if (!redisProperties.isUseKeyPrefix ( )) {
config = config.disableKeyPrefix ( );
}
return config;
}
}
spring.cache.type=redis
spring.redis.host=101.34.181.131
spring.redis.port=6379
#毫秒單位
spring.cache.redis.time-to-live=360000
#緩存key加上前綴
spring.cache.redis.key-prefix=CACHE_
#開啟前綴
spring.cache.redis.use-key-prefix=true
#是否緩存控制(防止穿透問題)
spring.cache.redis.cache-null-values=true