您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关怎么在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 "<h2>$str<h2/>";
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中利用Memcached实现一个留言板功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。