<!-- 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- 引入ehcache支持 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
启动类上面添加@EnableCaching 注解
@SpringBootApplication @EnableCaching public class EpidemicApplication { public static void main(String[] args) { SpringApplication.run(EpidemicApplication.class, args); } }
实现上面添加@Cacheable
@Component @CacheConfig(cacheNames = "orgCode") public class OrgCodeDaoImpl implements OrgCodeDao { @Autowired private OrgCodeRepository orgCodeRepository; @Cacheable(value = "orgCodeFindAll") @Override public List<OrgCode> findAll() { return orgCodeRepository.findAll(); } }
实体类需要实现Serializable
public class OrgCode implements Serializable { }
resource目录下添加ehcache.xml,或者在application.properties文件中添加配置文件路径
spring.cache.ehcache.config=ehcache.xml
具体配置文件内容:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="50000" maxElementsOnDisk="100000" eternal="true" overflowToDisk="true" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="0" diskSpoolBufferSizeMB="50" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LFU" /> <cache name="orgCodeFindAll" maxElementsInMemory="1" maxElementsOnDisk="100000" eternal="false" overflowToDisk="true" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="21600" diskSpoolBufferSizeMB="50" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="FIFO" /> </ehcache> <!--
http://www.ehcache.org/ehcache.xsd 文件idea报错,可以访问地址,将文件下载后引入项目,或者忽略报错,测试报错不影响使用
diskStore:设置缓存文件 .data 的创建路径 user.home – 用户主目录; user.dir – 用户当前工作目录 java.io.tmpdir – 默认临时文件路径 name:缓存名称. maxElementsInMemory:缓存最大个数. maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大. eternal="false":缓存中对象是否为永久的,如果为true,超时设置将被忽略,对象从不过期 overflowToDisk="true":当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中. diskPersistent:是否缓存虚拟机重启期数据,默认为false. timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒),当缓存闲置 n 秒后销毁. 仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大. timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒),从开始创建的时间算起,当缓存存活 n 秒后销毁. 最大时间介于创建时间和失效时间之间.仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大. diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区. diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔.标识对象状态的线程多长时间运行一次. memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存. 默认策略是LRU(最近最少使用).你可以设置为FIFO(先进先出)或是LFU(较少使用). clearOnFlush:内存数量最大时是否清除. -->
1.
java.lang.IllegalArgumentException: Cannot find cache named ‘‘ for Builder[public java.util.List com.avivacofco.epidemic.hr.dao.impl.HrUserDaoImpl.findAll()] caches=[hrFindAll] | key=‘‘ | keyGenerator=‘‘ | cacheManager=‘‘ | cacheResolver=‘‘ | condition=‘‘ | unless=‘‘ | sync=‘false‘
解决方案:
在ehcache.xml中添加具体cache配置
其中重要的是两个属性 即 value 和 key,其中value必填,并且在ehcache.xml中进行配置,key可以为空,如为空,spring会使用默认策略生成key。
备注:key值逻辑:引用参数使用 #p0 是比较适配的一种方案
springboot添加cache缓存,并添加自定义缓存配置
原文:https://www.cnblogs.com/jerry-wei/p/12779256.html