- @Cacheable
- @CacheEvict
一 @Cacheable
1.1 @Cacheable作用:把方法的返回值添加到Ehcache缓存中。
1.2 value属性:指定一个Ehcache配置文件中的缓存策略,如果有给定一个value,name则表示使用的默认的缓存策略。
如:
@Override
// @Cacheable:对当前查询的对象做缓存处理 value指定ehcache.xml中的哪个配置
@Cacheable(value="users")
public Users findUserById(Integer id) {
return usersRepository.findById(id).get();
}
<!-- 自定义缓存策略,name不能重复 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
1.3 key属性:给存储的值起个名称,在查询时,如果有名称相同的,那么则直接从缓存中将数据返回。
UsersServiceImpl.j