javascript
Spring Boot使用缓存功能
緩存的好處不言而喻,比如查詢商品的價格,如果可以放到緩存中,而不用每次都到數(shù)據(jù)庫中查詢,這將會大大提升系統(tǒng)性能,因為和緩存交互比訪問數(shù)據(jù)庫要快很多。或者在緩存中存放臨時數(shù)據(jù),而不用放到數(shù)據(jù)庫中。
在學(xué)習(xí)Spring Boot中的數(shù)據(jù)的時候,我們需要先來了解一下幾個非常重要的概念:
以數(shù)據(jù)庫查詢緩存為例,緩存結(jié)構(gòu)如下:
如果要使用緩存的話,這里就不給出具體的例子了,在上面重要的概念的講解中,我們已經(jīng)講解了幾個注解的作用,在編程過程中使用這些注解即可。關(guān)于這些注解具體的使用規(guī)則,可以參考:https://blog.csdn.net/xm393392625/article/details/88639082
我們來看看Spring Boot底層是如何實現(xiàn)緩存配置的,依照之前的經(jīng)驗我們可以想象存在一個CacheAutoConfiguration,這個類將會完成我們的緩存配置,實際上這個類也存在,這個配置類導(dǎo)入了CacheConfigurationImportSelector這個類,他將會選擇出下面這些緩存配置類,默認(rèn)SimpleCacheConfiguration生效:
org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration【默認(rèn)】 org.springframework.boot.autoconfigure.cache.NoOpCacheConfigurationSimpleCacheConfiguration配置類將給容器中注冊一個CacheManager:ConcurrentMapCacheManager,它可以獲取和創(chuàng)建ConcurrentMapCache類型的緩存組件,它的作用是將數(shù)據(jù)保存在ConcurrentMap中;
以@Cacheable注解為例講解獲取緩存的流程:
- 方法運行之前,先去CacheManager中按照cacheNames指定的名字得到Cache(緩存組件),如果沒有Cache組件則會自動創(chuàng)建。結(jié)合之前的圖,Emp就是一個Cache組件。
- 之后在找到的Cache組件中查找緩存的內(nèi)容,使用一個key,默認(rèn)就是使用方法的參數(shù)生成這個key,比如在Emp這個Cache組件中查找具體的Value。
- 沒有查到緩存就調(diào)用目標(biāo)方法;
- 之后將目標(biāo)方法返回的結(jié)果,放進(jìn)緩存中;
以上就是Spring Boot中的緩存原理,最后來說說使用其他的CacheManager,比如常用的radis。
這里我使用的環(huán)境是 ubuntu + docker + redis來啟動,在docker pull redis已經(jīng)完成的情況下,運行下列內(nèi)容:
然后在項目中的pow.xml文件中引入redis:
在RedisAutoConfiguration.java中Spring Boot已經(jīng)幫我們做好的配置,如下:
/*** {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis support.** @author Dave Syer* @author Andy Wilkinson* @author Christian Dupuis* @author Christoph Strobl* @author Phillip Webb* @author Eddú Meléndez* @author Stephane Nicoll* @author Marco Aust* @author Mark Paluch* @since 1.0.0*/ @Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration {@Bean@ConditionalOnMissingBean(name = "redisTemplate")public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);return template;}@Bean@ConditionalOnMissingBeanpublic StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}}可以看到它幫我們在容器中注冊了StringRedisTemplate (K-V都是字符串)和RedisTemplate(K-V都是Object)兩個組件,他們就和JdbcTemplate一樣,幫助我們操作redis,不過我們可以直接使用緩存注解,在底層會自動調(diào)用這些方法,RedisTemplate 保存對象時是保存序列化后的數(shù)據(jù),所以務(wù)必確認(rèn)保存的對象可以序列化。不過大多數(shù)的時候,我們只是保存一個String和一個JSON串,所有StringRedisTemplate用的可能更多一點。如果要使用StringRedisTemplate 和RedisTemplate的話直接注入即可。不過在使用之前,需要在配置文件中寫好redis主機地址:
spring.redis.host=192.168.31.246引入redis的依賴之后,容器中保存的是RedisCacheConfiguration為我們添加的RedisCacheManager,而不使用之前所說的SimpleCacheConfiguration為我們添加的CacheManager。這是因為在SimpleCacheConfiguration類之上有一個@ConditionalOnMissingBean(CacheManager.class)這樣的注解。默認(rèn)創(chuàng)建的 RedisCacheManager 操作redis的時候使用的是 RedisTemplate<Object, Object>,RedisTemplate<Object, Object> 是默認(rèn)使用jdk的序列化機制。如果你想保存JSON數(shù)據(jù)的話,請自行查找其他參考內(nèi)容。
總結(jié)
以上是生活随笔為你收集整理的Spring Boot使用缓存功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot数据库操作原理及整
- 下一篇: gradle idea java ssm