决定像罗胖一样,坚持转发他的60“语音, 看我能不能一样坚持下来!

thinkphp 静态缓存 ReadHtmlCache 的解读不错的文章

thinkphp eagle 18701℃ 0评论

thinkphp 静态缓存 ReadHtmlCache

Application\Common\Conf\config.php

<span class="preprocessor">&lt;?php</span>

<span class="keyword">return</span> <span class="keyword">array</span>(
    <span class="string">'HTML_CACHE_ON'</span> =&gt; <span class="keyword">true</span>, <span class="comment">// 开启静态缓存</span>
    <span class="string">'HTML_CACHE_TIME'</span> =&gt; <span class="number">600</span>, <span class="comment">// 全局静态缓存有效期(秒)</span>
    <span class="string">'HTML_FILE_SUFFIX'</span> =&gt; <span class="string">'.shtml'</span>, <span class="comment">// 设置静态缓存文件后缀</span>
    <span class="string">'HTML_CACHE_RULES'</span> =&gt; <span class="keyword">array</span>(<span class="comment">// 定义静态缓存规则</span>
<span class="comment">//        '静态地址' =&gt; array('静态规则', '有效期', '附加规则'),// 定义格式1 数组方式</span>
<span class="comment">//        '静态地址' =&gt; '静态规则',// 定义格式2 字符串方式</span>
        <span class="string">'*'</span> =&gt; <span class="keyword">array</span>(<span class="string">'{$_SERVER.REQUEST_URI|md5}'</span>, <span class="number">600</span>)
    ),
);

以上配置程序就会执行静态缓存了

程序会创建一个文件夹(  Linux下记得注意要可写权限 )下面就看下源码分析下

thinkphp默认启动了此行为扩展

ThinkPHP\Conf\Mode\common.php

<span class="string">'app_begin'</span>=&gt;  <span class="keyword">array</span>(
            <span class="string">'Behavior\ReadHtmlCache'</span>, <span class="comment">// 读取静态缓存</span>
),

当执行app_begin这个动作时

程序会自动运行ReadHtmlCache 的 run 方法

ThinkPHP\Library\Think\App.class.php

<span class="comment">// 应用开始标签</span>
        Hook::listen(<span class="string">'app_begin'</span>);
        <span class="comment">// Session初始化</span>
        <span class="keyword">if</span> (!IS_CLI) {
            session(C(<span class="string">'SESSION_OPTIONS'</span>));
        }
        <span class="comment">// 记录应用初始化时间</span>
        G(<span class="string">'initTime'</span>);
        App::exec();

也就是Controller里面的方法(action)执行之前会执行ReadHtmlCache 这样就不会再查数据库什么的了缓解服务器压力直接从静态页面读取

我们在看看ReadHtmlCache的run方法

<span class="comment">// 行为扩展的执行入口必须是run</span>
    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">run</span><span class="params">(&amp;<span class="variable">$params</span>)</span> {</span>
        <span class="comment">// 开启静态缓存</span>
        <span class="keyword">if</span> (IS_GET &amp;&amp; C(<span class="string">'HTML_CACHE_ON'</span>)) {
            <span class="variable">$cacheTime</span> = <span class="variable">$this</span>-&gt;requireHtmlCache();
            <span class="keyword">if</span> (<span class="keyword">false</span> !== <span class="variable">$cacheTime</span> &amp;&amp; <span class="variable">$this</span>-&gt;checkHTMLCache(HTML_FILE_NAME, <span class="variable">$cacheTime</span>)) { <span class="comment">//静态页面有效</span>
                <span class="comment">// 读取静态页面输出</span>
                <span class="keyword">echo</span> Storage::read(HTML_FILE_NAME, <span class="string">'html'</span>);
                <span class="keyword">exit</span>();
            }
        }
    }

1、是GET操作并查看配置文件HTML_CACHE_ON 是否开启2、requireHtmlCache获取缓存时间也就是根据规则获取规则中定义的缓存时间 否则获取 HTML_CACHE_TIME

3、false !== $cacheTime 所以    HTML_CACHE_TIME或者规则时间设置为 -1、0 、false时都不会执行

4、checkHTMLCache 静态页面有效

5、Storage::read(HTML_FILE_NAME, ‘html’) 读取静态页面输出

我们来研究下requireHtmlCache

