This paper describes the simple configuration and usage of thinkphp static cache. Share to everyone for your reference, as follows:
According to thinkphp Official Handbook: The thinkphp has built-in static cache classes that enable configurable static caching through static cache rule definitions.
To enable static caching:
Thinkphp Official Handbook wrote
To use the static caching feature, you need to turn on the html_cache_on parameter, and add the static cache rule file htmls.php under the project configuration directory, both of which are indispensable. Otherwise, the static cache does not take effect.
In the configuration file conf\config.php, in the array (), add:
' html_cache_on ' + true,//turn on static cache ' Html_path ' = ' __app__/html ',//static cache file directory, Html_path can be set arbitrarily, this is set as the current project under the new HTML directory
Static rule definitions:
Thinkphp Official Handbook wrote
There are three ways to define a static rule:
Return Array (' ActionName ' =>array (' Static rules ', ' static cache expiration ', ' additional rules '),///First ' modulename:actionname ' =>array (' Static rules ', ' Static cache validity period ', ' additional rules '),//second ' * ' =>array (' Static rules ', ' static cache expiration ', ' additional rules '),//Third ... More static rules for operations)
The first is to define a global operation static rule, such as a static rule that defines all of the read operations:
' Read ' =>array (' {id} ', ' 60 ')
where {ID} means take $_get[' id '] as a static cache file name, the second parameter represents cache for 60 seconds.
The second is a static rule that defines the operation of a module, for example, we need to define the read operation of the blog module for static caching
' Blog:read ' =>array (' {ID} ',-1)
The third way is to define the global static cache rules, which are used in special cases, the operation of any module is applicable, for example
' * ' =>array (' {$_server. REQUEST_URI|MD5} ')//cache according to the current URL.
Here I write in the static cache rule file htmls.php:
<?phpreturn Array (' gethtml ' = = Array (' {: action} ',-1),//-1 means permanent cache);? >
The above static cache rule represents a static rule that defines all the gethtml operations:
' Gethtml ' =>array (' {: action} ',-1)
{: Action} indicates that the current operation name is a static cache file name.
Also written in the \lib\action\indexaction.class.php file:
<?phpclass Indexaction extends action{ //Generate gethtml.shtml public function gethtml () in the HTML directory of the current project { Header (' Content-type:text/html;charset=utf-8 '); $this->assign (' title ', ' Generate HTML file '); $this->assign (' info ', ' Generate HTML file '); $this->display (); }}? >
Write in \tpl\default\index\gethtml.html:
{$title} {$info}
Then enter: http://127.0.0.1/myApp/index.php/index/getHtml in the browser to see the expected page.
When the page is refreshed, the browser address bar changes, as follows:
PS: If you use Apache,firefox and opera may not support the sHTML file, you can find "AddType text/html. shtml" in the httpd.conf file. "Addoutputfilter includes". sHTML ", remove the front" # "respectively.
More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.