Rumah  >  Artikel  >  php教程  >  PDO和消息队列的一点个人理解

PDO和消息队列的一点个人理解

WBOY
WBOYasal
2016-11-30 23:59:341383semak imbas

  什么是消息队列,百度百科说,···消息队列····是在消息的传输过程中保存消息的容器。

  看着网上林林总总的文章,都说是为了应对高并发,处理数据量超级大的一种数据容器,也可以说是利用各种方式,先把数据存储在一个···容器···中,然后,再慢慢从这个容器中获取数据,实现·····异步操作数据库·····的方式,以便降低数据库的压力。

  不管消息队列是什么,个人姑且认为它就是一种存储数据的···容器···,就这么简单明了,呵呵,因为本人对消息队列这东西研究的比较肤浅。

  不过,在这里,我还是要卖弄一下一个个人的小实验代码,呵呵,假若下面的东西与消息队列相离太远,那么请看官可别见怪哦,因为我上面已经声明的很清楚了,本人对这东西还是不甚了解,只不过是工作之余卖弄一下‘文采’而已,所以,假如真有误解消息队列这个东西,我只能说声,请你指正我的错误,让笔者迷途知返。

  先给出连接mysql数据库配置文件mysql.ini的内容:

<span style="color: #800000; font-weight: bold">[</span><span style="color: #800000">database</span><span style="color: #800000; font-weight: bold">]</span><span style="color: #000000">
driver </span>=<span style="color: #000000"> mysql
host </span>= 127.0.0.1<span style="color: #000000">
port </span>= 3306<span style="color: #000000">
dbname </span>=<span style="color: #000000"> mysql
username </span>=<span style="color: #000000"> root
password </span>= 1234
<span style="color: #800000; font-weight: bold">[</span><span style="color: #800000">options</span><span style="color: #800000; font-weight: bold">]</span><span style="color: #000000">
PDO::MYSQL_ATTR_INIT_COMMAND </span>= set names utf8

  数据表文件queue.sql的内容:

