ThinkPHP静态缓存简单配置和使用方法详解,thinkphp静态
本文实例讲述了ThinkPHP静态缓存简单配置和使用方法。分享给大家供大家参考,具体如下:
根据ThinkPHP官方手册:ThinkPHP内置了静态缓存类,通过静态缓存规则定义来实现了可配置的静态缓存。
启用静态缓存:
ThinkPHP官方手册写道
要使用静态缓存功能,需要开启HTML_CACHE_ON 参数,并且在项目配置目录下面增加静态缓存规则文件 htmls.php,两者缺一不可。否则静态缓存不会生效。
在配置文件Conf\config.php的array()中加上:
'HTML_CACHE_ON' => true,//开启静态缓存 'HTML_PATH' => '__APP__/html',//静态缓存文件目录,HTML_PATH可任意设置,此处设为当前项目下新建的html目录
静态规则定义:
ThinkPHP官方手册写道
静态规则的定义有三种方式:
Return Array( 'ActionName'=>array('静态规则','静态缓存有效期','附加规则'), //第一种 'ModuleName:ActionName'=>array('静态规则','静态缓存有效期','附加规则'),//第二种 '*'=>array('静态规则','静态缓存有效期','附加规则'),//第三种 …更多操作的静态规则 )
第一种是定义全局的操作静态规则,例如定义所有的read操作的静态规则为:
'read'=>array('{id}','60')
其中,{id} 表示取$_GET['id'] 为静态缓存文件名,第二个参数表示缓存60秒。
第二种是定义某个模块的操作的静态规则,例如,我们需要定义Blog模块的read操作进行静态缓存
'Blog:read'=>array('{id}',-1)
第三种方式是定义全局的静态缓存规则,这个属于特殊情况下的使用,任何模块的操作都适用,例如
'*'=>array('{$_SERVER.REQUEST_URI|md5}') //根据当前的URL进行缓存。
我这里在静态缓存规则文件 htmls.php中写:
<?php return array( 'getHtml' => array('{:action}', -1),//-1表示永久缓存 ); ?>
上面的静态缓存规则表示定义所有的getHtml操作的静态规则为:
'getHtml'=>array('{:action}',-1)
{:action}表示当前操作名为静态缓存文件名。
同样在\Lib\Action\IndexAction.class.php文件中写:
<?php class IndexAction extends Action{ //在当前项目的html目录下生成getHtml.shtml public function getHtml() { header('Content-type:text/html;charset=utf-8'); $this->assign('title', '生成html文件'); $this->assign('info', '生成html文件'); $this->display(); } } ?>
在\Tpl\default\index\getHtml.html中写:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>{$title}</title> </head> <body> <h2 id="info">{$info}</h2> </body> </html>
然后在浏览器中输入:http://127.0.0.1/myApp/index.php/index/getHtml,可看到预期的页面。
刷新页面后,浏览器地址栏会发生变化,如下:
ps:如果用的apache,firefox和opera可能会不支持shtml文件,可以在httpd.conf文件中找到"AddType text/html .shtml","AddOutputFilter INCLUDES .shtml",分别去掉前面的"#"即可。
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- thinkphp的静态缓存用法分析
- thinkPHP实现MemCache分布式缓存功能
- 浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
- Thinkphp关闭缓存的方法
- ThinkPHP文件缓存类代码分享
- thinkphp缓存技术详解
- ThinkPHP实现一键清除缓存方法
- 修改ThinkPHP缓存为Memcache的方法
- ThinkPHP缓存方法S()概述
- 采用ThinkPHP中F方法实现快速缓存实例

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.