基于redis购物车java代码_如何用java做一个购物车,用redis来缓存商品id?

Redis购物车

个人商城系统,后台对购物车数据进行“半持久化”。

因为购物车增删改的操作很频繁,如果使用mysql效率会很低,

所以使用redis进行存储。如果楼主担心redis会挂,可使用redis集群,还是很靠谱的

购物车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 getCartList(String userId) {

List jsonList = redisService.hvals(CartPrefix.getCartList,userId);

List 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 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值