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