Spring Cache
Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。Spring Cache 提供了一层抽象,底层可以切换不同的缓存实现,例如:
EHCache
KCaffeine
Redis
CachePut:
@PostMapping
@Cacheput(cacheNames ="userCache",key="#user,id")//如果使用spring cache级存数据,key的生@usercache::2//@CachePut(cacheNames="userCache",key=“#result.id")//对象导航
//@CachePut(cacheNames = “userCache",key =“#p0. id")
//@CachePut(cacheNames "userCache",key=“#a.id”)
public User save(@RequestBody User user){
userMapper.insert(user);
return user;
}
Cacheable:
@GetMapping
@Cacheable(cacheName="usercache",key="#id")
public User getById(Long id){
User user= userMapper.getById(id);
return user;
}
CacheEvict:
删除对应所有的缓存数据
根据id删除对应的缓存数据
@DeleteMapping(/delAll)
@CacheEvic(cacheNames="userCache",allEntries = true)
public void deleteAll(){
userMapper.deleteAll();
}
@DeleteMapping
@CacheEvict(cacheNames = "usercache",key = “#id")
public void deleteById(Long id){
userMapper.deleteById(id);
}