使用redis实现一个购物车功能

  redis = new Redis;
        $this->redis->connect('127.0.0.1', 6379);
    }

    public function addToCart($gid, $cartNum=1)
    {

        session_start();
        if ($gid <= 0) {

            throw new Exception("请输入商品ID");

        }

        //根据商品ID查询商品数据
        $goodData = $this->goodsData($gid);

        $key = 'cart:'.session_id().':'.$gid;//id 说明:1、不仅仅要区分商品  2、 用户

        // $data = $this->redis->hget($key, 'id');
        $data = $this->redis->exists($key);


        //判断购物车中是否有无商品,然后根据情况加入购物车
        if (!$data) {

            //购物车之前没有对应的商品的

            //购物车的商品数量
            $goodData['num'] = $cartNum;

            //将商品数据存放到redis中hash
            $this->redis->hmset($key, $goodData);


            $key1 = 'cart:ids:set:'.session_id();

            //将商品ID存放集合中,是为了更好将用户的购物车的商品给遍历出来
            $this->redis->sadd($key1, $gid);

        } else {

            //购物车有对应的商品,只需要添加对应商品的数量
            $originNum = $this->redis->hget($key, 'num');

            //原来的数量加上用户新加入的数量
            $newNum = $originNum + $cartNum;

            $this->redis->hset($key, 'num', $newNum);


        }

    }

    //显示用户购物车的所有商品
    public function showCartList()
    {

        session_start();

        $sessId = session_id();


        $key = 'cart:ids:set:'.session_id();

        //先根据集合拿到商品ID
        $idArr =  $this->redis->sMembers($key);


        for ($i=0; $i';
            $list[] = $this->redis->hGetAll($k);
        }

        var_dump($list);

    }

    public function goodsData($gid)
    {

        $goodsData = array(

            1 => array(
                'id' => 1,
                'gname' => 'xxoo',
                'price' => '1.5'
            ),

            2 => array(
                'id' => 2,
                'gname' => 'xxoo22',
                'price' => '221.5'
            ),
            3 => array(
                'id' => 3,
                'gname' => 'xxoo33',
                'price' => '331.5'
            ),
            4 => array(
                'id' => 4,
                'gname' => 'xxoo44',
                'price' => '4441.5'
            ),    
        );

        return $goodsData[$gid];
    }
}


$ceshi = new Cart();
$ceshi->addToCart(2,2);

echo session_id();

你可能感兴趣的:(redis)