购物车:
一, 将未登录的购物车存在redis 中!
实现思路:
1.设定一个UUID保存在cookie 中
private String getUUID(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
boolean isMatch=false;
if(cookies!=null && cookies.length>0){
for (Cookie cookie : cookies) {
if ("user-Key".equals(cookie.getName())){
String value = cookie.getValue();
userKey=value;
isMatch=true;
}
}
}
// 没有userKey 则创建一个uuId
if (!isMatch){
userKey = UUID.randomUUID().toString().replace("-","");
}
return userKey;
}
2.编写接口
void addToCart(String skuId, Integer skuNum,String userKey);
3.实现类
@Override
public void addToCart(String skuId, Integer skuNum,String userKey) {
Jedis jedis = redisUtil.getJedis();
String userCartKey = CartConst.USER_KEY_PREFIX+userKey+CartConst.USER_CART_KEY_SUFFIX;
// 获取缓存中的所有数据
Map<String, String> map = jedis.hgetAll