Table of Contents
{$info}
Home Backend Development PHP Tutorial ThinkPHP静态缓存简单配置和使用方法详解_php实例

ThinkPHP静态缓存简单配置和使用方法详解_php实例

Jun 07, 2016 pm 05:08 PM
thinkphp use Configuration

本文实例讲述了ThinkPHP静态缓存简单配置和使用方法。分享给大家供大家参考,具体如下:

根据ThinkPHP官方手册:ThinkPHP内置了静态缓存类,通过静态缓存规则定义来实现了可配置的静态缓存。

启用静态缓存:

ThinkPHP官方手册写道

要使用静态缓存功能,需要开启HTML_CACHE_ON 参数,并且在项目配置目录下面增加静态缓存规则文件 htmls.php,两者缺一不可。否则静态缓存不会生效。

在配置文件Conf\config.php的array()中加上:

'HTML_CACHE_ON' => true,//开启静态缓存
'HTML_PATH' => '__APP__/html',//静态缓存文件目录,HTML_PATH可任意设置,此处设为当前项目下新建的html目录

Copy after login

静态规则定义:

ThinkPHP官方手册写道

静态规则的定义有三种方式:

Return Array(
'ActionName'=>array('静态规则','静态缓存有效期','附加规则'), //第一种
'ModuleName:ActionName'=>array('静态规则','静态缓存有效期','附加规则'),//第二种
'*'=>array('静态规则','静态缓存有效期','附加规则'),//第三种
…更多操作的静态规则
)

Copy after login

第一种是定义全局的操作静态规则,例如定义所有的read操作的静态规则为:

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

Copy after login

其中,{id} 表示取$_GET['id'] 为静态缓存文件名,第二个参数表示缓存60秒。

第二种是定义某个模块的操作的静态规则,例如,我们需要定义Blog模块的read操作进行静态缓存

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

第三种方式是定义全局的静态缓存规则,这个属于特殊情况下的使用,任何模块的操作都适用,例如

'*'=>array('{$_SERVER.REQUEST_URI|md5}')
//根据当前的URL进行缓存。

Copy after login

我这里在静态缓存规则文件 htmls.php中写:

<&#63;php
return array(
'getHtml' => array('{:action}', -1),//-1表示永久缓存
);
&#63;>

Copy after login

上面的静态缓存规则表示定义所有的getHtml操作的静态规则为:

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

Copy after login

{:action}表示当前操作名为静态缓存文件名。

同样在\Lib\Action\IndexAction.class.php文件中写:

<&#63;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();
  }
}
&#63;>

Copy after login

在\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>

Copy after login

然后在浏览器中输入: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程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

See all articles