Spring Cache相關知識總結
Spring 從 3.1 開始定義了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口來統一不同的緩存技術; 并支持使用 JCache ( JSR-107 )注解簡化我們開發;
Cache 接口為緩存的組件規范定義,包含緩存的各種操作集合; Cache 接 口 下 Spring 提 供 了 各 種 xxxCache 的 實 現 ; 如 RedisCache , EhCacheCache , ConcurrentMapCache 等;
每次調用需要緩存功能的方法時, Spring 會檢查檢查指定參數的指定的目標方法是否已 經被調用過;如果有就直接從緩存中獲取方法調用后的結果,如果沒有就調用方法并緩 存結果后返回給用戶。下次調用直接從緩存中獲取。
使用 Spring 緩存抽象時我們需要關注以下兩點;
1 、確定方法需要被緩存以及他們的緩存策略
2 、從緩存中讀取之前緩存存儲的數據
第一步
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache </artifactId> </dependency>
第二步application.properties配置:
spring.cache.type=redis spring.cache.redis.time-to-live=3600000spring.cache.redis.key-prefix=CACHE_spring.cache.redis.use-key-prefix=truespring.cache.redis.cache-null-values=true
第三步:
config創建MyCacheConfig
package com.atguigu.gulimall.product.config; import org.springframework.boot.autoconfigure.cache.CacheProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;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.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer; @EnableConfigurationProperties(CacheProperties.class)@Configuration@EnableCachingpublic class MyCacheConfig { // @Autowired// CacheProperties cacheProperties; /** * 配置文件中的東西沒有用上; * * 1、原來和配置文件綁定的配置類是這樣子的 * @ConfigurationProperties(prefix = 'spring.cache') * public class CacheProperties * * 2、要讓他生效 * @EnableConfigurationProperties(CacheProperties.class) * * @return */ @Bean RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();//config = config.entryTtl();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.prefixKeysWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()) { config = config.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()) { config = config.disableKeyPrefix();} return config; }}
第四步:
測試使用緩存 * @Cacheable: Triggers cache population.:觸發將數據保存到緩存的操作 * @CacheEvict: Triggers cache eviction.:觸發將數據從緩存刪除的操作 * @CachePut: Updates the cache without interfering with the method execution.:不影響方法執行更新緩存 * @Caching: Regroups multiple cache operations to be applied on a method.:組合以上多個操作 * @CacheConfig: Shares some common cache-related settings at class-level.:在類級別共享緩存的相同配置
失效模式:編輯的時候直接清空緩存。使其第一次查庫的時候存入緩存雙寫模式:有一定的延遲,緩存期以后才可以讀到最新數據
具體案例:
@Cacheable(value = {'category'},key = '#root.method.name',sync = true) @Override public List<CategoryEntity> getLevel1Categorys() {System.out.println('getLevel1Categorys.....');long l = System.currentTimeMillis();List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq('parent_cid', 0));return categoryEntities; }
以下沒有整理。暫時記錄一下。
到此這篇關于Spring Cache相關知識總結的文章就介紹到這了,更多相關Spring Cache內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
