thinkphp static cache simple configuration and how to use _php instance

Source: Internet
Author: User
Tags php template smarty template
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.