三十二 SpringBoot整合@Cacheable注解使用

  • @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();
	}
    
    
        
    

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

UsersServiceImpl.java

	@Override
	@Cacheable(value="users", key="#pageable.pageSize")
	public Page findUserByPage(Pageable pageable) {
		return usersRepository.findAll(pageable);
	}

UsersServiceTest.java

	/**
	 	2019-04-12 17:58:52.047  INFO 10904 --- [           main] com.bjsxt.service.UsersServiceTest       : Started UsersServiceTest in 15.43 seconds (JVM running for 18.177)
		Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
		Users [id=1, name=guozi44, age=18, address=地球]
		Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
		Users [id=1, name=guozi44, age=18, address=地球]
		2019-04-12 17:58:52.573  INFO 10904 --- [       Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
		从以上结果可看出,会执行两次sql
		解决:在UsersServiceImpl.findUserById方法添加@Cacheable(value="users")注解
		二次改后:
		Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
		Users [id=1, name=guozi44, age=18, address=地球]
		Users [id=1, name=guozi44, age=18, address=地球]
		只会查一次,两次输出结果一致,但有异常,异常信息为:java.io.NotSerializableException: com.bjsxt.pojo.Users
		解决:Users实现Serializable接口即可
	 */
	@Test
	public void testFindUserById() {
		// 第一次查询
		Users users = usersService.findUserById(1);
		System.out.println(users);
		
		// 第二次查询
		Users users2 = usersService.findUserById(1);
		System.out.println(users2);
	}

 

你可能感兴趣的:(SpringBoot)