思路
1、查询单条数据详情时,根据key从缓存中读取,读取不到的查库并缓存一份 2、在更新数据时,根据key从缓存中清除该条数据缓存
实现
查询单挑数据详情find.php 查询单条数据详情时,根据key从缓存中读取,读取不到的查库并缓存一份
<?php
require "./RunDbPdo.php";
require "./MmCache.php";
$model = new RunDbPdo();
$cache = new MmCache();
$model->configFile = './config/user.config.php';
$user_id = 2;
$d = $user_id % 2;
$model->configFile = "./config/user{$d}.config.php";
$sql = "select * from mm_user{$d} where user_id='{$user_id}'";
$key = "{user_id:$user_id}";
$data = $cache->get($key);
//获取不到缓存则加入缓存
if(empty($data)){
$data = $model->getRow($sql);
$cache->set($key,$data,3600);
}
var_dump($data);
在更新数据时,根据key从缓存中清除该条数据缓存
<?php
require './RunDbPdo.php';
require './RedisQ.php';
require './MmCache.php';
$model = new RunDbPdo();
$cache = new MmCache('127.0.0.1', '11211');
$model->configFile = './config/user.config.php';
$redis = new RedisQ();
if(isset($_GET) && !empty($_GET)){
extract($_GET);
$user_id = isset($user_id)?$user_id:0;
$sql = "select * from mm_user where user_id='{$user_id}'";
$data = $model->getRow($sql);
}
$model->close();
if(isset($_POST) && !empty($_POST)){
extract($_POST);
$user_id = isset($user_id)?$user_id:0;
$username = isset($username)?$username:0;
$age = isset($age)?$age:0;
$d = $user_id%2;
$model->configFile = "./config/user{$d}.config.php";
$sql = "update mm_user{$d} set username='{$username}',age='{$age}' where user_id='{$user_id}'";
$resutlt = $model->query($sql);
if($resutlt){
//根据key从缓存中清除该条数据缓存
$key = "{user_id:$user_id}";
$cache->remove($key);
$sql = "update mm_user set username='{$username}',age='{$age}' where user_id='{$user_id}'";
$redis->lpush('sqls', $sql);
header('location:findAll.php');
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>用户管理系统</title>
<link href="./css/property-system.css" rel="stylesheet">
<link href="./css/operate-system.css" rel="stylesheet">
</head>
<body>
<main>
<section class="content">
<fieldset class="form-list-32">
<h3 class="operate-title-1"><i></i>编辑用户信息</h3>
<form action="edit.php" id="editFormId" method="post" autocomplete="off">
<ul>
<input type="hidden" name="user_id" value="<?=$data['user_id']?>">
<li><h6>用户名:</h6><aside><input type="text" name="username" value="<?=$data['username']?>" class="tbox30 tbox30-6"/></aside></li>
<li><h6>年龄:</h6><aside><input name="age" value="<?=$data['age']?>" maxlength="11" class="tbox30 tbox30-6" type="text"></aside></li>
<li class="agent-subbtn-wrap mt30px">
<h6> </h6><aside><input class="btn-2" type="submit" value="提交"></aside>
</li>
</ul>
</form>
</fieldset>
</section>
</main>
</body>
</html>
缓存类MmCache.php
<?php
/**
+------------------------------------------------------------------------------
* Run Framework Memcache操作类
*/
class MmCache {
public $mem = null; //Memcache对象
public $expire = 300; //过期时间(5分钟)
public $connected = false; //连接标识
public $compressed = false; //是否启用数据压缩-暂时停止使用
public $compressed_new = true; //是否启用数据压缩
public $prefix = 'run_'; //缓存键前缀
/**
+----------------------------------------------------------
* 类的构造子
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function __construct($host = '127.0.0.1', $port = '11211') {
if (!class_exists('Memcache')) {
die('Not Support : Memcache');
}
$this->mem = new Memcache();
$this->host = $host;
$this->port = $port;
}
/**
+----------------------------------------------------------
* 类的析构方法(负责资源的清理工作)
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function __destruct() {
$this->close();
$this->mem = null;
$this->expire = null;
$this->connected = null;
$this->compressed = null;
$this->compressed_new = null;
$this->prefix = null;
}
/**
+----------------------------------------------------------
* 打开Memcache连接
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
*/
private function connect() {
if (!$this->connected) {
$this->connected = $this->mem->pconnect($this->host, $this->port);
if (!$this->connected){
die("连接Memcache失败");
}
$host = $port = null;
}
}
/**
+----------------------------------------------------------
* 关闭Memcache连接
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
*/
private function close() {
if ($this->connected) {
$this->mem->close();
$this->connected = null;
}
}
/**
+----------------------------------------------------------
* 写入缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $key 缓存键值
* @param mixed $value 被缓存的数据
* @param mixed $expire 缓存时间
+----------------------------------------------------------
* @return boolean
+----------------------------------------------------------
*/
public function set($key, $value, $expire = 0) {
$data = serialize($value);
if ($this->compressed_new && function_exists('gzcompress')) {
if (!empty($data)){
$data = gzcompress($data, 3);
}
}
$expire = $expire > 0 ? $expire : $this->expire;
if (!$this->connected || !isset($this->connected)){
$this->connect();
}
return $this->mem->set(md5($this->prefix . $key), $data, 0, $expire);
}
/**
+----------------------------------------------------------
* 读取缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $key 缓存键值
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
public function get($key) {
if (!$this->connected || !isset($this->connected)){
$this->connect();
}
$data = $this->mem->get(md5($this->prefix . $key));
if (empty($data)){
return '';
}
if ($this->compressed_new && function_exists('gzcompress')) {
$data = gzuncompress($data);
}
return unserialize($data);
}
/**
+----------------------------------------------------------
* 删除缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $key 缓存键值
+----------------------------------------------------------
* @return boolean
+----------------------------------------------------------
*/
public function remove($key) {
if ($this->connected == null || !isset($this->connected)){
$this->connect();
}
return $this->mem->delete(md5($this->prefix . $key));
}
/**
+----------------------------------------------------------
* 清除缓存(删除所有缓存数据)
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolean
+----------------------------------------------------------
*/
public function clear() {
if ($this->connected == null || !isset($this->connected)){
$this->connect();
}
return $this->mem->flush();
}
}
?>
|