CakeHtmlHelper 加速您的网站,启用 HTML 缓存 - Cakephp 2.x

我重写了 Matt Curry 的 HTML 缓存插件。来自 https://github.com/mcurry/html_cache – Cake 的核心缓存助手很棒,但它输出的文件是 PHP 文件,因此它永远不会像纯 HTML 文件那样快。此 HTML 缓存助手写入纯 HTML,这意味着 Web 服务器在请求时无需接触 PHP。好的,首先您必须启用 mod_rewrite 才能使 HTML 缓存正常工作;

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_METHOD} ^GET$
    RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f
    RewriteRule ^(.*)$ /cache/$1/index.html [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>

现在在文件 View/Helper/CacheHtmlHelper.php 中创建助手

<?php
// Original idea from Matt Curry, rewritten by Eugenio Fage
App::uses('AppHelper', 'View/Helper');

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class CacheHtmlHelper extends AppHelper {
    private $CacheHtmlStrategy=null;

    public function __construct(View $view, $settings = array()) {
        parent::__construct($view, $settings);
        $defaults=array('CacheHtmlStrategy'=>'CacheHtmlStrategy');
        $settings=array_merge($settings,$defaults);

        if(!class_exists($settings['CacheHtmlStrategy'])) $settings['CacheHtmlStrategy']='CacheHtmlStrategy';
        $this->CacheStrategy=new $settings['CacheHtmlStrategy']();
    }

    public function afterLayout($viewFile) {
            $this->CacheStrategy->save($this->request,$this->_View->output);
    }
}


class CacheHtmlStrategy{
    public function save($request,$content){
            $File = new File($this->url2file($request->url), true);
            $File->write($content);
    }

    public function url2file($url){
            $path=WWW_ROOT.'cache'.DS;
            $path.=implode(DS, array_filter(explode('/', $url))).DS;
            $path=str_replace('//','/',$path);
            return $path.'index.html';
    }

    public function rendercache($url=null){
            if(!$url){
                    if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO']){
                            $url=$_SERVER['PATH_INFO'];
                    }else{
                            return;
                    }
            }
            $file=cacheUrl2File($this->url2file($url));

            if(file_exists($file)){
                    $fp = fopen($file, 'r');
                    fpassthru($fp);
                    exit;
            }
    }
}

现在,每次您想要缓存操作时,将助手添加到控制器中;function someaction(){ $this->helpers[]=’HtmlCache’; }

您可以创建自己的缓存策略,将文件保存在不同的路径中,或者做一些疯狂的事情,例如:Config/Crazyhtmlcacheconfig.php

<?
class CrazyHtmlStrategy extends CacheHtmlStrategy{
    public function url2file($request){
            $path=WWW_ROOT.'cache2'.DS;
            $path.=implode(DS, array_filter(explode('/', $request->url))).DS;
            $path=str_replace('//','/',$path);
            return $path.'index.html';
    }
}

然后在您的引导程序中添加;config(‘Crazyhtmlcacheconfig’);

当您添加助手时

$this->helpers[‘HtmlCache’]=array(‘CacheHtmlStrategy’=>’CrazyHtmlStrategy’);

您还可以尝试使用 php 而不是 .htaccess 路由来渲染文件,只需在引导程序中添加以下内容;

// use CacheHtmlStrategy or the strategy that you are currently using
$cache=new CacheHtmlStrategy();
$cache->rendercache();

向大家致敬!由 Eugenio Fage 撰写