<span class="variable">$htmls</span> = C(<span class="string">'HTML_CACHE_RULES'</span>); <span class="comment">// 读取静态规则</span>
        <span class="keyword">if</span> (!<span class="keyword">empty</span>(<span class="variable">$htmls</span>)) {

如果规则存在则执行

<span class="variable">$htmls</span> = array_change_key_case(<span class="variable">$htmls</span>);

规则中的key都转为小写 所以  规则的key不区分大小写

            <span class="keyword">if</span> (<span class="keyword">isset</span>(<span class="variable">$htmls</span>[<span class="variable">$controllerName</span> . <span class="string">':'</span> . <span class="variable">$actionName</span>])) {
                <span class="variable">$html</span> = <span class="variable">$htmls</span>[<span class="variable">$controllerName</span> . <span class="string">':'</span> . <span class="variable">$actionName</span>];   <span class="comment">// 某个控制器的操作的静态规则</span>
            } <span class="keyword">elseif</span> (<span class="keyword">isset</span>(<span class="variable">$htmls</span>[<span class="variable">$controllerName</span> . <span class="string">':'</span>])) {<span class="comment">// 某个控制器的静态规则</span>
                <span class="variable">$html</span> = <span class="variable">$htmls</span>[<span class="variable">$controllerName</span> . <span class="string">':'</span>];
            } <span class="keyword">elseif</span> (<span class="keyword">isset</span>(<span class="variable">$htmls</span>[<span class="variable">$actionName</span>])) {
                <span class="variable">$html</span> = <span class="variable">$htmls</span>[<span class="variable">$actionName</span>]; <span class="comment">// 所有操作的静态规则</span>
            } <span class="keyword">elseif</span> (<span class="keyword">isset</span>(<span class="variable">$htmls</span>[<span class="string">'*'</span>])) {
                <span class="variable">$html</span> = <span class="variable">$htmls</span>[<span class="string">'*'</span>]; <span class="comment">// 全局静态规则</span>
            }

所以规则的key可以写成一下四种$controllerName :$actionName

$controllerName :

$actionName

*
key之后我们在研究下规则

第一种   {$_SERVER.REQUEST_URI|md5}

<span class="variable">$rule</span> = preg_replace_callback(<span class="string">'/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/'</span>, <span class="variable">$callback</span>, <span class="variable">$rule</span>);

根据$callback这个匿名函数可以看出{\$(_\w+)\.(\w+)(?:\|(\w+))?}

$_字符串.字符串|字符串

我设置的规则是

{$_SERVER.REQUEST_URI|md5}

<span class="keyword">return</span> (count(<span class="variable">$match</span>) == 4) ? <span class="variable">$match</span>[3](<span class="variable">$var</span>) : <span class="variable">$var</span>;

最终会返回 md5($_SERVER[REQUEST_URI]) 或$_SERVER[REQUEST_URI]);

查看函数$callback = function($match)可以看出

{$_SERVER.REQUEST_URI|md5}

SERVER可以是GET、POST、REQUEST、SERVER、SESSION、COOKIE

md5可以是任意单参数函数 如 trim等

第二种   {id|md5} 

<span class="variable">$rule</span> = preg_replace_callback(<span class="string">'/{(\w+)\|(\w+)}/'</span>, function(<span class="variable">$match</span>) {
                    <span class="keyword">return</span> <span class="variable">$match</span>[2](<span class="variable">$_GET</span>[<span class="variable">$match</span>[1]]);
                }, <span class="variable">$rule</span>);

默认用了GET规则如下  返回  md5($_GET[id])
第三种  {id}

<span class="variable">$rule</span> = preg_replace_callback(<span class="string">'/{(\w+)}/'</span>, function(<span class="variable">$match</span>) {
                    <span class="keyword">return</span> <span class="variable">$_GET</span>[<span class="variable">$match</span>[1]];
                }, <span class="variable">$rule</span>);

默认用$_GET 返回   $_GET[id]第四种   特殊系统变量  

<span class="variable">$rule</span> = str_ireplace(<span class="keyword">array</span>(<span class="string">'{:controller}'</span>, <span class="string">'{:action}'</span>, <span class="string">'{:module}'</span>), <span class="keyword">array</span>(CONTROLLER_NAME, ACTION_NAME, MODULE_NAME), <span class="variable">$rule</span>);

如 {:controller} 、{:action}、{:module}

第五种

<span class="variable">$rule</span> = preg_replace_callback(<span class="string">'/{|(\w+)}/'</span>, function(<span class="variable">$match</span>) {
                    <span class="keyword">return</span> <span class="variable">$match</span>[1]();
                }, <span class="variable">$rule</span>);

如:|time、|md5 不过这样没有意义 这个地方应该是为特殊函数自定义用的之后系统定义了一个HTML_FILE_NAME并返回缓存时间

define(<span class="string">'HTML_FILE_NAME'</span>, HTML_PATH . <span class="variable">$rule</span> . C(<span class="string">'HTML_FILE_SUFFIX'</span>));
<span class="keyword">return</span> <span class="variable">$cacheTime</span>;

转载请注明:艾依小站 » thinkphp 静态缓存 ReadHtmlCache 的解读不错的文章

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址