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(); } }