利用redis集群存储用户登录后的购物车信息,利用cookie存储用户未登录状态下的cookie
前言
本文使用java实现
一、详情解释
1、购物车在未登录的情况下,应为没有用户的信息,所以这个时候如果想要存储购物车信息,只能保存在浏览器客户端。有两种选择cookies、localstorange,其中cookie是每个浏览器都有的,既可以通过js操作,也可以随着请求传递到服务器端,在java里面可以通多request和response对其进行操作,可以设置cookie的有限期、作用域、作用路径等。但是缺点是也有可能被禁用,不能跨域,存储量小。localstorage是HTML5的本地存储,存储量可以达到5M,本身也不支持跨域,但是可以借助其他的方式进行解决,不能随着请求传递地到后台用java进行操作。我们这次使用cookie实现购物车的匿名情况下的添加购物车操作。
2、cookie是以key-value的方式存储,因为没有用户信息,说一我们一般使用“cartList”作为key,以购物车明细列表作为value,用户未登录可以对购物车进行操作,直接对cookie进行操作即可。但是在用户登录以后,我们必须要实现的两个功能:a.cookie购物车域本地购物车进行合并,b.购物车在不同的设备上进行同步。
3、登录以后因为购物车数据一直都需要修改,会对服务器造成较大的压力,所以购物车一般存储在redis缓存中,userId或者loginname作为存储的可以,购物车的详细列表作为value。用户未登录,登录后调用同步购物车接口,将本地购物车商品列表上传到服务器,同时根据userId或者loginname从redis中获取购物车商品详细列表,然后让本地购物车列表与rides购物列表进行对比,如商品在redis购物车列表中存在则商品数量增加,如若商品不存在,则添加到redis列表,然后更新购物车列表,清空本地购物车列表缓存。同时,后台还需要负责购物车数量增减,购物车商品山粗和、购物车清空接口,如果购物车商品列表中某个商品被全部删除,则购物车选项从列表中一处,如果购物车中商品全部一处,要将购物车列表整个清空。
二、核心源码
package com.xja.controller;
import com.alibaba.fastjson.JSONObject;
import com.xja.pojo.Cart;
import com.xja.pojo.Product;
import com.xja.pojo.User;
import com.xja.service.CartService;
import com.xja.service.ProductService;
import com.xja.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/cart")
public class CartController {
@Autowired
private CartService cartService;
@Value("${cart_cookie_name}")
private String cookieName;
@Autowired
private ProductService productService;
@RequestMapping("/add")
public @ResponseBody Map<String,Object> addCart(Integer id,HttpServletRequest request, HttpServletResponse response){
Map<String,Object> map = new HashMap<>();
Product product = productService.selectProductById(id);
User userInfo = (User)request.getSession().getAttribute("user");
//System.out.println(cookieName+"77777777777777777777777777777777");
if(userInfo==null){
//System.out.println("000-------------");
String cookies = CookieUtil.getCookieValue(request, cookieName, true);
//System.out.println("cookies-----------"+cookies);
if(cookies != null&&!"".equals(cookies)){
//System.out.println("111-------------");
List<Cart> cartListCookie = JSONObject.parseArray(cookies, Cart.class);
for(int i=0;i<cartListCookie.size();i++){
//System.out.println(cartListCookie.get(i));
if(product.getId().equals(cartListCookie.get(i).getProId())) {
//System.out.println("222-----------");
cartListCookie.get(i).setNum(cartListCookie.get(i).getNum()+1);
cookies = JSONObject.toJSONString(cartListCookie);
CookieUtil.setCookie(request, response, cookieName, cookies, 3600 * 24 * 7, true);
map.put("code",200);
return map;
}
}
Cart cart = new Cart();
cart.setNum(1);
cart.setProId(product.getId());
cart.setProfullname(product.getProfullname());
cart.setProname(product.getProname());
cart.setProprice(product.getProprice());
cart.setProimg(product.getProimg());
cartListCookie.add(cart);
String jsonCookies=JSONObject.toJSONString(cartListCookie);
CookieUtil.setCookie(request,response,cookieName,jsonCookies,3600*24*7,true);
map.put("code",200);
return map;
}else {
Cart cart = new Cart(