【发布时间】:2019-01-09 14:22:43
【问题描述】:
我一直在尝试开发一个使用 Redis 缓存进行缓存的 Spring Boot 应用程序。我的应用程序适用于除更新方法之外的所有方法。这是我写的代码
@RestController
@RequestMapping(value = "users/")
public class UserController {
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@RequestMapping(value = "add/", method = RequestMethod.POST)
public void saveUser(@RequestBody User user){
log.info("Saving User information to the database...");
userService.saveUser(user);
}
@Cacheable(value = "user-get" , key = "#id")
@RequestMapping(value = "get/{id}/", method = RequestMethod.GET)
public User getUserById(@PathVariable Long id){
return userService.getUserById(id);
}
@Cacheable(value = "user-get-all")
@RequestMapping(value = "get/all/", method = RequestMethod.GET)
public List<User> getAllUser(){
return userService.getAllUsers();
}
@CacheEvict(value = "user-delete", key = "#id")
@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
public void deleteUser(@PathVariable Long id){
userService.deleteUser(id);
}
@CachePut(value = "user-update", key = "#user.id")
@RequestMapping(value = "update/{id}/", method = RequestMethod.POST)
public User updateUser(@PathVariable Long id, @RequestBody User user){
User newuser = new User();
newuser = userRepository.findById(id).get();
newuser.setUserName(user.getUserName());
newuser.setAge(user.getAge());
return userService.saveUser(newuser);
}
}
帮我解决问题
【问题讨论】:
标签: mysql spring-boot redis