IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> Mysql分布式之缓存(Memcache)的应用 09 -> 正文阅读

[PHP知识库]Mysql分布式之缓存(Memcache)的应用 09

思路

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>&nbsp;</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();
    }

}

?>
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2022-03-22 20:18:59  更:2022-03-22 20:19:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 1:03:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码