php+redis实现全页缓存系统-php教程

资源魔 26 0

保举:《PHP视频教程》《redis教程》

php redis 完成全页缓存零碎

以前的一个名目说的一个性能,需求正在后盾事后存入某个页面信息放到数据库,比方app的注册协定,用户协定,这类.而后正在写成一个php页面,app正在挪用接口的时分拜访这个页面.过后我就发现一个成绩,这些协定往往几个月才会修正一次,而每一一次用户查看这些协定的时分,nginx城市从新从数据库读取文件,速率会很缓缓了.

以下图m_about.php是我天生的数据页,

正在虚构机环境下从数据库加载进去从新天生文件需求2.4s(当然实际的测试环境会快一点).

既然这类页面数据都是更新少,为何没有缓存起来呢,想到以前看的redis罕用使用外面有一个全页缓存零碎(full page cache).没有如写一个碰运气.

代码思绪

redis应用的是phpredis扩大,当然你也可是用predis扩大,只不外需求更改外面几个读取函数罢了.

对于缓存零碎的接口,我这里参考了laravel外面cache零碎.这个零碎的设计接口我感觉设置的很明晰,外面不仅是蕴含redis,还能够应用文件,mysql,memcache.

当然全页缓存用没有到那末多货色.只是借用他的函数设计.起首是函数getUrlText,这个是猎取全页面的数据,这里不想到太多,间接应用file_get_contents,当然你也能够改写成curl函数

/**
     * 猎取对应的url的信息
     * @param string $url 对应的地点
     * @return boolean|string
     */
    public function getUrlText($url)
    {
        if (empty($url)) {
            return false;
        }
        return  file_get_contents($url);

    }

其次是几个自创cache零碎的函数,remember函数,影象缓存,这个是对外的最首要的接口,普通正在缓存零碎外面间接应用它就好.

/**
   * 记载对应的缓存,假如以前存正在则前往本来的缓存
   * @param string $cacheName 缓存名
   * @param string | callback $urlOrCallback 需求缓存的数据地点.能够是一个 网页地点也一个可回调类型,假如没有是可回调类型,则断定是一个网址
   * @param null | int $ttl 缓存过时工夫,假如不外期就是用默许值null
   * @throws \Exception 假如无奈拜访地点
   * @return boolean|string 缓存胜利前往猎取到的页面地点
   */
  public function remember($cacheName, $urlOrCallback, $ttl = null)
  {
      $value = $this->get($cacheName);//反省缓存能否存正在
      if (!$value) {
          //以前不应用键
          if (is_callable($urlOrCallback)) {
              $text = $urlOrCallback();
          } else {
              //假如没有是回调类型,则测验考试读取网址
              $text = $this->getUrlText($urlOrCallback);
          }

          if (empty($text)) {
              throw new \Exception('can not get value:' . $urlOrCallback);
          }
          $this->put($cacheName, $text, $ttl);
          return $text;
      } else {
          return $value;
      }

  }

refresh函数,刷新缓存函数,假如缓存页面被更新了,就去刷新它.

/**
 * 更新缓存,并前往以后的缓存
 * @param string $cacheName 缓存名
 * @param string | callback $urlOrCallback 需求缓存的数据地点.能够是一个 网页地点也一个可回调类型,假如没有是可回调类型,则断定是一个网址
 * @param null | int $ttl 过时工夫,假如不外期就是用默许值null
 * @return boolean|string 缓存胜利前往猎取到的页面地点
 */
public function refresh($cacheName, $urlOrCallback, $ttl = null)
{
    $this->delete($cacheName);
    return $this->remember($cacheName, $urlOrCallback, $ttl);
}

剩下的两个代码文件.一个是redisFPC.php,这是全页缓存的demo,一个是测试用的文件
fpcTest.php
这里是用的是github,衔接到我自己的git博客下面.假如衔接github有成绩,能够看本文最初给的完好代码.

测试

咱们正在这里测试,第一次加载由于需求读取对应的m_ahout的信息,以是慢一点

第二次加载由于从redislimian 读取了,以是会快的多

应用倡议

代码我以为曾经给了足够多的接口了,正在第一次缓存的时分应用remember函数记载缓存,之后假如缓存变动后应用refresh函数,更新缓存便可.假如可能的话,只管即便应用ttl设置缓存的过时工夫.

