日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot使用缓存功能

發(fā)布時間:2025/4/16 javascript 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot使用缓存功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

緩存的好處不言而喻,比如查詢商品的價格,如果可以放到緩存中,而不用每次都到數(shù)據(jù)庫中查詢,這將會大大提升系統(tǒng)性能,因為和緩存交互比訪問數(shù)據(jù)庫要快很多。或者在緩存中存放臨時數(shù)據(jù),而不用放到數(shù)據(jù)庫中。

在學(xué)習(xí)Spring Boot中的數(shù)據(jù)的時候,我們需要先來了解一下幾個非常重要的概念:

  • Cache: 緩存接口,定義緩存操作,具體實現(xiàn)這些操作的有:RedisCache、EhCacheCache、ConcurrentMapCache等。
  • CacheManager:緩存管理器,管理各種緩存(Cache)組件
  • @Cacheable:主要針對方法配置,能夠根據(jù)方法的請求參數(shù)對方法返回的結(jié)果進(jìn)行緩存。
  • @CacheEvict:清除緩存,比如一個用戶退出了,我們需要清除這個用戶的所有臨時數(shù)據(jù)緩存。
  • @CachePut:保證方法被調(diào)用,又希望結(jié)果被緩存,主要可以用來更新緩存。
  • @EnableCaching:開啟基于注解的緩存
  • keyGenerator:緩存數(shù)據(jù)時key的生成策略
  • serialize:緩存數(shù)據(jù)時value序列化策略
  • 以數(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.NoOpCacheConfiguration

    SimpleCacheConfiguration配置類將給容器中注冊一個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:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐redis</artifactId> </dependency><!--或者--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId><version>1.4.7.RELEASE</version> </dependency>

    在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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。