本文实例讲述了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程序设计有所帮助。

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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.
