百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> php+redis实现全页缓存系统
分享文章到:

php+redis实现全页缓存系统

发布时间:09/01 来源:未知 浏览: 关键词:

引荐:《PHP视频教程》《redis教程》

php redis 实现全页缓存系统

此前的一个项目说的一个功效,需要在后台预先存入某个页面信息放到数据库,比方app的注册和谈,会员和谈,这种.然后在写成一个php页面,app在调取接口的时候拜访这个页面.当时我就发明一个问题,这些和谈往往几个月才会修改一次,而每一次会员查看这些和谈的时候,nginx都会从新从数据库读取文件,速度会很渐渐了.

如下图m_about.php是我生成的数据页,

企业微信截图_15998021311479.png

在虚拟机环境下从数据库加载出来从新生成文件需要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的信息,所以慢一点

企业微信截图_15998021867240.png

第二次加载由于从redislimian 读取了,所以会快的多
企业微信截图_1599802202828.png

使用倡议

代码我认为已经给了足够多的接口了,在第一次缓存的时候使用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实现全页缓存系统的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板