Recommended: "PHP Video Tutorial" "redis Tutorial》
php redis implements a full-page cache system
A function mentioned in a previous project requires pre-stored page information in the background Put it in the database, such as the app's registration agreement, user agreement, etc. Then write it into a php page, and the app accesses this page when calling the interface. At that time, I discovered a problem. These agreements are often modified only once every few months. , and every time the user views these protocols, nginx will re-read the file from the database, and the speed will be very slow.
The following picture m_about.php is the data page I generated,
It takes 2.4s to load it from the database and regenerate the file in the virtual machine environment (of course the actual test environment will be faster).
Since this kind of page data is updated Less, why not cache it? I thought of a full page cache system (full page cache) in the common redis applications I looked at before. Why not write one and give it a try.
Code Ideas
Redis uses the phpredis extension. Of course, you can also use the predis extension. You just need to change a few read functions.
Regarding the interface of the cache system, I refer to the cache in laravel here. System. I think the design interface of this system is very clear. It not only includes redis, but also can use files, mysql, memcache.
Of course, the full page cache does not use so many things. I just borrowed his Function design. The first is the function getUrlText, which is to get the data of the whole page. I didn’t think too much here. I just use file_get_contents directly. Of course, you can also rewrite it into a curl function
/** * 获取对应的url的信息 * @param string $url 对应的地址 * @return boolean|string */ public function getUrlText($url) { if (empty($url)) { return false; } return file_get_contents($url); }
. Secondly, there are several functions that draw lessons from the cache system. , remember function, memory cache, this is the most important interface to the outside world, generally use it directly in the cache system.
/** * 记录对应的缓存,如果之前存在则返回原本的缓存 * @param string $cacheName 缓存名 * @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址 * @param null | int $ttl 缓存过期时间,如果不过期就是用默认值null * @throws \Exception 如果无法访问地址 * @return boolean|string 缓存成功返回获取到的页面地址 */ public function remember($cacheName, $urlOrCallback, $ttl = null) { $value = $this->get($cacheName);//检查缓存是否存在 if (!$value) { //之前没有使用键 if (is_callable($urlOrCallback)) { $text = $urlOrCallback(); } else { //如果不是回调类型,则尝试读取网址 $text = $this->getUrlText($urlOrCallback); } if (empty($text)) { throw new \Exception('can not get value:' . $urlOrCallback); } $this->put($cacheName, $text, $ttl); return $text; } else { return $value; } }
refresh function, refresh cache function, if the cache page has been updated, go Refresh it.
/** * 更新缓存,并返回当前的缓存 * @param string $cacheName 缓存名 * @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址 * @param null | int $ttl 过期时间,如果不过期就是用默认值null * @return boolean|string 缓存成功返回获取到的页面地址 */ public function refresh($cacheName, $urlOrCallback, $ttl = null) { $this->delete($cacheName); return $this->remember($cacheName, $urlOrCallback, $ttl); }
The remaining two code files. One is redisFPC.php, which is the demo of full page cache, and the other is the file for testing
fpcTest.php
Here is the use The one is github, connected to my own git blog. If there is a problem connecting to github, you can see the complete code given at the end of this article.
test
We are here Test, the first load is slower because it needs to read the corresponding m_ahout information
The second load is much faster because it is read from redislimian
Usage suggestions
I think the code has given enough interfaces. Use the remember function to record the cache when caching for the first time. , then if the cache changes, use the refresh function to update the cache. If possible, try to use ttl to set the cache expiration time.
Full code
redisFPC. php
<?php namespace RedisFPC; class RedisFPC { /** * php redis的访问类 * @var unknown */ private $redis; /** * 构造函数 * @param array $redis 使用phpredis的类 * @param 是否连接成功 */ public function __construct($redis = []) { //$this->redis = $redis; $this->redis = new \Redis(); return $this->redis->connect('127.0.0.1'); } /** * 记录对应的缓存,如果之前存在则返回原本的缓存 * @param string $cacheName 缓存名 * @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址 * @param null | int $ttl 缓存过期时间,如果不过期就是用默认值null * @throws \Exception 如果无法访问地址 * @return boolean|string 缓存成功返回获取到的页面地址 */ public function remember($cacheName, $urlOrCallback, $ttl = null) { $value = $this->get($cacheName);//检查缓存是否存在 if (!$value) { //之前没有使用键 if (is_callable($urlOrCallback)) { $text = $urlOrCallback(); } else { //如果不是回调类型,则尝试读取网址 $text = $this->getUrlText($urlOrCallback); } if (empty($text)) { throw new \Exception('can not get value:' . $urlOrCallback); } $this->put($cacheName, $text, $ttl); return $text; } else { return $value; } } /** * 获取对应的缓存值 * @param string $cacheName 缓存名 * @return String | Bool,如果不存在返回false,否则返回对应的缓存页信息 */ public function get($cacheName) { return $this->redis->get($this->getKey($cacheName)); } /** * 将对应的全页缓存保存到对应redis中 * @param string $cacheName 缓存名 * @param string $value * @param null | int $ttl 过期时间,如果不过期就是用默认值null * @return boolean 保存成功返回true */ public function put($cacheName, $value, $ttl = null) { if (is_null($ttl)) { return $this->redis->set($this->getKey($cacheName), $value); } else { return $this->redis->set($this->getKey($cacheName), $value, $ttl); } } /** * 删除对应缓存 * @param string $cacheName 缓存名 */ public function delete($cacheName) { return $this->redis->delete($this->getKey($cacheName)); } /** * 更新缓存,并返回当前的缓存 * @param string $cacheName 缓存名 * @param string | callback $urlOrCallback 需要缓存的数据地址.可以是一个 网页地址也一个可回调类型,如果不是可回调类型,则判定是一个网址 * @param null | int $ttl 过期时间,如果不过期就是用默认值null * @return boolean|string 缓存成功返回获取到的页面地址 */ public function refresh($cacheName, $urlOrCallback, $ttl = null) { $this->delete($cacheName); return $this->remember($cacheName, $urlOrCallback, $ttl); } /** * 获取对应的url的信息 * @param string $url 对应的地址 * @return boolean|string */ public function getUrlText($url) { if (empty($url)) { return false; } return file_get_contents($url); } /** * 生成全页缓存键名 * @param string $cacheName 需要缓存的名称 * @return string 对应的在redis中的键名 */ private function getKey($cacheName) { return 'FPC:'. $cacheName; } }
Test code for testing
Note that the url here is the local cache url
<?php use RedisFPC\RedisFPC; require_once 'redisFPC.php'; /* $text = file_get_contents('http://localhost:1002/m_about.php'); var_dump($text); */ $url = 'http://localhost:1002/m_about.php'; $fpc = new RedisFPC(); echo $fpc->remember('服务协议', $url, 60*60*24);
The above is the detailed content of PHP+redis implements full-page caching system. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
