infrajs/nostore

该软件包最新版本(v1.1.15)没有可用的许可证信息。

处理Cache-Control: no-store和no-cache头部

v1.1.15 2021-09-10 07:47 UTC

This package is auto-updated.

Last update: 2024-09-14 14:45:13 UTC


README

Latest Stable Version Total Downloads

浏览器缓存管理

通过composer安装

{
	"require":{
		"infrajs/nostore":"~1"
	}
}

使用

Nostore::init();

将根据配置php Nostore::$conf设置Cache-Control头部

public static $conf=array(
		"max-age" => 86400, //24 часа, время кэша, когда public:true, для динамики
		"max-age-stat" => 2419200, //4 недели, время кэша когда public:true и вызван Nostore::pubStat() для статики
		
		//План обновлений сайта программистом. В Expires будет подставляться следующая непрошедшая дата
		"expires-year" => [ //dd.mm
			'05.03','10.03',
			'25.01','01.01','18.01',
			'18.02','25.02'],
		"expires-month" => [],//Дата месяца 1,20
		"expires-str" => [], //'next monday'
		"public" => true, //expires работает только с этим ключём
		"port" => array( //указанные файлы можно загрузить по адресу vendor/infrajs/nostore/?port=watch
			"watch" => "https://#/metrika/watch.js",
			"twitter" => "http://platform.twitter.com/widgets.js"
		)
	);

测试

安装后,在浏览器中打开vendor/infrajs/nostore/tester.php

描述

在配置中指定网站计划的更新日期。例如,在3月8日之前更改静态横幅,替换文件,因此指定日期为3月5日。因此,如果3月1日访问网站的访客,缓存将不是4周,而是只有4天,直到3月5日。

{
	"expires-year": ["05.03","10.03","25.01","01.01","18.01","18.02","25.02"],
	"expires-month": [1],
	"expires-str": ["next friday"]
}

expires-str遵循strtotime函数的规则。指定next friday表示缓存总是设置为下一个星期五,而不是默认的2周。

默认情况下,Php文件在调用Nostore::init()时被视为动态的,以便将缓存设置为静态,需要单独调用Nostore::pubStat()。例如,imager/index.php脚本更改图片的行为与静态类似,发送相应的头部。

网站管理员所做的所有更改通常都涉及动态数据更改,这些更改在24小时内生效。

如果网站使用直接指向文件的链接,则将替换一个文件为另一个文件的替换操作视为更改静态,如果网站使用直接链接到文件,则更改静态不会在四周内或根据配置中的expires...的更接近的日期生效。

浏览器缓存管理

3种模式

  • no-store。内容可能在不可预测的时刻更改。内容随着用户的更改而更改。来自数据库的新数据。会话处理。内容取决于isAdmin() isDebug() isTest()。这意味着此类内容不能缓存,也不能为不同的用户使用缓存。
  • no-cache。内容可能由管理员更改,管理员需要立即看到结果。数据缓存,但始终检查。内容可能在可预测的时刻更改。根据文件更改日期缓存。
  • public + Expires - 浏览器允许缓存。默认情况下,此模式已启用 Nostore::$conf['public'] = true;
Nostore::on(); //no-store
Nostore::off(); //no-cache
Nostore::pub(); //public
Nostore::is(); //true - если заголовок Cache-Controll no-store и кэширование полностью запрещено
Nostore::isPub(); //true - если заголовок Cache-Controll public

Cache-Control

默认情况下,Apache Web服务器响应通常不包含控制缓存的头部。

Nostore::init(); //Выставляются заголовки по умолчанию согласно конфига
  • 'public':true - 公共缓存启用。
  • 'public':false - 网站更新过于频繁,访客经常返回网站以获取新内容,浏览器将始终向服务器发送请求以获取新内容。

对于public true,需要将类似条目添加到.htaccess中,以激活静态和绕过php的文件的相同级别的缓存。

#2419200 4 недели
<FilesMatch "\.(woff|jpeg|jpg|png|gif|ico|js|tpl|json|html)$">
	Header set Cache-Control "max-age=2419200, public"
</FilesMatch>

