采用单例模式编写PHP的PDO类

下面的代码是用此前一个名为MyPDO的类改写的,引入了单例模式来保证在全局调用中不会重复实例化这个类,降低系统资源的浪费。

代码如下:

<?php
/**
 * MyPDO
 * @author Jason.Wei <jasonwei06@hotmail.com>
 * @license http://www.sunbloger.com/
 * @version 5.0 utf8
 */
class MyPDO
{
    protected static $_instance = null;
    protected $dbName = '';
    protected $dsn;
    protected $dbh;
    
    /**
     * 构造
     * 
     * @return MyPDO
     */
    private function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
    {
        try {
            $this->dsn = 'mysql:host='.$dbHost.';dbname='.$dbName;
            $this->dbh = new PDO($this->dsn, $dbUser, $dbPasswd);
            $this->dbh->exec('SET character_set_connection='.$dbCharset.', character_set_results='.$dbCharset.', character_set_client=binary');
        } catch (PDOException $e) {
            $this->outputError($e->getMessage());
        }
    }
    
    /**
     * 防止克隆
     * 
     */
    private function __clone() {}
    
    /**
     * Singleton instance
     * 
     * @return Object
     */
    public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
    {
        if (self::$_instance === null) {
            self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
        }
        return self::$_instance;
    }
    
    /**
     * Query 查询
     *
     * @param String $strSql SQL语句
     * @param String $queryMode 查询方式(All or Row)
     * @param Boolean $debug
     * @return Array
     */
    public function query($strSql, $queryMode = 'All', $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $recordset = $this->dbh->query($strSql);
        $this->getPDOError();
        if ($recordset) {
            $recordset->setFetchMode(PDO::FETCH_ASSOC);
            if ($queryMode == 'All') {
                $result = $recordset->fetchAll();
            } elseif ($queryMode == 'Row') {
                $result = $recordset->fetch();
            }
        } else {
            $result = null;
        }
        return $result;
    }
    
