springboot缓存测试

首先打印sql日志:
在application.properties配置文件里加上logging.level.com.neo.teamManagement.teamOverview.mapper=debug

配置pom.xml加入缓存依赖:

	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-cache</artifactId>
	</dependency>
	程序入口加上注解@EnableCaching

缓存对应的实体类序列化:implements Serializable
在缓存的需要缓存的service 加上@CacheConfig(cacheNames = “initAccountPeriod”)
在方法上加入注解@Cacheable(cacheNames = {“getGroupOverviewforNo”})缓存有就在缓存取,没有就放到缓存
在方法上加入注解@CachePut(key = “#result.id”)同步更新缓存
@CacheEvict:缓存清除,key:指定要清除的数据,allEntries = true : 指定清除这个缓存中的所有数据,beforeInvocation=fales: 缓存的清除是否在方法之前执行,默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除,beforeInvocation=true 代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除。

猜你喜欢

转载自blog.csdn.net/weixin_44310277/article/details/87932838