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

yipeiwu_com6年前PHP代码库

本文实例讲述了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+MySQL留言板开发专题》、《php缓存技术总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php将文件夹打包成zip文件的简单实现方法

示例如下: function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定。 wh...

php 根据自增id创建唯一编号类

在开发过程中,我们数据表一般都使用自增数字作为id主键,而id是数字型,不容易理解。我们把id按一定格式转为编号后,很容易根据编号知道代表的是什么内容。 例如订单表id=20160111...

PHP字符串的递增和递减示例介绍

今天看到php手册上有这么一段话: “在处理字符变量的算数运算时,PHP 沿袭了 Perl 的习惯,而非 C 的。例如,在 Perl 中 $a = 'Z'; $a++; 将把 $a 变成...

iis下php mail函数的sendmail配置方法(官方推荐)

首先你需要先到从http://glob.com.au/sendmail/下载sendmail.zip文件,点此可以直接下载噢,然后把它解压到如D:\php\sendmail\目录下。 然...

php中可能用来加密字符串的函数[base64_encode、urlencode、sha1]

登录原理还是蛮复杂的,像我这样以为curl获取页面再post上去的想法真是太单纯了。 整理下遇到的价格处理字符串的函数: 复制代码 代码如下: <?php $encryption...