Spring Boot整合@Cacheable注解使用

mac2024-02-02  52

1 @Cacheable @Cacheable 作用:把方法的返回值添加到Ehcache 中做缓存 Value 属性:指定一个Ehcache 配置文件中的缓存策略,如果么有给定value,name 则 表示使用默认的缓存策略。

<!-- 自定义缓存策略 --> <cache name="users" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </cache>

Key 属性:给存储的值起个名称。在查询时如果有名称相同的,那么则知己从缓存中将 数据返回

@Override @Cacheable(value="users",key="#pageable.pageSize") public Page<Users> findUserByPage(Pageable pageable) { return this.usersRepository.findAll(pageable); } @Test public void testFindUserByPage(){ Pageable pageable = new PageRequest(0, 2); //第一次查询 System.out.println(this.usersService.findUserByPage(pageable).getTotalElements()); //第二次查询 System.out.println(this.usersService.findUserByPage(pageable).getTotalElements()); //第三次查询 pageable = new PageRequest(1, 2); System.out.println(this.usersService.findUserByPage(pageable).getTotalElements()); }

 

最新回复(0)