hash 表 电商购物车缓存

   $id = $_GPC['id'];
        $product= pdo_fetch("select id,title,marketprice,total from ".tablename('ewei_shop_goods')." where id='$id'");

       //使用hashtable存储
        $redis= new Redis();
        $redis->connect('127.0.0.1',6379);

        if($p=$redis->hget('cart','productid_'.$id)){
            $p= json_decode($p,true);
            $p['num']++;
            $redis->hset('cart','productid_'.$id,json_encode($p));
        }else{
            $product['num'] =1;
            $redis->hset('cart','productid_'.$id,json_encode($product));
        }

        var_dump($redis->hGetAll('cart'));
//session主流方式购物车的存储
  $id = $_GPC['id'];
        $product= pdo_fetch("select id,title,marketprice,total from ".tablename('ewei_shop_goods')." where id='$id'");
      session_start();
   //     var_dump($_SESSION['cart'][$id]);exit;


        if(!empty($_SESSION['cart']) && !empty($_SESSION['cart'][$id]))
        {
            echo 1122;
            $_SESSION['cart'][$id]['num']++;

        }else{
            $product['num'] = 1;

            $_SESSION['cart'][$id]= $product;

        }

 

posted @ 2020-12-09 10:45  大熊童鞋  阅读(211)  评论(0编辑  收藏  举报