SpringCache 分布式緩存的實現(xiàn)方法(規(guī)避redis解鎖的問題)
簡介
spring 從3.1 開始定義
- org.springframework.cache.Cache
- org.springframework.cache.CacheManager
來統(tǒng)一不同的緩存技術
并支持使用JCache(JSR-107)注解簡化我們的開發(fā)
基礎概念
實戰(zhàn)使用
整合SpringCache簡化緩存開發(fā)
常用注解
常用注解 | 說明 |
---|---|
@CacheEvict | 觸發(fā)將數據從緩存刪除的操作 (失效模式) |
@CachePut | 不影響方法執(zhí)行更新緩存 |
@Caching | 組合以上多個操作 |
@CacheConfig | 在類級別共享緩存的相同配置 |
@Cacheable | 觸發(fā)將數據保存到緩存的操作 |
方法
1)、開啟緩存功能 @EnableCaching
2)、只需要使用注解就能完成緩存操作
1、引入依賴
spring-boot-starter-cache、spring-boot-starter-data-redis
配合redis使用
<!-- 引入 redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- 排除 lettuce --> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
2、寫配置
在項目新建config文件夾,新建一個config類
代碼如下:
@EnableConfigurationProperties(CacheProperties.class)//為configuration容器中放參數 @EnableCaching @Configuration public class MyCacheConfig { /** * 配置文件中的內容不再生效(全部走自定義配置) * @param cacheProperties * @return */ @Bean RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); config= config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())); config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); CacheProperties.Redis redisProperties = cacheProperties.getRedis(); if (redisProperties.getTimeToLive() != null) { 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; } }
(1)、自動配置寫了哪些 CacheAutoConfiguration 會導入 RedisAutoConfiguration 自動配置好緩存管理器RedisCacheManager (2)、配置使用redis做為緩存 spring.cache.typeredis
3、修改pom 配置
spring: cache: type: redis redis: # 緩存過期時間 time-to-live: 60000 # 如果制定了前綴,我們就是用指定的前綴,如果沒有我們就默認使用緩存的名字作為前綴 key-prefix: CACHE_ # 是否使用前綴 use-key-prefix: true # 是否把緩存空值,防止緩存穿透 cache-null-values: true
4、原理
1、每一個要緩存的數據 我們都來指定要放到那個名字的緩存【緩存的分區(qū)(按照業(yè)務類型)】 2、@cacheable({"category"}) 代表當前方法的結果需要緩存,如果緩存中,方法不用調用 如果緩存中沒有,會調用方法,最后將方法的結果放入緩存 3、默認行為 1)、如果緩存中有,方法不用調用 2)、key默認自動生成:緩存的名字::SimpleKey[] (自主生成的key值) 3)、緩存的value的值。默認使用jdk序列化機制,將序列化后的數據存到redis 4)、默認 ttl 時間 -1 (永不過期) 自定義: 1)、指定生成的緩存使用的key: key屬性制定,接受一個SpEL SpEL(詳見文檔) 2)、指定緩存的數據的存活時間:配置文件中修改 ttl 3)、將數據保存為 json 格式: 自定義 RedisCacheConfiguration即可
失效模式:@CacheEvict
原理:變更緩存的時候會將redis中的緩存刪除
(當下次查詢時,會重新載入緩存)
推薦使用@CacheEvict
同時進行多種緩存操作 @Caching指定刪除某個分區(qū)下的所有數據
@CacheEvict(value=“category”,allEntries=true)存儲統(tǒng)一類型的數據,都可以指定成同一個分區(qū)。分區(qū)名默認就是緩存的前綴
類中使用:@CacheEvict(value=“category”,allEntries=true)
配置中使用:(禁用前綴 + 默認前綴)
spring.cache.redis.use-key-prefix=true
雙寫模式:@CachePut
原理:在變更緩存時,刪除原有的緩存,然后將新數據重新插入到緩存中
到此這篇關于SpringCache 分布式緩存(規(guī)避redis解鎖的問題)的文章就介紹到這了,更多相關SpringCache 分布式緩存內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
datax-web在windows環(huán)境idea中模塊化打包部署操作步驟
這篇文章主要介紹了datax-web在windows環(huán)境idea中模塊化打包部署操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05SpringBoot項目在IntelliJ IDEA中如何實現(xiàn)熱部署
spring-boot-devtools是一個為開發(fā)者服務的一個模塊,其中最重要的功能就是自動應用代碼更改到最新的App上面去。,這篇文章主要介紹了SpringBoot項目在IntelliJ IDEA中如何實現(xiàn)熱部署,感興趣的小伙伴們可以參考一下2018-07-07springboot schedule 解決定時任務不執(zhí)行的問題
這篇文章主要介紹了springboot schedule 解決定時任務不執(zhí)行的問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09