今天学到一个新知识:SpringCache,刚开始项目是使用Redis来进行缓存数据,但是当进行数据库操作的时候,通常也需要对Redis缓存的数据进行操作,这就需要写很多的代码量。
然后就了解到了SpringCache,可以通过注解来把数据添加到缓存或者删除缓存中的数据。
<!--springboot项目进行web开发必不可少的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
package com.itheima;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@Slf4j
@SpringBootApplication
@EnableCaching
public class CacheDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CacheDemoApplication.class,args);
log.info("项目启动成功...");
}
}
package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private CacheManager cacheManager;
@Autowired
private UserService userService;
/**
* CachePut:将方法返回值放入缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
* @param user
* @return
*/
@CachePut(value = "userCache",key = "#user.id")
@PostMapping
public User save(User user){
userService.save(user);
return user;
}
/**
* CacheEvict:清理指定的缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
*/
@CacheEvict(value = "userCache",key = "#id")
// @CacheEvict(value = "userCache",key = "#p0") //第一个参数
// @CacheEvict(value = "userCache",key = "#root.args[0]") //第一个参数
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
userService.removeById(id);
}
/**
*
* @param user
* @return
*/
@CacheEvict(value = "userCache",key = "#user.id")
// @CacheEvict(value = "userCache",key = "#p0.id") //获取user中的id
// @CacheEvict(value = "userCache",key = "#root.args[0].id") //获取user中的id
// @CacheEvict(value = "userCache",key = "#result.id") //获取user中的id
@PutMapping
public User update(User user){
userService.updateById(user);
return user;
}
/**
* Cacheable:先看缓存中有没有数据,如果有就不再执行sql
* condition:条件,满足指定条件时才缓存数据
* unless:满足条件则不缓存
* @param id
* @return
*/
// @Cacheable(value = "userCache",key = "#id",condition = "#result != null ")
@Cacheable(value = "userCache",key = "#id",unless = "#result == null ")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){
User user = userService.getById(id);
return user;
}
/**
* Cacheable
* @param user
* @return
*/
@Cacheable(value = "userCache",key = "#user.id + #user.name")
@GetMapping("/list")
public List<User> list(User user){
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(user.getId() != null,User::getId,user.getId());
queryWrapper.eq(user.getName() != null,User::getName,user.getName());
List<User> list = userService.list(queryWrapper);
return list;
}
}