Table of Contents
ThinkPHP静态缓存简单配置和使用方法详解,thinkphp静态
{$info}
您可能感兴趣的文章:
Home php教程 php手册 ThinkPHP静态缓存简单配置和使用方法详解,thinkphp静态

ThinkPHP静态缓存简单配置和使用方法详解,thinkphp静态

Jun 13, 2016 am 08:43 AM
thinkphp

ThinkPHP静态缓存简单配置和使用方法详解,thinkphp静态

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

您可能感兴趣的文章:

  • thinkphp的静态缓存用法分析
  • thinkPHP实现MemCache分布式缓存功能
  • 浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
  • Thinkphp关闭缓存的方法
  • ThinkPHP文件缓存类代码分享
  • thinkphp缓存技术详解
  • ThinkPHP实现一键清除缓存方法
  • 修改ThinkPHP缓存为Memcache的方法
  • ThinkPHP缓存方法S()概述
  • 采用ThinkPHP中F方法实现快速缓存实例
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

Video Face Swap

Video Face Swap

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

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)

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.

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.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles