Spring Cache 学习笔记

Spring Cache 介绍

Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。
Spring Cahce 提供了一层抽象,底层可以切换不同的 cache 实现。具体就是通过 CacheManager 接口来统一不同的缓存技术。
CacheManager 是 Spring 提供的各种缓存技术抽象接口。

针对不同的缓存技术需要实现不同的 CacheManager:

CacheManager描述
EnCacheCacheManager使用 EhCache 作为缓存技术
GuavaCacheManager使用 Google 的 GuavaCache 作为缓存技术
RedisCachemanager使用 Redis 作为缓存技术

Spring Cache 注解

注解说明
@EnableCaching开启缓存注解功能
@Cacheable在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除

在 spring boot 项目中,使用缓存技术只需要在项目中导入相关缓存技术的依赖包,并在启动类上使用 @EnableCaching 开启缓存支持即可。

Spring Cache 使用

@Cacheable

@Cacheble 注解表示这个方法有了缓存的功能,方法的返回值会被缓存下来,下一次调用该方法前,会去检查是否缓存中已经有值,如果有就直接返回,不调用方法。如果没有,就调用方法,然后把结果缓存起来。这个注解一般用在查询方法上。

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 condition: 条件,满足条件才缓存数据
*/
@Cacheable(value = "userCache", key = "#user.id", condition = "#user != null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
	User user = userService.getByUd(id);
	return user;
}

/**
 @Cacheable: 在方法执行前 spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 condition: 条件,满足条件才缓存数据
 unless: 满足条件则不缓存
*/
@Cacheable(value = "userCache", key = "#user.id", unless= "#user == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
	User user = userService.getByUd(id);
	return user;
}

/**
 动态生成 key
*/
@Cacheable(value = "userCache", key = "#user.id + '_' + #user.name")
@GetMapping("/list")
public List<User> list(User user){
	LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
	wrapper.eq(user.getId() != null, User::getId, user.getId());
	List<User> list = userService.list(wrapper);
	return list;
}

@CachePut

加了 @CachePut 注解的方法,会把方法的返回值 put 到缓存里面缓存起来,供其它地方使用。它通常用在新增方法上。

/**
 CachePut: 将方法返回值放入缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
*/
@CachePut(value = "userCache", key = "#user.id")
@PostMapping
public User save(User user){
	userService.save(user);
	return user;
}

@CacheEvict

使用了 @CacheEvict 注解的方法,会清空指定缓存。一般用在更新或者删除的方法上。

/**
 CacheEvict: 清理指定缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
*/
@CacheEvict(value = "userCache", key = "#id")
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
	userService.removeById(id);
}

/**
 CacheEvict: 清理指定缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
*/
@CacheEvict(value = "userCache", key = "#user.id")
@PutMapping
public User update(User user){
	userService.updateById(user);
	return user;
}

/**
 CacheEvict: 清理指定缓存
 value: 缓存的名称,每个缓存名称下面可以有多个 key
 key: 缓存的 key
 allEntries : 清理所有缓存属性,默认为 false
*/
@CacheEvict(value = "userCache", allEntries = true)
@DeleteMapping
public String update(@RequestParam List<Long> ids){
	// removeWithDish() 自定义方法,根据 id 批量删除 Dish
	userService.removeWithDish(ids);
	return "删除成功";
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值