苍穹外卖--springCache-@CachePut使用

This commit is contained in:
2025-11-24 11:14:30 +08:00
parent 2132000c93
commit 35e93d1449
8 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package com.inmind.controller;
import com.inmind.entity.User;
import com.inmind.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserMapper userMapper;
/*
*
* 业务在新增用户时将用户信息缓存到redis中
*
* Spring Expression LanguageSpEL
*
*/
@PostMapping
// @CachePut(cacheNames = "userCache",key = "#user.id")//如果使用springCache缓存数据key的生成的规则userCache::动态ID
@CachePut(cacheNames = "userCache",key = "#result.id")//对象导航result就表示该方法的返回值
// @CachePut(cacheNames = "userCache",key = "#p0.id")//p0:param0 参数列表中第一个参数
// @CachePut(cacheNames = "userCache",key = "#a0.id")//a0:argment0 参数列表中第一个参数
// @CachePut(cacheNames = "userCache",key = "#root.args[0].id")//root.args[0]:save方法的参数列表的第0个参数
public User save(@RequestBody User user){
userMapper.insert(user);
return user;
}
@DeleteMapping
public void deleteById(Long id){
userMapper.deleteById(id);
}
@DeleteMapping("/delAll")
public void deleteAll(){
userMapper.deleteAll();
}
@GetMapping
public User getById(Long id){
User user = userMapper.getById(id);
return user;
}
}