一、注解配置与EhCache使用。
1、pom文件引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> |
---|
2、新建ehcache.xml 文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir/Tmp_EhCache" />
<!-- 默认配置 --> <defaultCache maxElementsInMemory="5000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" memoryStoreEvictionPolicy="LRU" overflowToDisk="false" /> <cache name="baseCache" maxElementsInMemory="10000" maxElementsOnDisk="100000" /> </ehcache> |
3、 配置信息介绍
|
---|
4、代码使用Cacheable
@CacheConfig(cacheNames = "baseCache") public interface UserMapper { @Select("select * from users where name=#{name}") @Cacheable UserEntity findName(@Param("name") String name); } |
---|
5、清除缓存
@Autowired private CacheManager cacheManager; @RequestMapping("/remoKey") public void remoKey() { cacheManager.getCache("baseCache").clear(); }
|