ehcache
一、springboot下如何使用?文档
1. pom.xml中引入ehcahe

<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2. classpath下引入ehcache的配置

<?xml version="1.0" encoding="UTF-8" ?> <ehcache> <diskStore path="java.io.EhCache"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <cache name="company" maxElementsInMemory="1" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" maxElementsOnDisk="2" diskExpiryThreadIntervalSeconds="600" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> <cache name="energyType" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="6000" timeToLiveSeconds="6000" maxElementsOnDisk="50" diskExpiryThreadIntervalSeconds="6000" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache> </ehcache>
3. springboot入口标记使用缓存

@EnableCaching @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class TsApplication { public static void main(String[] args) { SpringApplication.run(TsApplication.class, args); } }
4. 使用,在需要缓存处理的方法上使用注解标记

@Cacheable(key = "#param1+'-'+#param2") public Object methodNameHere(int param1,int param2) { return something; }
二、ehcache的原理、架构理解
三、 需要注意的要点:
1.设计严谨合理的缓存策略、及时清除(@CacheEvict)无用的缓存
2.缓存的key如何指定
@Cacheable(key = "#param1+'-'+#param2") public Object methodNameHere(int param1,int param2) { return something; } @Cacheable(key = "'const_key'") public Object methodNameHere() { // 对于没有参数的函数,可以通过单引号,引用字符串作为常量key return something; }