完好代码

redisFPC.php

<?php
namespace RedisFPC;
class RedisFPC
{
    /**
     * php redis的拜访类
     * @var unknown
     */
    private $redis;

    /**
     * 结构函数
     * @param array $redis 应用phpredis的类
     * @param 能否衔接胜利
     */
    public function __construct($redis = [])
    {
    
        //$this->redis = $redis;
        $this->redis = new \Redis();
        return $this->redis->connect('127.0.0.1');
    }
    /**
     * 记载对应的缓存,假如以前存正在则前往本来的缓存
     * @param string $cacheName 缓存名
     * @param string | callback $urlOrCallback 需求缓存的数据地点.能够是一个 网页地点也一个可回调类型,假如没有是可回调类型,则断定是一个网址
     * @param null | int $ttl 缓存过时工夫,假如不外期就是用默许值null
     * @throws \Exception 假如无奈拜访地点
     * @return boolean|string 缓存胜利前往猎取到的页面地点
     */
    public function remember($cacheName, $urlOrCallback, $ttl = null) 
    {
        $value = $this->get($cacheName);//反省缓存能否存正在
        if (!$value) {
            //以前不应用键
            if (is_callable($urlOrCallback)) {
                $text = $urlOrCallback();
            } else {
                //假如没有是回调类型,则测验考试读取网址
                $text = $this->getUrlText($urlOrCallback);
            }
            
            if (empty($text)) {
                throw new \Exception('can not get value:' . $urlOrCallback);
            }
            $this->put($cacheName, $text, $ttl);
            return $text;
        } else {
            return $value;
        }
        
    }
    /**
     * 猎取对应的缓存值
     * @param string $cacheName 缓存名
     * @return String | Bool,假如没有存正在前往false,不然前往对应的缓存页信息
     */
    public function get($cacheName)
    {
        return $this->redis->get($this->getKey($cacheName));
    }
    /**
     * 将对应的全页缓存保留到对应redis中
     * @param string $cacheName 缓存名
     * @param string $value
     * @param null | int $ttl 过时工夫,假如不外期就是用默许值null
     * @return boolean 保留胜利前往true
     */
    public function put($cacheName, $value, $ttl = null)    
    {
        if (is_null($ttl)) {
            return $this->redis->set($this->getKey($cacheName), $value);
        } else {
            return $this->redis->set($this->getKey($cacheName), $value, $ttl);
        }
        
    }
    /**
     * 删除了对应缓存
     * @param string $cacheName 缓存名
     */
    public function delete($cacheName)
    {
        return $this->redis->delete($this->getKey($cacheName));
    }
    
    /**
     * 更新缓存,并前往以后的缓存
     * @param string $cacheName 缓存名
     * @param string | callback $urlOrCallback 需求缓存的数据地点.能够是一个 网页地点也一个可回调类型,假如没有是可回调类型,则断定是一个网址
     * @param null | int $ttl 过时工夫,假如不外期就是用默许值null
     * @return boolean|string 缓存胜利前往猎取到的页面地点
     */
    public function refresh($cacheName, $urlOrCallback, $ttl = null)
    {
        $this->delete($cacheName);
        return $this->remember($cacheName, $urlOrCallback, $ttl);
    }
    /**
     * 猎取对应的url的信息
     * @param string $url 对应的地点
     * @return boolean|string
     */
    public function getUrlText($url)
    {
        if (empty($url)) {
            return false;
        } 
        return  file_get_contents($url);
        
    }
    /**
     * 天生全页缓存键名
     * @param string $cacheName 需求缓存的称号
     * @return string 对应的正在redis中的键名
     */
    private function getKey($cacheName)
    {
        return 'FPC:'. $cacheName;
    }
}

测试用的test代码
留意这里的url写的是内陆的缓存url

<?php 
use RedisFPC\RedisFPC;

require_once 'redisFPC.php';
/* $text = file_get_contents('http://localhost:1002/m_about.php');
var_dump($text); */
$url = 'http://localhost:1002/m_about.php';

$fpc = new RedisFPC();
echo $fpc->remember('效劳协定', $url, 60*60*24);

以上就是php+redis完成全页缓存零碎的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 Redis

抱歉,评论功能暂时关闭!