    /**
     * Update 更新
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param String $where 条件
     * @param Boolean $debug
     * @return Int
     */
    public function update($table, $arrayDataValue, $where = '', $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        if ($where) {
            $strSql = '';
            foreach ($arrayDataValue as $key => $value) {
                $strSql .= ", `$key`='$value'";
            }
            $strSql = substr($strSql, 1);
            $strSql = "UPDATE `$table` SET $strSql WHERE $where";
        } else {
            $strSql = "REPLACE INTO `$table` (`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        }
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    
    /**
     * Insert 插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param Boolean $debug
     * @return Int
     */
    public function insert($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "INSERT INTO `$table` (`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    
    /**
     * Replace 覆盖方式插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 字段与值
     * @param Boolean $debug
     * @return Int
     */
    public function replace($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "REPLACE INTO `$table`(`".implode('`,`', array_keys($arrayDataValue))."`) VALUES ('".implode("','", $arrayDataValue)."')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    
    /**
     * Delete 删除
     *
     * @param String $table 表名
     * @param String $where 条件
     * @param Boolean $debug
     * @return Int
     */
    public function delete($table, $where = '', $debug = false)
    {
        if ($where == '') {
            $this->outputError("'WHERE' is Null");
        } else {
            $strSql = "DELETE FROM `$table` WHERE $where";
            if ($debug === true) $this->debug($strSql);
            $result = $this->dbh->exec($strSql);
            $this->getPDOError();
            return $result;
        }
    }
    
    /**
     * execSql 执行SQL语句
     *
     * @param String $strSql
     * @param Boolean $debug
     * @return Int
     */
    public function execSql($strSql, $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }
    
    /**
     * 获取字段最大值
     * 
     * @param string $table 表名
     * @param string $field_name 字段名
     * @param string $where 条件
     */
    public function getMaxValue($table, $field_name, $where = '', $debug = false)
    {
        $strSql = "SELECT MAX(".$field_name.") AS MAX_VALUE FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        $maxValue = $arrTemp["MAX_VALUE"];
        if ($maxValue == "" || $maxValue == null) {
            $maxValue = 0;
        }
        return $maxValue;
    }
    
    /**
     * 获取指定列的数量
     * 
     * @param string $table
     * @param string $field_name
     * @param string $where
     * @param bool $debug
     * @return int
     */
    public function getCount($table, $field_name, $where = '', $debug = false)
    {
        $strSql = "SELECT COUNT($field_name) AS NUM FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        return $arrTemp['NUM'];
    }
    
    /**
     * 获取表引擎
     * 
     * @param String $dbName 库名
     * @param String $tableName 表名
     * @param Boolean $debug
     * @return String
     */
    public function getTableEngine($dbName, $tableName)
    {
        $strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='".$tableName."'";
        $arrayTableInfo = $this->query($strSql);
        $this->getPDOError();
        return $arrayTableInfo[0]['Engine'];
    }
    
    /**
     * beginTransaction 事务开始
     */
    private function beginTransaction()
    {
        $this->dbh->beginTransaction();
    }
    
    /**
     * commit 事务提交
     */
    private function commit()
    {
        $this->dbh->commit();
    }
    
    /**
     * rollback 事务回滚
     */
    private function rollback()
    {
        $this->dbh->rollback();
    }
    
    /**
     * transaction 通过事务处理多条SQL语句
     * 调用前需通过getTableEngine判断表引擎是否支持事务
     *
     * @param array $arraySql
     * @return Boolean
     */
    public function execTransaction($arraySql)
    {
        $retval = 1;
        $this->beginTransaction();
        foreach ($arraySql as $strSql) {
            if ($this->execSql($strSql) == 0) $retval = 0;
        }
        if ($retval == 0) {
            $this->rollback();
            return false;
        } else {
            $this->commit();
            return true;
        }
    }

    /**
     * checkFields 检查指定字段是否在指定数据表中存在
     *
     * @param String $table
     * @param array $arrayField
     */
    private function checkFields($table, $arrayFields)
    {
        $fields = $this->getFields($table);
        foreach ($arrayFields as $key => $value) {
            if (!in_array($key, $fields)) {
                $this->outputError("Unknown column `$key` in field list.");
            }
        }
    }
    
    /**
     * getFields 获取指定数据表中的全部字段名
     *
     * @param String $table 表名
     * @return array
     */
    private function getFields($table)
    {
        $fields = array();
        $recordset = $this->dbh->query("SHOW COLUMNS FROM $table");
        $this->getPDOError();
        $recordset->setFetchMode(PDO::FETCH_ASSOC);
        $result = $recordset->fetchAll();
        foreach ($result as $rows) {
            $fields[] = $rows['Field'];
        }
        return $fields;
    }
    
    /**
     * getPDOError 捕获PDO错误信息
     */
    private function getPDOError()
    {
        if ($this->dbh->errorCode() != '00000') {
            $arrayError = $this->dbh->errorInfo();
            $this->outputError($arrayError[2]);
        }
    }
    
    /**
     * debug
     * 
     * @param mixed $debuginfo
     */
    private function debug($debuginfo)
    {
        var_dump($debuginfo);
        exit();
    }
    
    /**
     * 输出错误信息
     * 
     * @param String $strErrMsg
     */
    private function outputError($strErrMsg)
    {
        throw new Exception('MySQL Error: '.$strErrMsg);
    }
    
    /**
     * destruct 关闭数据库连接
     */
    public function destruct()
    {
        $this->dbh = null;
    }
}
?>

 

调用方法:

<?php
require 'MyPDO.class.php';
$db = MyPDO::getInstance('localhost', 'root', '123456', 'test', 'utf8');

//do something...

$db->destruct();
?>

阳光部落原创,更多内容请访问 http://www.sunbloger.com/

Linux下管理Memcached的Service脚本

Memcached 的编译安装过程请参考本站的《Linux下部署Memcached和PHP的Memcache扩展方法

如 Memcached 已在 Linux 中安装完毕,且被部署到 /usr/local/memcached 这个路径下,接下来我们来开始为其安装用于启动和停止的服务管理脚本。

首先,通过 vi 在 /etc/rc.d/init.d 路径下新建一个名为 memcached 的脚本文件,命令如下:

vi /etc/rc.d/init.d/memcached

然后,向其中写入 Shell 脚本如下:

#!/bin/sh  
#  
# chkconfig: 2345 90 50
# description: Memcached Service Daemon
#
# processname: Memcached
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0

MEMCACHED="/usr/local/memcached/bin/memcached"

start()
{
    echo -n $"Starting Memcached: "
    daemon $MEMCACHED -u daemon -d -m 32 -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
    echo
}
stop()
{
    echo -n $"Shutting down Memcached: "
    killproc memcached
    echo
}

[ -f $MEMCACHED ] || exit 1
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0

保存退出 vi 后,执行下面的命令来安装这个脚本

chmod 777 /etc/rc.d/init.d/memcached
chkconfig --add memcached
chkconfig --level 235 memcached on

然后可以通过下面的命令来检查是否安装成功

chkconfig --list | grep memcached
service memcached start
service memcached stop
service memcached restart

阳光部落原创,更多内容请访问 http://www.sunbloger.com/

开源消息队列MemcacheQ在Linux中编译安装教程

队列(Queue)是一种常用的数据结构。在队列这种数据结构中,最先插入的元素将会最先被取出;反之最后插入的元素将会最后被取出,因此队列又称为“先进先出”(FIFO:First In First Out)的线性表。

加入元素的一端叫“队尾”,取出元素的一端叫“队头”。利用消息队列可以很好地异步处理数据的传送和存储,当遇到频繁且密集地向后端数据库中插入数据时,就可采用消息队列来异步处理这些数据写入。

MemcacheQ是一款基于Memcache协议的开源消息队列服务软件,由于其遵循了Memcache协议,因此开发成本很低,不需要学习额外的知识便可轻松掌握。

我在最近的一个项目中也应用了MemcacheQ,下面我将分享一下MemcacheQ在Linux中的编译和安装过程。

首先,MemcacheQ依赖于BerkeleyDB和Libevent,如果服务器中曾经安装过Memcached,那么Libevent应该已经存在了,否则就需要先下载安装Libevent。

下载链接如下:

Libevent:https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
Berkeley DB:http://download.oracle.com/otn/berkeley-db/db-6.0.30.tar.gz
MemcacheQ:https://github.com/stvchu/memcacheq

安装Libevent

tar zvxf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
make && make install
echo "/usr/local/libevent/lib" >> /etc/ld.so.conf
ldconfig

安装BerkeleyDB

BerkeleyDB简介:BerkeleyDB是一个开源的文件数据库,介于关系数据库与内存数据库之间,使用方式与内存数据库类似,它提供的是一系列直接访问数据库的函数,而不是像关系数据库那样需要网络通讯、SQL解析等步骤。

MemcacheQ依赖BerkleyDB用于队列数据的持久化存储,以免在MemcacheQ意外崩溃或中断时,队列数据不会丢失。

tar zxvf db-6.0.30.tar.gz
cd db-6.0.30/build_unix
../dist/configure --prefix=/usr/local/berkeleydb
make && make install
ln -s /usr/local/berkeleydb/lib/libdb-6.0.so /usr/lib/
echo "/usr/local/berkeleydb/lib/" >> /etc/ld.so.conf
ldconfig

安装MemcacheQ

tar zxvf memcacheq-0.2.0.tar.gz
cd memcacheq-0.2.0
./configure --prefix=/usr/local/memcacheq --with-bdb=/usr/local/berkeleydb --with-libevent=/usr/local/libevent --enable-threads
make && make install

启动MemcacheQ

/usr/local/memcacheq/bin/memcacheq -d -uroot -r -l 127.0.0.1 -p11210 -H /usr/local/mcq -N -R -v -L 1024 -B 1024 > /usr/local/mcq/logs/mcq_error.log 2>&1

附:MemcacheQ参数

-p <num>      TCP监听端口(default: 22201)
-U <num>      UDP监听端口(default: 0, off)
-s <file>     unix socket路径(不支持网络)
-a <mask>     unix socket访问掩码(default 0700)
-l <ip_addr>  监听网卡
-d            守护进程
-r            最大化核心文件限制
-u <username> 以用户身份运行(only when run as root)
-c <num>      最大并发连接数(default is 1024)
-v            详细输出 (print errors/warnings while in event loop)
-vv           更详细的输出 (also print client commands/reponses)
-i            打印许可证信息
-P <file>     PID文件
-t <num>      线程数(default 4)

用PHP测试一下

<?php
$mcq = new Memcache;
$mcq->connect('127.0.0.1', 11210) or die ('Connect Error!');
for ($i=1; $i<=5; $i++) {
    memcache_set($mcq, 'k', $i, 0, 0);
}
for ($i=1; $i<=6; $i++) {
    $queue = memcache_get($mcq, 'k');
    if ($queue === false) {
        echo "null\n";
    } else {
        echo $queue."\n";
    }
}

memcache_close($mcq);
?>

阳光部落原创,更多内容请访问 http://www.sunbloger.com/