2.0.3.RELEASEhtml
①,pom.xmljava
<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.propertiesnginx
spring.cache.ehcache.config=classpath:/ehcache.xml
③,啓用緩存註解web
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爲咱們注入的緩存先關類spring
@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; } //緩存管理器 @Bean public EhCacheCacheManager cacheManager(CacheManager ehCacheCacheManager) { return this.customizers.customize(new EhCacheCacheManager(ehCacheCacheManager)); } @Bean @ConditionalOnMissingBean public 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>
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 { //注入緩存管理器 @Autowired EhCacheCacheManager cacheManager; //使用註解緩存,key默認爲入參 @Cacheable() public String find(Person person) { person.getAge(); System.out.println("service 進來了。。。"); return "service find===="; } //手動使用緩存 public String get(Integer age) { //查找指定緩存home org.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(); } }