对于由php生成的静态内容,需要添加调用Nostore::pubStat()。从脚本的角度来看,设置为no-cache或public无关紧要。这两种选项都允许缓存。但是no-cache缓存由If-Modified头部管理,而public请求根本不会到达,好像If-Modified已经知道它将是false。而no-store确实禁止缓存,内容将再次收集,而无需任何条件。Nostore::check和Nostore::is允许no-store头部出现,并禁止缓存依赖于no-store内容的人,该内容可能进入public内容并相应地将public更改为no-store。

Nostore::is()

检查是否有发送的头部列表中的'Cache-control'头部具有传递的值'no-store'。如果有,则返回true,否则返回false。

header('Cache-Control: public');
$res = Nostore::is();
assert(false === $res);
header('Cache-Control: no-store');
$res = Nostore::is();
assert(true === $res);

Nostore::pub()

如果不存在 'Cache-control' 标头并设置为 'no-store',则该方法将设置 'Cache-control' 标头为 Expire - max-age,该值在配置中定义。

Nostore::pub();

Nostore::pubStat()

如果不存在 'Cache-control' 标头并设置为 'no-store',则该方法将设置 'Cache-control' 标头为 Expire - max-age-stat,该值在配置中定义。

Nostore::pub();

Nostore::init()

如果 config.public = true,则方法将设置 Cache-control: public 和 Expire: conf.max-age。如果属性 public 等于 false,则方法将设置 Cache-control: no-cache。

Nostore::init();

Nostore::$conf['public'] = true;
Nostore::init();
//Response Headers
//Cache-Control: max-age=18000, public
Nostore::$conf['public'] = false;
Nostore::init();
//Response Headers
//Cache-Control: no-cache

Nostore::isPub()

方法将在标头列表中查找 'Cache-control' 标头并设置为 'public'。如果找到这样的值,则方法返回 false,否则返回 true。

header('Cache-Control: max-age=18000, public');
Nostore::isPub(); //true
header('Cache-Control: no-cache');
Nostore::isPub(); //false

Nostore::on()

方法将 'Cache-control' 标头的值设置为 'no-store'。

header('Cache-Control: max-age=18000, public');
Nostore::on()
//Response Headers
//Cache-Control: no-store

Nostore::off()

no-cache 或 public 仅由通用配置选择。不建议单独调用 off()。如果 conf.public=true,则调用 Nostore::pub(); 如果 conf.public=false,则设置 'Cache-control' 标头的值为 'no-cache'。

Nostore::off();

Nostore::check($callback)

方法检查在执行函数 $callback 时是否被 'no-store' 标头禁止缓存。no-cache 和 public 标头允许缓存,如同没有 'Cache-control' 标头一样。

header('Cache-control: no-cache');
$is = Nostore::check( function (){
	header('Cache-Control: no-cache');
}); //false
header('Cache-control: no-cache');
$is = Nostore::check( function (){
	header('Cache-Control: no-store');
}); //true
header('Cache-control: no-store');
$is = Nostore::check( function (){
	header('Cache-Control: no-cache');
}); //false
header('Cache-control: no-store');
$is = Nostore::check( function (){
	header('Cache-Control: no-store');
}); //true

从互联网缓存文件 config.port

对于远程服务器未设置缓存标头且无法访问该服务器的情况。在配置中,config.port 注册了可以按地址 /vendor/infrajs/nostore/?port=watch 下载的文件。如果已配置 infrajs/router -nostore/?port=watch,则将按新地址设置缓存标头,如同静态文件一样。

{
	"port":{
		"watch": "https://#/metrika/watch.js",
		"twitter": "http://platform.twitter.com/widgets.js"
	}
}

参数 ?-nostore=true

如果脚本在 infrajs 中使用 Nostore::init() 和 router,则此调用始终发生。存在参数 -nostore=true 时,响应将包含禁止缓存的标头。这样,任何地址都可以通过地址无 public 缓存打开。这是忽略本地缓存的简单方法,只有请求会到达服务器,并按常规逻辑使用服务器缓存。

参数 ?-nostore=true 确保请求到达服务器。

如果浏览器不使用缓存,则所有请求都会到达服务器。适用于在完全优化后的生产环境中运行网站。这是关键的,尽管网站已经完全缓存,但仍允许立即看到更改。与 infrajs/env 一起使用。