个人商城系统,后台对购物车数据进行“半持久化”。
因为购物车增删改的操作很频繁,如果使用mysql效率会很低,
所以使用redis进行存储。如果担心redis会挂,可使用redis集群,还是很靠谱的
参考上一篇redis进行缓存
购物车redis缓存设置:
CartPrefix.class
public class CartPrefix extends BasePrefix{
public CartPrefix(int expireSeconds, String prefix) {
super(expireSeconds, prefix);
}
/**
* 设置购物车缓存
*/
public static CartPrefix getCartList= new CartPrefix(0,"cart");
}
购物车service层代码:
CartService.class
@Service
@Slf4j
public class CartServiceImpl implements CartService {
@Autowired
RedisService redisService;
@Autowired
ProductInfoDao productInfoDao;
@Override
public int addCart(String userId, String productId, int num) {
//key为 userId_cart,校验是否已存在
Boolean exists = redisService.existsValue(CartPrefix.getCartList,userId,productId);
if (exists){
//获取现有的购物车中的数据
String json = redisService.hget(CartPrefix.getCartList,userId,productId);
if (json !=null){
//转换为java实体类
CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json),CartDto.class);
cartDto.setProductNum(cartDto.getProductNum()+num);
redisService.hset(CartPrefix.getCartList,userId,productId,JSON.toJSON(cartDto).toString());
}else {
return 0;
}
return 1;
}
//根据商品id获取商品
ProductInfo productInfo = productInfoDao.findProductById(productId);
if (productInfo==null){
return 0;
}
//设置购物车值
CartDto cartDto = new CartDto();
cartDto.setProductId(productId);
cartDto.setProductName(productInfo.getProductName());
cartDto.setProductIcon(productInfo.getProductIcon());
cartDto.setProductPrice(productInfo.getProductPrice());
cartDto.setProductStatus(productInfo.getProductStatus());
cartDto.setProductNum(num);
cartDto.setCheck("1");
redisService.hset(CartPrefix.getCartList,userId,productId,JSON.toJSON(cartDto).toString());
return 1;
}
/**
* 展示购物车
* @param userId
* @return
*/
@Override
public List<CartDto> getCartList(String userId) {
List<String> jsonList = redisService.hvals(CartPrefix.getCartList,userId);
List<CartDto> cartDtoList = new LinkedList<>();
for (String json:jsonList){
CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json),CartDto.class);
cartDtoList.add(cartDto);
}
return cartDtoList;
}
/**
* 更新数量
* @param userId
* @param productId
* @param num
* @return
*/
@Override
public int updateCartNum(String userId, String productId, int num) {
String json = redisService.hget(CartPrefix.getCartList,userId,productId);
if (json==null){
return 0;
}
CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json),CartDto.class);
cartDto.setProductNum(num);
redisService.hset(CartPrefix.getCartList,userId,productId,JSON.toJSON(cartDto).toString());
return 1;
}
/**
* 全选商品
* @param userId
* @param checked
* @return
*/
@Override
public int checkAll(String userId, String checked) {
//获取商品列表
List<String> jsonList = redisService.hvals(CartPrefix.getCartList,userId);
for (String json:jsonList){
CartDto cartDto = JSON.toJavaObject(JSONObject.parseObject(json),CartDto.class);
if ("true".equals(checked)){
cartDto.setCheck("1");
}else if ("false".equals(checked)){
cartDto.setCheck("0");
}else {
return 0;
}
redisService.hset(CartPrefix.getCartList,userId,cartDto.getProductId(),JSON.toJSON(cartDto).toString());
}
return 1;
}
/**
* 删除商品
* @param userId
* @param productId
* @return
*/
@Override
public int delCartProduct(String userId, String productId) {
redisService.hdel(CartPrefix.getCartList,userId,productId);
return 1;
}
/**
* 清空购物车
* @param userId
* @return
*/
@Override
public int delCart(String userId) {
redisService.delete(CartPrefix.getCartList,userId);
return 1;
}
}
购物车接口实现:
CartController.class
@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {
@Autowired
CartService cartService;
/**
* 加入购物车
* @param reqMap
* @param user
* @return
*/
@PostMapping(value = "/add")
@Autorization
public Object addCart(@RequestBody Map<String, Object> reqMap, @CurrentUser User user){
//获取登陆用户的userId
String userId = RequestUtil.getMapString(user.getId());
String productId = RequestUtil.getMapString(reqMap.get("product_id").toString());
String numString = RequestUtil.getMapString(reqMap.get("product_num").toString());
Integer num = Integer.parseInt(numString);
//加入购物车
int effectNum = cartService.addCart(userId,productId,num);
if (effectNum<=0){
return ResultUtil.fail(ResultEnum.ADD_CART_ERROR);
}
return ResultUtil.ok(ResultEnum.ADD_CART_SUCCESS.getMessage());
}
@GetMapping(value = "/getCartList")
@Autorization
public Object getCartList(@CurrentUser User user){
String userId = RequestUtil.getMapString(user.getId());
List<CartDto> cartDtoList = cartService.getCartList(userId);
return ResultUtil.ok(cartDtoList);
}
@PostMapping(value = "/updateCartNum")
@Autorization
public Object updateCartNum(@RequestBody Map<String,Object> reqMap,@CurrentUser User user){
String userId = RequestUtil.getMapString(user.getId());
String productId = RequestUtil.getMapString(reqMap.get("product_id").toString());
String numString = RequestUtil.getMapString(reqMap.get("product_num").toString());
Integer num = Integer.parseInt(numString);
int effectNum = cartService.updateCartNum(userId,productId,num);
if (effectNum <=0){
return ResultUtil.fail();
}
return ResultUtil.ok();
}
@PostMapping(value = "/checkAll")
@Autorization
public Object checkAll(@RequestBody Map<String,Object> reqMap,@CurrentUser User user){
String userId = RequestUtil.getMapString(user.getId());
String check = RequestUtil.getMapString(reqMap.get("check").toString());
int effectNum = cartService.checkAll(userId,check);
if (effectNum <=0){
return ResultUtil.fail();
}
return ResultUtil.ok();
}
@PostMapping("/delCartProduct")
@Autorization
public Object delCartProduct(@RequestBody Map<String,Object> reqMap,@CurrentUser User user){
String userId = RequestUtil.getMapString(user.getId());
String productId = RequestUtil.getMapString(reqMap.get("product_id").toString());
int effectNum = cartService.delCartProduct(userId,productId);
if (effectNum <=0){
return ResultUtil.fail();
}
return ResultUtil.ok();
}
@PostMapping("/delCart")
@Autorization
public Object delCart(@CurrentUser User user){
String userId = RequestUtil.getMapString(user.getId());
int effectNum = cartService.delCart(userId);
if (effectNum <=0){
return ResultUtil.fail();
}
return ResultUtil.ok();
}
}
商城项目地址 https://github.com/627886474/sneaker
欢迎start,如有不足,还请指教。