方式一:老 不推荐
参考:https://www.cnblogs.com/lic309/p/4072848.html
/*************************第一种 引入 ehcache.xml 使用注解即可 **********************************
方式二:集成springboot
参考:https://www.jb51.net/article/135050.htm
2.1依赖:
<!-- 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>
2.2 入口类配置
加入注解 @EnableCaching
@SpringBootApplication
@EnableCaching
public class DemoApplication {
}
2.3 在src\main\resources目录下,添加ehcache.xml文件
ehcache.xml:(关于字段意思:参考:https://www.cnblogs.com/lic309/p/4072848.html)
<?xml version="1.0" encoding="UTF-8"?> updateCheck = "false"> <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下 --> <diskStore path = "java.io.tmpdir"/> <!-- 默认的管理策略 --> <defaultCache eternal = "false" maxElementsInMemory = "10000" overflowToDisk = "true" diskPersistent = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "120" diskExpiryThreadIntervalSeconds = "120" memoryStoreEvictionPolicy = "LRU"/> <!-- 此缓存最多可以存活timeToLiveSeconds秒,如果期间超过timeToIdleSeconds秒未访问,缓存失效 --> <cache name = "userCache" eternal = "false" maxElementsInMemory = "100" overflowToDisk = "false" diskPersistent = "false" timeToIdleSeconds = "120" timeToLiveSeconds = "180" memoryStoreEvictionPolicy = "LRU"/> <!-- maxElementsInMemory 内存中最大缓存对象数,看着自己的heap大小来搞 --> <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false --> <!-- maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大 --> <!-- overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后, 会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。--> <!-- diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。--> <!-- diskPersistent:是否缓存虚拟机重启期数据 --> <!-- diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒 --> <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后, 如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期, EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0, 则表示对象可以无限期地处于空闲状态 --> <!-- timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后, 如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期, EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0, 则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义 --> <!-- memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时, Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、 FIFO(先进先出)、LFU(最少访问次数)。--></ehcache>
2.4 application.application配置
# 配置ehcache缓存 spring.cache.type=ehcache # 指定ehcache配置文件路径 spring.cache.ehcache.config=classpath:/ehcache.xml
test:
import com.bbf.frame.Application;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class TestCache {
@Resource
private CacheManager cacheManager;
@Test
public void cacheTest() {
// 显示所有的Cache空间
System.out.println(StringUtils.join(cacheManager.getCacheNames(), ","));
Cache cache = cacheManager.getCache("userCache");
cache.put("key", "123");
System.out.println("缓存成功");
String res = cache.get("key", String.class);
System.out.println(res);
}
}
@Cacheable 拼接key eg:
@Cacheable(value = "page_user",key ="T(String).valueOf(#page).concat('-').concat(#pageSize)",unless = "#result=null")//由于page是int型,concat要求变量必须为String,所以强转一下
@Override
public List<SysUserEntity> page(int page, int pageSize) { return userMapper.page(page,pageSize); }
来源:https://www.cnblogs.com/lshan/p/10138967.html