<span style="color: #008080">--</span><span style="color: #008080"> 消息队列测试</span><span style="color: #008080">
--</span><span style="color: #008080"> 商品表</span>
<span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span><span style="color: #000000"> goods(
id </span><span style="color: #0000ff">int</span> unsigned <span style="color: #0000ff">primary</span> <span style="color: #0000ff">key</span><span style="color: #000000"> auto_increment,
goodsname </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">40</span>) <span style="color: #808080">not</span> <span style="color: #0000ff">null</span> comment <span style="color: #ff0000">'</span><span style="color: #ff0000">商品名</span><span style="color: #ff0000">'</span><span style="color: #000000">,
price </span><span style="color: #0000ff">decimal</span>(<span style="color: #800000; font-weight: bold">9</span>,<span style="color: #800000; font-weight: bold">2</span>) <span style="color: #808080">not</span> <span style="color: #0000ff">null</span> comment <span style="color: #ff0000">'</span><span style="color: #ff0000">价格</span><span style="color: #ff0000">'</span><span style="color: #000000">,
category_id </span><span style="color: #0000ff">tinyint</span> unsigned <span style="color: #808080">not</span> <span style="color: #0000ff">null</span> comment <span style="color: #ff0000">'</span><span style="color: #ff0000">商品类别id</span><span style="color: #ff0000">'</span><span style="color: #000000">,
sort </span><span style="color: #0000ff">tinyint</span> comment <span style="color: #ff0000">'</span><span style="color: #ff0000">商品排序</span><span style="color: #ff0000">'</span><span style="color: #000000">,
description </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">255</span>) comment <span style="color: #ff0000">'</span><span style="color: #ff0000">商品描述</span><span style="color: #ff0000">'</span><span style="color: #000000">,
remark </span><span style="color: #0000ff">varchar</span>(<span style="color: #800000; font-weight: bold">255</span>) comment <span style="color: #ff0000">'</span><span style="color: #ff0000">备注</span><span style="color: #ff0000">'</span><span style="color: #000000">
)engine InnoDB </span><span style="color: #0000ff">default</span> charset<span style="color: #808080">=</span><span style="color: #000000">utf8;
</span><span style="color: #0000ff">insert</span> <span style="color: #0000ff">into</span> goods <span style="color: #0000ff">values</span>(<span style="color: #0000ff">default</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">iPhone5s</span><span style="color: #ff0000">'</span>,<span style="color: #800000; font-weight: bold">5999</span>,<span style="color: #800000; font-weight: bold">1</span>,<span style="color: #800000; font-weight: bold">1</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">美国入口贵重电子产品</span><span style="color: #ff0000">'</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">请多多销售</span><span style="color: #ff0000">'</span>),(<span style="color: #0000ff">default</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">宏基手提电脑1</span><span style="color: #ff0000">'</span>,<span style="color: #800000; font-weight: bold">4888</span>,<span style="color: #800000; font-weight: bold">3</span>,<span style="color: #800000; font-weight: bold">1</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">价格划算好电脑</span><span style="color: #ff0000">'</span>,<span style="color: #0000ff">null</span>),(<span style="color: #0000ff">default</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">f-30绒衣</span><span style="color: #ff0000">'</span>,<span style="color: #800000; font-weight: bold">200</span>,<span style="color: #800000; font-weight: bold">2</span>,<span style="color: #800000; font-weight: bold">1</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">以纯白色厚皮羽绒服</span><span style="color: #ff0000">'</span>,<span style="color: #0000ff">null</span>),(<span style="color: #0000ff">default</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">n-100鞋子</span><span style="color: #ff0000">'</span>,<span style="color: #800000; font-weight: bold">300</span>,<span style="color: #800000; font-weight: bold">4</span>,<span style="color: #800000; font-weight: bold">1</span>,<span style="color: #ff0000">'</span><span style="color: #ff0000">Nick品牌</span><span style="color: #ff0000">'</span>,<span style="color: #0000ff">null</span>);

  下面是简单的消息队列实现PDO商品入库的代码:

  郑重提醒:请认真看我的注释,请认真看我的注释,请认真看我的注释。重要的信息要说三遍,呵呵!

<span style="color: #000000">php
namespace Home;
</span><span style="color: #008080">header</span>('content-type:text/html;charset=utf-8;'<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">引入PDO类</span>
<span style="color: #0000ff">use</span><span style="color: #000000"> \PDO;
</span><span style="color: #0000ff">use</span><span style="color: #000000"> \PDOStatement;
</span><span style="color: #0000ff">use</span><span style="color: #000000"> \PDOException;
</span><span style="color: #008000">/*</span><span style="color: #008000">简单消息队列</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">class</span><span style="color: #000000"> Queue{
    </span><span style="color: #008000">//</span><span style="color: #008000">容器可以是私有,最好是公有</span>
    <span style="color: #0000ff">static</span> <span style="color: #0000ff">public</span> <span style="color: #800080">$container</span>=<span style="color: #0000ff">array</span><span style="color: #000000">();
    </span><span style="color: #008000">//</span><span style="color: #008000">把PDO对象存放到属性中是为了跨方法使用</span>
    <span style="color: #0000ff">static</span> <span style="color: #0000ff">private</span> <span style="color: #800080">$pdo</span><span style="color: #000000">;
    </span><span style="color: #008000">//</span><span style="color: #008000">构造函数实现初始化PDO连接数据库</span>
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$file</span> = './mysql.ini'){<span style="color: #008000">//</span><span style="color: #008000">mysql数据库的配置文件,./mysql.ini与这个类是同一个目录
        //parse_ini_file函数作用,解析并获取ini文件的参数,加true是二维数组</span>
        <span style="color: #800080">$dbini</span>        = <span style="color: #008080">parse_ini_file</span>(<span style="color: #800080">$file</span>,<span style="color: #0000ff">true</span><span style="color: #000000">);
        </span><span style="color: #800080">$driver</span>        = <span style="color: #800080">$dbini</span>['database']['driver'<span style="color: #000000">];
        </span><span style="color: #008000">//</span><span style="color: #008000">这里的数据库是mysql的总数据库</span>
        <span style="color: #800080">$dbname</span>        = <span style="color: #800080">$dbini</span>['database']['dbname'<span style="color: #000000">];
        </span><span style="color: #800080">$host</span>        = <span style="color: #800080">$dbini</span>['database']['host'<span style="color: #000000">];
        </span><span style="color: #800080">$port</span>        = <span style="color: #800080">$dbini</span>['database']['port'<span style="color: #000000">];
        </span><span style="color: #008000">//</span><span style="color: #008000">构造PDO连接数据库的第一个参数,这个参数这么设置纯属是PDO类的语法,不懂的可以百度一下,这里就不做详解了</span>
        <span style="color: #800080">$dsn</span> = <span style="color: #800080">$driver</span>.':'.'dbname='.<span style="color: #800080">$dbname</span>.';host='.<span style="color: #800080">$host</span>.';port='.<span style="color: #800080">$port</span><span style="color: #000000">;
        </span><span style="color: #008000">//</span><span style="color: #008000">其他连接数据库参数</span>
        <span style="color: #800080">$username</span>    = <span style="color: #800080">$dbini</span>['database']['username'<span style="color: #000000">];
        </span><span style="color: #800080">$password</span>    = <span style="color: #800080">$dbini</span>['database']['password'<span style="color: #000000">];
        </span><span style="color: #800080">$options</span>    = <span style="color: #800080">$dbini</span>['options'];<span style="color: #008000">//</span><span style="color: #008000">初始化设置PDO选项</span>
        <span style="color: #0000ff">try</span><span style="color: #000000">{
            self</span>::<span style="color: #800080">$pdo</span> = <span style="color: #0000ff">new</span> PDO(<span style="color: #800080">$dsn</span>,<span style="color: #800080">$username</span>,<span style="color: #800080">$password</span>,<span style="color: #800080">$options</span><span style="color: #000000">);
            </span><span style="color: #008000">//</span><span style="color: #008000">设置PDO属性。抛出错误报告</span>
            self::<span style="color: #800080">$pdo</span>->setAttribute(PDO::ATTR_ERRMODE,PDO::<span style="color: #000000">ERRMODE_EXCEPTION);
            </span><span style="color: #0000ff">try</span><span style="color: #000000">{
                </span><span style="color: #008000">//</span><span style="color: #008000">选择商品数据表所在的数据库</span>
                self::<span style="color: #800080">$pdo</span>-><span style="color: #008080">exec</span>('use queue'<span style="color: #000000">);
            }</span><span style="color: #0000ff">catch</span>(PDOException <span style="color: #800080">$e</span><span style="color: #000000">){
                </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$e</span>-><span style="color: #000000">getMessage();
            }
        }</span><span style="color: #0000ff">catch</span>(PDOException <span style="color: #800080">$e</span><span style="color: #000000">){
            </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$e</span>-><span style="color: #000000">getMessage();
        }
    }
    </span><span style="color: #008000">/*</span><span style="color: #008000">
    *入列
    *$data数组
    *成功返回1,失败返回false,或者null
    </span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> _unshift(<span style="color: #800080">$data</span> =<span style="color: #0000ff">array</span><span style="color: #000000">()){
        </span><span style="color: #0000ff">if</span>(!<span style="color: #008080">is_array</span>(<span style="color: #800080">$data</span>) || <span style="color: #0000ff">empty</span>(<span style="color: #800080">$data</span>)){<span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;}
        </span><span style="color: #800080">$sql</span> = 'default'<span style="color: #000000">;
        </span><span style="color: #0000ff">foreach</span>(<span style="color: #800080">$data</span> <span style="color: #0000ff">as</span> <span style="color: #800080">$k</span>=><span style="color: #800080">$v</span><span style="color: #000000">){
            </span><span style="color: #800080">$sql</span> .= ',"'.<span style="color: #800080">$v</span>.'"'<span style="color: #000000">;
        }
        </span><span style="color: #008000">//</span><span style="color: #008000">把$data转为sql语句,添加到队列容器contain中</span>
        <span style="color: #800080">$sql</span> = 'insert into goods values('.<span style="color: #800080">$sql</span>.')'<span style="color: #000000">;
        </span><span style="color: #0000ff">return</span> @<span style="color: #008080">array_unshift</span>(self::<span style="color: #800080">$container</span>,<span style="color: #800080">$sql</span><span style="color: #000000">);
    }
    </span><span style="color: #008000">/*</span><span style="color: #008000">
    *出列
    *成功返回最后插入的数据id
    *失败返回失败信息
    </span><span style="color: #008000">*/</span>
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">function</span><span style="color: #000000"> _pop(){
        </span><span style="color: #0000ff">try</span>{<span style="color: #008000">//</span><span style="color: #008000">获取尾部的sql语句,出列</span>
            <span style="color: #800080">$sql</span> = <span style="color: #008080">array_pop</span>(self::<span style="color: #800080">$container</span><span style="color: #000000">);
            self</span>::<span style="color: #800080">$pdo</span>-><span style="color: #008080">exec</span>(<span style="color: #800080">$sql</span><span style="color: #000000">);
            </span><span style="color: #008000">//</span><span style="color: #008000">把剩余的容器值赋给中间变量</span>
            <span style="color: #800080">$middle</span> = self::<span style="color: #800080">$container</span><span style="color: #000000">;
            </span><span style="color: #008000">//</span><span style="color: #008000">判断中间变量是否为空,不为空则继续获取容器尾部的数据,继续出列</span>
            <span style="color: #0000ff">while</span>(<span style="color: #800080">$middle</span><span style="color: #000000">){
                </span><span style="color: #800080">$sql</span> = <span style="color: #008080">array_pop</span>(<span style="color: #800080">$middle</span><span style="color: #000000">);
                self</span>::<span style="color: #800080">$pdo</span>-><span style="color: #008080">exec</span>(<span style="color: #800080">$sql</span><span style="color: #000000">);
                </span><span style="color: #008000">//</span><span style="color: #008000">把数据再转给容器,也就是转给while循环外面的$middle,这样才能实现循环,让容器里面的数据全部出列,插入到数据库中</span>
                self::<span style="color: #800080">$container</span> = <span style="color: #800080">$middle</span><span style="color: #000000">;
            }
            </span><span style="color: #008000">//</span><span style="color: #008000">返回的是,最后插入的id,失败是null</span>
            <span style="color: #0000ff">return</span> self::<span style="color: #800080">$pdo</span>-><span style="color: #000000">lastInsertId();

            }</span><span style="color: #0000ff">catch</span>(PDOException <span style="color: #800080">$e</span><span style="color: #000000">){
                </span><span style="color: #0000ff">return</span> <span style="color: #800080">$e</span>-><span style="color: #000000">getMessage();
            }
    }
    </span><span style="color: #008000">//</span><span style="color: #008000">析构函数,对象回收时调用,通过它实现自动让容器数据入库</span>
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> __destruct(){
        </span><span style="color: #008000">//</span><span style="color: #008000">当对象被回收时,判断容器是否存在消息,假如存在,则调用_pop方法,把消息出列</span>
        <span style="color: #0000ff">if</span>(self::<span style="color: #800080">$container</span><span style="color: #000000">){
            self</span>::<span style="color: #000000">_pop();
        }
    }
}</span>

  下面是同一级目录下调用这个类的文件的内容,当然,你也可以在上面那个queue类的下面调用_unshift方法实现消息队列的操作

