php+Memcached實(shí)現(xiàn)簡(jiǎn)單留言板功能示例
本文實(shí)例講述了php+Memcached實(shí)現(xiàn)簡(jiǎn)單留言板功能。分享給大家供大家參考,具體如下:
MyPdo.php
<?php
class MyPdo{
private $pdo;
function __construct()
{
$this->pdo = $this->getPdo();
}
/**
* CreatePDO
*
* @return PDO
*/
public function getPdo()
{
$dbms='mysql';
$dbName='testdb';
$user='root';
$pwd='diligentyang';
$host='localhost';
$dsn="$dbms:host=$host;dbname=$dbName";
try{
$pdo=new PDO($dsn,$user,$pwd);
}catch(Exception $e){
echo $e->getMessage().'<br>';
exit();
}
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->exec("set names utf8");
return $pdo;
}
/**
* Execute SQL
*
* @param string $sql Sql
* @param string $mode Mode
*
* @return mixed
*/
function query($sql = "", $mode = "array")
{
$sql = trim($sql);
if ($sql == "") {
$this->showErrors("the mothe query neet at least one param!");
}
$query = $this->pdo->query($sql);
if (!$query) {
$this->showErrors("the sql string is false");
}
if (strpos(strtolower($sql), "select") ===false) {
return $query;
}
switch ($mode) {
case 'array' :
$res = $query->fetchAll(PDO::FETCH_ASSOC);
break;
case 'object' :
$res = $query->fetchObject();
break;
case 'count':
$res = $query->rowCount();
break;
default:
$this->showErrors("SQLERROR: please check your second param!");
}
return $res;
}
/**
* 提示錯(cuò)誤
*
* @param string $str 錯(cuò)誤提示內(nèi)容
*/
public function showErrors($str)
{
echo "<h1>$str<h1/>";
exit();
}
}
ShowMessage.php
<?php
include("MyPdo.php");
//連接Memcached服務(wù)器
$m = new Memcached();
$m->addServer('127.0.0.1',11211);
//獲取Memcached中的list
$res = $m->get("list");
//如果沒有數(shù)據(jù),則從數(shù)據(jù)庫(kù)中查出,并放入Memcached中,如果有數(shù)據(jù)則直接輸出
if(!$res){
$MyPdo = new MyPdo();
$res = $MyPdo->query("select * from message","array");
$m->set('list',$res,3600);
}
foreach($res as $val){
echo $val['title']."-------".$val['content']."<br>";
}
?>
<a href="AddMessage.php" rel="external nofollow" >添加留言</a>
AddMessage.php
<form action="CheckAdd.php" method="post"> 標(biāo)題:<input type="text" name="title"><br> 內(nèi)容:<input type="text" name="content"><br> <input type="submit" value="提交"> </form>
CheckAdd.php
<?php
include("MyPdo.php");
//連接Memcached服務(wù)器
$m = new Memcached();
$m->addServer('127.0.0.1',11211);
$title = $_POST['title'];
$content = $_POST['content'];
$MyPdo = new MyPdo();
$res = $MyPdo->query("insert into message(title,content) values('$title','$content')");
if($res){//如果insert語(yǔ)句執(zhí)行成功則清除Memcache中的緩存
$m->delete("list");
}
header("location:ShowMessage.php");
運(yùn)行結(jié)果如下所示:


注:此例子只是簡(jiǎn)單實(shí)現(xiàn)了,留言列表和添加留言功能,需要注意的是,如果對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)有了添加或修改,需要清除緩存,然后重新緩存一下,已保證數(shù)據(jù)顯示同步。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL留言板開發(fā)專題》、《php緩存技術(shù)總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php判斷兩個(gè)浮點(diǎn)數(shù)是否相等的方法
這篇文章主要介紹了php判斷兩個(gè)浮點(diǎn)數(shù)是否相等的方法,涉及php操作浮點(diǎn)數(shù)的技巧,比較實(shí)用,需要的朋友可以參考下2015-03-03
php中ftp_chdir與ftp_cdup函數(shù)用法
這篇文章主要介紹了php中ftp_chdir與ftp_cdup函數(shù)用法,以實(shí)例形式講述了PHP中的FTP目錄操作技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析
這篇文章主要介紹了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析,本文用代碼實(shí)例講解了PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù),需要的朋友可以參考下2014-10-10

