Simple configuration and usage of ThinkPHP static cache

Source: Internet
Author: User
This article describes the simple configuration and usage of ThinkPHP static cache, and analyzes in detail the simple configuration method of ThinkPHP static cache based on the instance form. the meanings of common parameters and related usage skills are as follows, for more information about how to configure and use ThinkPHP static cache, see the following example. We will share this with you for your reference. The details are as follows:

According to ThinkPHP official Manual: ThinkPHP has built-in static cache class, and implements configurable static cache by defining static cache rules.

Enable static cache:

ThinkPHP official manual

To use the static cache function, you must enable the HTML_CACHE_ON parameter and add the static cache rule file htmls. php under the project configuration directory. Otherwise, the static cache will not take effect.

Add the following to the configuration file Conf \ config. php's array:

'HTML _ CACHE_ON '=> true, // enable the static cache 'HTML _ path' =>'/index. php/html ', // static cache file directory, HTML_PATH can be set arbitrarily, here is the newly created html directory under the current project

Static rule definition:

ThinkPHP official manual

There are three methods to define static rules:

Return Array ('actionname' => array ('static rule', 'static cache status', 'additional rule'), // The first 'modulename: actionname' => array ('static rule', 'static cache validity period ', 'additional rule'), // type 2' * '=> array ('static rule ', 'static cache validity status', 'additional rule'), // The third type... Static rules for more operations)

The first is to define global static rules for operations. for example, to define static rules for all read operations:

'read'=>array('{id}','60')

{Id} indicates that $ _ GET ['id'] is the static cache file name, and the second parameter indicates that the cache is 60 seconds.

The second is to define the static rules for operations of a module. for example, we need to define the read operations of the Blog module for static cache.

'Blog:read'=>array('{id}',-1)

The third method is to define global static cache rules, which are used in special cases and are applicable to any module operations, such

'*' => Array ('{$ _ SERVER. REQUEST_URI | md5}') // cache data based on the current URL.

Here I write in the static cache rule file htmls. php:

<? Phpreturn array ('gethtml' => array ('{: action}',-1), //-1 indicates permanent cache);?>

The above static cache rules indicate that the static rules for defining all getHtml operations are:

'getHtml'=>array('{:action}',-1)

{: Action} indicates the static cache file name of the current operation.

Similarly, in the \ Lib \ Action \ IndexAction. class. php file, write:

<? Phpclass IndexAction extends Action {// gethtml.shtml public function getHtml () {header ('content-type: text/html; charset = utf-8 ') is generated under the current target html directory '); $ this-> assign ('title', 'generate html file'); $ this-> assign ('info', 'generate html file '); $ this-> display () ;}}?>

In \ Tpl \ default \ index \ getHtml.html, write:

   
   {$title}    {$info} 

Enter http: // 127.0.0.1/myApp/index. php/index/getHtml in the browser to view the expected page.

After the page is refreshed, the address bar of the browser changes as follows:

Ps: If apache, firefox, and opera files are used, they may not support shtml files. in the conf file, find "AddType text/html. shtml "," AddOutputFilter implements Des. shtml, remove the previous.

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.