spring boot 整合redis和ehcache并实现缓存过期设置
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!-- 测试 -->
<cache name="myCache"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
>
</cache>
</ehcache>
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:ehcache.xml
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
spring.redis.database=0
@EnableCaching // 开始缓存功能
//@CacheConfig(cacheNames="myCache")
@Service
public class CacheService {
@Cacheable(value = "myCache",key="#p0")//#:取值方式 p:参数的意思parameter 0:下标
public String test1(Integer id) {
System.out.println(id+"test1");
return id.toString();
}
@Cacheable(value = "myCache",key="#id")
public String test2(Integer id) {
System.out.println(id+"test2");
return id.toString();
}
@Cacheable(value = "myCache",key="#id")
public String test3(Integer id) {
System.out.println(id+"test3");
return id.toString();
}
}