php+Memcached实现简单留言板功能示例

yang-pig| 阅读:1555 发表时间:2017-03-22 21:17:24 PHP

MyPdo.php

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/22
* Time: 18:29
*/
class MyPdo{
private $pdo;
function __construct()
{
$this->pdo = $this->getPdo();
}

public function getPdo()
{
$dbms='mysql';
$dbName='msg';
$user='root';
$pwd='';
$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
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/22
* Time: 18:36
*/
include("MyPdo.php");
//连接Memcached服务器
$m = new Memcache;
$m->addServer('localhost', 11211);
//获取Memcached中的list
$res = $m->get("list");
//print_r($res);
//如果没有数据,则从数据库中查出,并放入Memcached中,如果有数据则直接输出
if(!$res){
$MyPdo = new MyPdo();
$res = $MyPdo->query("select * from test","array");
$m->set('list',$res,false,3600);
}
foreach($res as $val){
echo $val['title']."-------".$val['content']."<br>";
}
?>
<a href="AddMessage.php" rel="external nofollow" >添加留言</a>

AddMessage.php


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="CheckAdd.php" method="post">
标题:<input type="text" name="title"><br>
内容:<input type="text" name="content"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

CheckAdd.php

<?php
include("MyPdo.php");
//连接Memcached服务器
$m = new Memcache;
$m->addServer('127.0.0.1',11211);
$title = $_POST['title'];
$content = $_POST['content'];
$MyPdo = new MyPdo();
$res = $MyPdo->query("insert into test(title,content) values('$title','$content')");
if($res){
//如果insert语句执行成功则清除Memcache中的缓存
$m->delete("list");
}
header("location:ShowMessage.php");

Test.php 存取其他类型数据

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/3/22
* Time: 19:06
*/
$m = new Memcache;
$m->addServer('localhost', 11211);

$m->set('int', 99);
$m->set('string', 'a simple string');
$m->set('array', array(11, 12),false,64);
/* 'object'这个key将在5分钟后过期 */
$m->set('object', new stdclass,false, time() + 300);


var_dump($m->get('int'));
var_dump($m->get('string'));
var_dump($m->get('array'));
var_dump($m->get('object'));

Wampserver下安装memcache地址:http://www.yang-pig.com/article/5.html

本文参考地址:http://www.jb51.net/article/105612.htm

代码下载地址:http://pan.baidu.com/s/1eRBFGSM 密码:3b6n