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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot简单使用ehcache

發布時間:2025/3/20 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot简单使用ehcache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

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為我們注入的緩存先關類

@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"><!--將緩存保存到硬盤的那個位置 --><diskStore path="d:/ehcache/"></diskStore><!-- <diskStore path="java.io.tmpdir"></diskStore>java.io.tmpdir取值:操作系統不同 這個系統屬性所表示的目錄也不同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表示對象永不過期,此時會忽略timeToIdleSeconds和timeToLiveSeconds屬性,默認為false --><!-- maxEntriesLocalHeap:堆內存中最大緩存對象數,0沒有限制 --><!-- timeToIdleSeconds: 設定允許對象處于空閑狀態的最長時間,以秒為單位。當對象自從最近一次被訪問后,如果處于空閑狀態的時間超過了timeToIdleSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清空。只有當eternal屬性為false,該屬性才有效。如果該屬性值為0,則表示對象可以無限期地處于空閑狀態 --><!-- diskPersistent:是否在磁盤上持久化。指重啟jvm后,數據是否有效。默認為false。 --><!-- memoryStoreEvictionPolicy:如果內存中數據超過內存限制,向磁盤緩存時的策略。默認值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默認為入參@Cacheable()public String find(Person person) {person.getAge();System.out.println("service 進來了。。。");return "service find====";}//手動使用緩存public String get(Integer age) { //查找指定緩存homeorg.springframework.cache.Cache cache = cacheManager.getCache("home"); //根據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();}}

?

轉載于:https://my.oschina.net/u/3574106/blog/1835380

總結

以上是生活随笔為你收集整理的SpringBoot简单使用ehcache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。