深入理解PHP高级技巧、面向对象与核心技术之调整浏览器缓存
缓存页面是保存页面的内容的一种方式,然后在产生请求时提供这个保存的页面版本,而不是服务器上的版本。 对于动态的、PHP驱动的站点,有时我们需要确保终端用户获得的是最新的版本。 利用PHP的header()函数可以调整缓存,包括web浏览器的缓存和代理服务器的
缓存页面是保存页面的内容的一种方式,然后在产生请求时提供这个保存的页面版本,而不是服务器上的版本。
对于动态的、PHP驱动的站点,有时我们需要确保终端用户获得的是最新的版本。
利用PHP的header()函数可以调整缓存,包括web浏览器的缓存和代理服务器的缓存。
四种标头的类型:
a). Last-Modified(最后的修改时间)
b). Expires(过期时间)
c). Pragma(编译提示)
d).Cache-Control(缓存控制)
Last-Modified使用UTC时间值,使用来自服务器的更新页面的版本。
Expires标头用来表明缓存的版本过期,设置为一个以前的日期,就会强制使用服务器上的页面。
Header (“Expires: Jul 2013”);
Pragma表明页面数据应该如何被处理,可以避免对页面进行缓存。
Header(“Pragma:no-cache”);
Cache-Control能够更细致的控制
指令 |
含义 |
Public |
可以在任何地方缓存 |
Private |
只能呗浏览器缓存 |
No-cache |
不能在任何地方缓存 |
Must-revalidate |
缓存必须检查更新版本 |
Proxy-revalidate |
代理缓存必须检查更新版本 |
Max-age |
内容能被缓存的时间以秒为单位 |
s-maxage |
覆盖共享缓存的max-age值设置 |
为了避免任何系统对页面进行缓存,可以这样设置标头:
Header(“Last-Modified:Thu,5Jun 2012 14:26:00 GMT”);//Right Now
Header (“Expires:Mon,26 Jul1997 05:00:00 GMT”);//way back when
Header(“Pragma:no-cache”);
Header(“Cache-Control:no-cache”);
虽然这种方法很常见,但是很笨拙,并不是每个PHP脚本都不能缓存,设置时活跃的站点也会把一些脚本缓存一分钟或者更长时间,(很活跃的站点会频繁的收到大量的请求而缓存版本可以减少服务器的负载)
//connect to the database
$dbc = mysqli_connect();
//get latest dates as timestamps
$q = 'SELECTUNIX_TIMESTAMP(MAX(date_added)),UNIX_TIMESTAMP(MAX(date_completed)) FROMtasks';
$r = mysqli_query($dbc,$q);
list($max_a,$max_c) = mysqli_fetch_array($r,MYSQL_NUM);
$max = ($max_a > $max_c)?$max_a:$max_c;
$interval = 60 * 60 * 6;
header("Last-Modified:".gmdate("r",$max));
header("Expires:".gmdate("r",($max+$interval)));
header("Cache-Control:max-age = $interval");
?>
利用UNIX_TIMESTAMP()函数把返回值转换为整数。利用三元运算符把最新的时间赋值给$max.
1. 定义一个合理的缓存空间
$interval = 60*60*60;
2. 设置脚本最后修改时间为数据库的更新的最后时间。
header("Last-Modified:".gmdate("r",$max));
3. 设置有效期限是指当前时间加上前面定义的时间间隔。
header("Expires:".gmdate("r",($max+$interval)));
4. 取代使用一个日期值,使用秒数来设置变量max-age的值。
注意:
从理论上讲,缓存是一个不错的设计,他可以把不必要的服务器请求降低到最少,在适当控制的情况下,缓存对于服务器端和客户端都是有益的。放置在HTmL代码部分的Metaheader()方法更可靠。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Avoid N 1 query problems, reduce the number of database queries by loading associated data in advance; 2. Select only the required fields to avoid loading complete entities to save memory and bandwidth; 3. Use cache strategies reasonably, such as Doctrine's secondary cache or Redis cache high-frequency query results; 4. Optimize the entity life cycle and call clear() regularly to free up memory to prevent memory overflow; 5. Ensure that the database index exists and analyze the generated SQL statements to avoid inefficient queries; 6. Disable automatic change tracking in scenarios where changes are not required, and use arrays or lightweight modes to improve performance. Correct use of ORM requires combining SQL monitoring, caching, batch processing and appropriate optimization to ensure application performance while maintaining development efficiency.

To build a flexible PHP microservice, you need to use RabbitMQ to achieve asynchronous communication, 1. Decouple the service through message queues to avoid cascade failures; 2. Configure persistent queues, persistent messages, release confirmation and manual ACK to ensure reliability; 3. Use exponential backoff retry, TTL and dead letter queue security processing failures; 4. Use tools such as supervisord to protect consumer processes and enable heartbeat mechanisms to ensure service health; and ultimately realize the ability of the system to continuously operate in failures.

Use subprocess.run() to safely execute shell commands and capture output. It is recommended to pass parameters in lists to avoid injection risks; 2. When shell characteristics are required, you can set shell=True, but beware of command injection; 3. Use subprocess.Popen to realize real-time output processing; 4. Set check=True to throw exceptions when the command fails; 5. You can directly call chains to obtain output in a simple scenario; you should give priority to subprocess.run() in daily life to avoid using os.system() or deprecated modules. The above methods override the core usage of executing shell commands in Python.

Using the correct PHP basic image and configuring a secure, performance-optimized Docker environment is the key to achieving production ready. 1. Select php:8.3-fpm-alpine as the basic image to reduce the attack surface and improve performance; 2. Disable dangerous functions through custom php.ini, turn off error display, and enable Opcache and JIT to enhance security and performance; 3. Use Nginx as the reverse proxy to restrict access to sensitive files and correctly forward PHP requests to PHP-FPM; 4. Use multi-stage optimization images to remove development dependencies, and set up non-root users to run containers; 5. Optional Supervisord to manage multiple processes such as cron; 6. Verify that no sensitive information leakage before deployment

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

Bref enables PHP developers to build scalable, cost-effective applications without managing servers. 1.Bref brings PHP to AWSLambda by providing an optimized PHP runtime layer, supports PHP8.3 and other versions, and seamlessly integrates with frameworks such as Laravel and Symfony; 2. The deployment steps include: installing Bref using Composer, configuring serverless.yml to define functions and events, such as HTTP endpoints and Artisan commands; 3. Execute serverlessdeploy command to complete the deployment, automatically configure APIGateway and generate access URLs; 4. For Lambda restrictions, Bref provides solutions.
