search
HomeBackend DevelopmentPHP ProblemHow to set cache time in php

How to set cache time in php

Nov 27, 2020 am 09:05 AM
php

php method to set cache time: first create a PHP sample file; then pass "if(is_file('./index.html') && (time()-filemtime('./index.html') ))

How to set cache time in php

Recommendation: "PHP Video Tutorial"

Operating environment for this tutorial:

  • Windows7 system, PHP5.6 version

  • Suitable for all brands of computers

php handles static pages: page settings Cache time

1. Page added cache time

2. Manual triggering method

3.crontab scheduled scanner

Let’s implement it Option 1: Add cache time to the page

用户请求页面 => 页面是否过期 =>
=> 否(获取静态页面) || =>是(动态页面生成一份新的静态页面)
if( 如果存在这个静态文件 && 没有过期){
    // 获取页面
}else{
    // 重新生成一份静态页面
}

ok, the basic logic is like this, let’s improve the code below:

<?php
if(is_file(&#39;./index.html&#39;) && (time()-filemtime(&#39;./index.html&#39;)) < 60){ 
    // 假设缓存时间是60秒
    // 获取页面
    require_once(&#39;./index.html&#39;);
}else{
    // 重新生成一份静态页面
    // 准备要展示到网页的数据
    $data = array( 
        array(&#39;id&#39;=>1,&#39;msg&#39;=>&#39;hello java&#39;),
        array(&#39;id&#39;=>2,&#39;msg&#39;=>&#39;hello php&#39;),
        array(&#39;id&#39;=>3,&#39;msg&#39;=>&#39;hello python&#39;),
    );
    // 渲染到模板
    // 实际项目一般是在html里渲染
    // 这里演示 希望能看懂
    ob_start(); // 开始输入缓冲控制
    foreach($data as $item){
        echo $item[&#39;id&#39;].&#39;===>&#39;.$item[&#39;msg&#39;].&#39;<br/>&#39;;
    }
    // 开始生成静态页面文件
    file_put_contents(&#39;index.html&#39;,ob_get_contents());
}

In this way we access index.php, if the static file cache has not expired, its actual access The content comes from the static file index.html.

The above is the detailed content of How to set cache time in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function