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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

水晶依恋   2021-03-24 我要评论
想了解php+Memcached实现简单留言板功能示例的相关内容吗,水晶依恋在本文为您仔细讲解php+Memcached留言板的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php,Memcached,留言板,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们