zfcampus / zf-http-cache
Requires
- php: ^5.6 || ^7.0
- zendframework/zend-eventmanager: ^2.6.3 || ^3.0.1
- zendframework/zend-http: ^2.5.4
- zendframework/zend-mvc: ^2.7.15 || ^3.0.2
Requires (Dev)
- container-interop/container-interop: ^1.1
- phpunit/phpunit: ^5.7.27 || ^6.5.8 || ^7.1.5
- zendframework/zend-coding-standard: ~1.0.0
README
仓库已废弃 2019-12-31
简介
zf-http-cache
是一个ZF2模块,用于在Zend Framework 2应用程序中自动化http缓存任务。
安装
运行以下composer
命令
$ composer require "zfcampus/zf-http-cache:^1.0"
或者,手动将以下内容添加到您的composer.json
文件中的require
部分
"require": { "zfcampus/zf-http-cache": "^1.0" }
然后运行composer update
以确保模块已安装。
最后,将模块名称添加到您的项目的config/application.config.php
文件中的modules
键下
return [ /* ... */ 'modules' => [ /* ... */ 'ZF\HttpCache', ], /* ... */ ];
配置
用户配置
一般来说,应尽量避免使用匿名函数,因为它会阻止您缓存配置。
此模块用户配置的最高级配置键是zf-http-cache
。
config/module.config.php
文件包含了一个配置的示例。
键:controllers
controllers
键用于将以下任一项映射到缓存头配置:
- 路由名称
- 连接的
controller::action
- 控制器
- 正则表达式
- 通配符
每个都是大小写敏感的,并将一个或多个HTTP方法映射到给定的规则特定的缓存头配置。
示例
// See `config/module.config.php` for a complete commented example 'zf-http-cache' => [ /* ... */ 'controllers' => [ '<controller>' => [ '<http-method>' => [ '<cache-header-name>' => [ 'override' => true, 'value' => '<cache-header-value>', ], ], ], ], /* ... */ ],
键:<controller>
可以是
$controller::$action
的连接- 控制器名称(由
Zend\Mvc\MvcEvent::getRouteMatch()->getParam('controller')
返回;值是大小写敏感的) - 正则表达式(参见
<regex_delimiter>
键) - 通配符
通配符匹配任何未指定的控制器。
键:<http-method>
可以是小写的HTTP方法(如get
、post
等)(由Zend\Http\Request::getMethod()
返回)或通配符。
通配符代表所有未指定的HTTP方法。
键:<cache-header-name>
HTTP缓存头名称(如Cache-control
、expires
、etag
等)。
ETags
对于ETags,您可以在配置中指定自定义生成器
'etag' => [
'override' => true,
'generator' => 'Your\Own\ETagGenerator',
],
生成器示例可以在\ZF\HttpCache\DefaultETagGenerator
中找到。
键:<cache-header-value>
缓存头部的值。
键:override
是否覆盖您应用程序可能发送的缓存头部。
键:enable
enable
键用于在运行时启用/禁用http缓存模块。
如果您不再需要此模块,请考虑从application.config.php
列表中删除该模块。
注意:禁用时,http缓存模块不会覆盖/删除应用程序发送的缓存头部。
示例
'zf-http-cache' => [ /* ... */ 'enable' => true, // Cache module is enabled. /* ... */ ],
键:http_codes_black_list
http_codes_black_list
用于避免缓存具有所列HTTP状态代码的响应。默认为除200
之外的所有状态。
示例
'zf-http-cache' => [ /* ... */ 'http_codes_black_list' => ['201', '304', '400', '500'], // Whatever the other configurations, the responses with these HTTP codes won't be cached. /* ... */ ],
键:regex_delimiter
此键用于启用将键作为正则表达式评估。
它必须包含正则表达式的分隔符。
如果您不想在配置中使用正则表达式,将其设置为null以避免无效解析。
正则表达式按它们在配置中出现的顺序进行测试,首先是匹配项。
正则表达式优于通配符。
注意:当此值不为空且没有对应的当前控制器时,将使用preg_match。
示例
'zf-http-cache' => [ /* ... */ 'regex_delimiter' => '~', /* ... */ 'controllers' => [ '~.*~' => [ // Acts as a wildcard. /* ... */ ], ], /* ... */ ],
系统配置
以下配置在config/module.config.php
中提供
'service_manager' => [ 'factories' => [ 'ZF\HttpCache\HttpCacheListener' => 'ZF\HttpCache\HttpCacheListenerFactory', ], ],
ZF2事件
监听器
ZF\HttpCache\HttpCacheListener
此监听器附加到MvcEvent::EVENT_ROUTE
和MvcEvent::EVENT_FINISH
事件,优先级为-10000
。