php Memcached實現簡單留言板功能示例

2022-09-29 12:39:14 字數 1578 閱讀 8488

mypdo.php

<?php class mypdo

/*** createpdo

** @return pdo

*/public function getpdo()

catch(exception $e)

$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")

$query = $this->pdo->query($sql);

if (!$query)

if (strpos(strtolower($sql), "select") ===false)

switch ($mode)

return $res;

} /**

* 提示錯誤

* * @param string $str 錯誤提示內容

*/public function showerrors($str) }

showmessage.php

<?php include("mypdo.php");

//連線memcached伺服器

$m = new memcached();

$m->addserver('127.0.0.1',11211);

//獲取memcached中的list

$res = $m->get("list");

//如果沒有資料,則從資料庫中查出,並放入memcached中,如果有資料則直接輸出

if(!$res)

foreach($res as $val)

?>

新增留言

addmessage.php

checkadd.php

<?php include("mypdo.php");

//連線memcached伺服器

$m = 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')");

iwww.cppcns.comf($res)

heade"location:showmessage.php");

執行結果如下所示:

注:此例子只是簡單實現了,留言列表和新增留言功能,需要注意的是,如果對資料庫的資料有了新增或修改,需要清除快取,然後重新快取一下,已保證資料顯示同步。

PHP memcached 應用示例

php memcached 應用示例 add key,val,exp 0 往 memcached 中寫入物件,key 是物件的唯一識別符號,val 是寫入的物件資料,exp 為過期時間,單位為秒,預設為不限時間 get key 從 memcached 中獲取物件資料,通過物件的唯一識別符號 key ...

PHP memcached 常用指令 整理

p 監聽的埠 l 連線的ip位址,預設是本機 d start 啟動memcached服務 d restart 重起memcached服務 d stop shutdown 關閉正在執行的memcached服務 d install 安裝memcached服務 d uninstall 解除安裝memcac...

關於php memcached的使用

memcached 搭建環境的幾點關鍵 2.php.ini 開啟memcached服務 4在進行 win 環境 安裝時候 特別是win10 cmd 執行 memcached d install 會報錯 failed to install service or service already inst...