<span style="color: #000000">php
</span><span style="color: #0000ff">include</span> './queue.php'<span style="color: #000000">;
</span><span style="color: #0000ff">use</span> Home\Queue;<span style="color: #008000">//</span><span style="color: #008000">引入命名空间的Queue类</span>
<span style="color: #800080">$ob</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> Queue;
</span><span style="color: #008000">//</span><span style="color: #008000">消息入列</span>
<span style="color: #800080">$ob</span>->_unshift(<span style="color: #0000ff">array</span>('goodsname'=>'vivo手机8888','price'=>1120,'category_id'=>1,'sort'=>1,'description'=>'vivo品牌','remark'=>'好用'<span style="color: #000000">));
</span><span style="color: #800080">$ob</span>->_unshift(<span style="color: #0000ff">array</span>('goodsname'=>'小米手机888','price'=>1998,'category_id'=>1,'sort'=>1,'description'=>'小米品牌手机','remark'=>'国产好货'<span style="color: #000000">));
</span><span style="color: #008000">//</span><span style="color: #008000">之所以打印出来,完成是为了看看效果,至于,有没有实现商品入库,你得去看看你的数据库了,呵呵</span>
<span style="color: #008080">var_dump</span>(Queue::<span style="color: #800080">$container</span>);

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:网上商城7--订单模块Artikel seterusnya:Zend Studio安装详解