1、SpringCache介绍
2、SpringCache常用注解
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 = "userCache" , key = "#user.id" )
@PostMapping
public User save ( User user) {
userService. save ( user) ;
return user;
}
@CacheEvict ( value = "userCache" , key = "#p0" )
@DeleteMapping ( "/{id}" )
public void delete ( @PathVariable Long id) {
userService. removeById ( id) ;
}
@CacheEvict ( value = "userCache" , key = "#result.id" )
@PutMapping
public User update ( User user) {
userService. updateById ( user) ;
return user;
}
@Cacheable ( value = "userCache" , key = "#id" , unless = "#result == null" )
@GetMapping ( "/{id}" )
public User getById ( @PathVariable Long id) {
User user = userService. getById ( id) ;
return user;
}
@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;
}
}
3、SpringCache在Redis中