spring緩存cache的使用詳解
spring緩存cache的使用
在spring配置文件中添加schema和spring對緩存注解的支持:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" default-autowire="byName"> <!--緩存配置--> <cache:annotation-driven/>
在spring配置文件中加入緩存管理器:
<!-- generic cache manager --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="hardwareCache"/> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="bannerCache"/> </set> </property> </bean>
然后在代碼的service的impl層加上如下注解即可把數(shù)據(jù)緩存起來:
@Cacheable(value="bannerCache")
其中@Cacheable表示spring將緩存該方法獲取到的數(shù)據(jù),(緩存是基于key-value方式實(shí)現(xiàn)的),key為該方法的參數(shù),value為返回的數(shù)據(jù),當(dāng)你連續(xù)訪問該方法時(shí)你會(huì)發(fā)現(xiàn)只有第一次會(huì)訪問數(shù)據(jù)庫. 其他次數(shù)只是查詢緩存.減輕了數(shù)據(jù)庫的壓力.
當(dāng)更新了數(shù)據(jù)庫的數(shù)據(jù),需要讓緩存失效時(shí),使用下面的注解:
這個(gè)注解表示讓appCache緩存的所有數(shù)據(jù)都失效。
@CacheEvict(value = "appCache", allEntries = true)
springcache配置緩存存活時(shí)間
Spring Cache @Cacheable本身不支持key expiration的設(shè)置,以下代碼可自定義實(shí)現(xiàn)Spring Cache的expiration,針對Redis、SpringBoot2.0。
直接上代碼:
@Service @Configuration public class CustomCacheMng{ private Logger logger = LoggerFactory.getLogger(this.getClass()); // 指明自定義cacheManager的bean name @Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager") public User cache1(){ User user = new User().setId(1); logger.info("1"); return user; } @Cacheable(value = "test",key = "'obj2'") public User cache2(){ User user = new User().setId(1); logger.info("2"); return user; } // 自定義的cacheManager,實(shí)現(xiàn)存活2天 @Bean(name = "customCacheManager") public CacheManager cacheManager( RedisTemplate<?, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2)); return new RedisCacheManager(writer, config); } // 提供默認(rèn)的cacheManager,應(yīng)用于全局 @Bean @Primary public CacheManager defaultCacheManager( RedisTemplate<?, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); return new RedisCacheManager(writer, config); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring MVC + bootstrap實(shí)現(xiàn)文件上傳示例(帶進(jìn)度條)
本篇文章主要介紹了spring MVC + bootstrap實(shí)現(xiàn)文件上傳示例(帶進(jìn)度條),非常具有使用價(jià)值,有需要的朋友可以了解一下。2017-03-03Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解
這篇文章主要為大家介紹了Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11MyBatis學(xué)習(xí)教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(二)—如何使用MyBatis對users表執(zhí)行CRUD操作的相關(guān)資料,需要的朋友可以參考下2016-05-05java中for循環(huán)執(zhí)行的順序圖文詳析
關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2021-06-06Java創(chuàng)建對象之顯示創(chuàng)建與隱式創(chuàng)建
在本篇文章中,小編會(huì)帶大家學(xué)習(xí)面向?qū)ο笾嘘P(guān)于對象的創(chuàng)建之顯示創(chuàng)建和隱式創(chuàng)建,其實(shí)類和對象作為面向?qū)ο笾凶罨镜模彩亲钪匾?需要的朋友可以參考下2023-05-05Springboot之idea之pom文件圖標(biāo)不對問題
這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Spring?Data?JPA映射自定義實(shí)體類操作
這篇文章主要介紹了Spring?Data?JPA映射自定義實(shí)體類操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11