php+Memcached实现简单留言板功能示例
作者:水晶依恋 时间:2023-11-15 09:30:02
本文实例讲述了php+Memcached实现简单留言板功能。分享给大家供大家参考,具体如下:
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;
}
/**
* 提示错误
*
* @param string $str 错误提示内容
*/
public function showErrors($str)
{
echo "<h1>$str<h1/>";
exit();
}
}
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){
$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">
标题:<input type="text" name="title"><br>
内容:<input type="text" name="content"><br>
<input type="submit" value="提交">
</form>
CheckAdd.php
<?php
include("MyPdo.php");
//连接Memcached服务器
$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语句执行成功则清除Memcache中的缓存
$m->delete("list");
}
header("location:ShowMessage.php");
运行结果如下所示:
注:此例子只是简单实现了,留言列表和添加留言功能,需要注意的是,如果对数据库的数据有了添加或修改,需要清除缓存,然后重新缓存一下,已保证数据显示同步。
希望本文所述对大家PHP程序设计有所帮助。
标签:php,Memcached,留言板
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL常见错误提示及解决方法
2008-02-23 10:08:00
Python写一个简单的api接口的实现
2023-07-23 20:20:53
python学习之panda数据分析核心支持库
2023-11-04 17:39:29
![](https://img.aspxhome.com/file/2023/0/115640_0s.png)
详解Oracle在out参数中访问光标
2024-01-12 21:17:50
jsp学习之scriptlet的使用方法详解
2023-06-27 11:06:37
用Python制作一个可以聊天的皮卡丘版桌面宠物
2021-12-05 10:44:41
![](https://img.aspxhome.com/file/2023/0/133270_0s.jpg)
支持多浏览器(IE、Firefox、Opera)剪切板复制函数_脚本之家修正版
2024-05-03 15:08:06
MySQL索引最左匹配原则实例详解
2024-01-27 18:38:05
![](https://img.aspxhome.com/file/2023/5/71785_0s.png)
python在windows下创建隐藏窗口子进程的方法
2021-05-19 14:44:51
oracle修改scott密码与解锁的方法详解
2024-01-19 19:10:40
java.sql.SQLException: 内部错误: Unable to construct a Datum from the specified input
2010-07-16 13:23:00
python实现水仙花数实例讲解
2021-04-05 11:38:05
![](https://img.aspxhome.com/file/2023/9/101129_0s.png)
Python判断字符串与大小写转换
2021-07-17 03:23:35
python简单实现插入排序实例代码
2021-11-27 14:33:04
![](https://img.aspxhome.com/file/2023/5/134575_0s.png)
隐藏修改文件时间和文件属性的ASP脚本
2011-03-17 11:15:00
windows下安装、卸载mysql服务的方法(mysql 5.6 zip解压版安装教程)
2024-01-13 04:56:20
![](https://img.aspxhome.com/file/2023/8/124508_0s.jpg)
详解Oracle数据库各类控制语句的使用
2024-01-13 16:49:00
Go 库性能分析工具pprof
2024-02-13 20:55:27
oracle查看被锁的表和被锁的进程以及杀掉这个进程
2024-01-15 12:04:15
Numpy 理解ndarray对象的示例代码
2023-07-17 00:56:20
![](https://img.aspxhome.com/file/2023/7/62607_0s.png)