當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot简单使用ehcache
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot简单使用ehcache
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1,SpringBoot版本
2.0.3.RELEASE
①,pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId></dependency></dependencies>②,application.properties
spring.cache.ehcache.config=classpath:/ehcache.xml③,啟用緩存注解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication //啟用緩存注解 @EnableCaching public class SpringbootEhcacheApplication {public static void main(String[] args) {SpringApplication.run(SpringbootEhcacheApplication.class, args);} }④,springboot為我們注入的緩存先關(guān)類
@Configuration @ConditionalOnClass({ Cache.class, EhCacheCacheManager.class }) @ConditionalOnMissingBean(org.springframework.cache.CacheManager.class) @Conditional({ CacheCondition.class,EhCacheCacheConfiguration.ConfigAvailableCondition.class }) class EhCacheCacheConfiguration {private final CacheProperties cacheProperties;private final CacheManagerCustomizers customizers;EhCacheCacheConfiguration(CacheProperties cacheProperties,CacheManagerCustomizers customizers) {this.cacheProperties = cacheProperties;this.customizers = customizers;} //緩存管理器@Beanpublic EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) {return this.customizers.customize(new EhCacheCacheManager(ehCacheCacheManager));}@Bean@ConditionalOnMissingBeanpublic CacheManager ehCacheCacheManager() {Resource location = this.cacheProperties.resolveConfigLocation(this.cacheProperties.getEhcache().getConfig());if (location != null) {return EhCacheManagerUtils.buildCacheManager(location);}return EhCacheManagerUtils.buildCacheManager();}static class ConfigAvailableCondition extends ResourceCondition {ConfigAvailableCondition() {super("EhCache", "spring.cache.ehcache.config", "classpath:/ehcache.xml");}}}⑤,類路徑下的ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><!--將緩存保存到硬盤的那個(gè)位置 --><diskStore path="d:/ehcache/"></diskStore><!-- <diskStore path="java.io.tmpdir"></diskStore>java.io.tmpdir取值:操作系統(tǒng)不同 這個(gè)系統(tǒng)屬性所表示的目錄也不同On Windows: java.io.tmpdir:[C:\Users\liyhu\AppData\Local\Temp\]On Solaris: java.io.tmpdir:[/var/tmp/]On Linux: java.io.tmpdir: [/tmp]On Mac OS X: java.io.tmpdir: [/tmp]--><!--指定緩存名 --><cache name="home"eternal="false"maxEntriesLocalHeap="0"timeToIdleSeconds="200"diskPersistent="true"/><!-- eternal:true表示對象永不過期,此時(shí)會(huì)忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認(rèn)為false --><!-- maxEntriesLocalHeap:堆內(nèi)存中最大緩存對象數(shù),0沒有限制 --><!-- timeToIdleSeconds: 設(shè)定允許對象處于空閑狀態(tài)的最長時(shí)間,以秒為單位。當(dāng)對象自從最近一次被訪問后,如果處于空閑狀態(tài)的時(shí)間超過了timeToIdleSeconds屬性值,這個(gè)對象就會(huì)過期,EHCache將把它從緩存中清空。只有當(dāng)eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地處于空閑狀態(tài) --><!-- diskPersistent:是否在磁盤上持久化。指重啟jvm后,數(shù)據(jù)是否有效。默認(rèn)為false。 --><!-- memoryStoreEvictionPolicy:如果內(nèi)存中數(shù)據(jù)超過內(nèi)存限制,向磁盤緩存時(shí)的策略。默認(rèn)值LRU,可選FIFO、LFU。--></ehcache>2,使用緩存
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache.ValueWrapper; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.stereotype.Service;import com.example.demo.bean.Person;@Service @CacheConfig(cacheNames = "home") public class HomeService { //注入緩存管理器@AutowiredEhCacheCacheManager cacheManager; //使用注解緩存,key默認(rèn)為入?yún)?#64;Cacheable()public String find(Person person) {person.getAge();System.out.println("service 進(jìn)來了。。。");return "service find====";}//手動(dòng)使用緩存public String get(Integer age) { //查找指定緩存homeorg.springframework.cache.Cache cache = cacheManager.getCache("home"); //根據(jù)key查找是否有緩存該值ValueWrapper val = cache.get(age);if(val!=null) {System.out.println("有緩存。。。");return val.get().toString();}else {System.out.println("沒有緩存。。。");cache.put(age, age);return age.toString();}}?
轉(zhuǎn)載于:https://my.oschina.net/u/3574106/blog/1835380
總結(jié)
以上是生活随笔為你收集整理的SpringBoot简单使用ehcache的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APScheduler 浅析
- 下一篇: orcale存储过程学习之路--创建空存