edwincromleydev / simple-wp-http-cache
WordPress的简单HTTP缓存和日志
Requires
- php: >=7.0
- composer/installers: ^1.5.0
Requires (Dev)
- 10up/phpcs-composer: dev-master
- phpunit/phpunit: ^6.5.8
This package is not auto-updated.
Last update: 2024-09-25 09:27:57 UTC
README
WordPress的简单HTTP缓存
需求
PHP 7.0+ WordPress 4.9.8+
安装
基本安装
这是一个WordPress插件,可以按照以下说明进行安装:手动插件安装。
通过Composer
Composer是一个PHP依赖管理器。
在使用它之前,您需要先安装Composer。
要将此插件作为依赖项安装,请在WordPress安装根目录中运行以下命令,其中存在composer.json文件
composer require edwincromleydev/simple-wp-http-cache:dev-master
确保您的composer.json文件使用安装器路径将依赖项移动到您的插件文件夹中。以下是默认WordPress安装所需的配置
"extra": {
"installer-paths": {
"wp-content/plugins/{$name}": ["type:wordpress-plugin"]
}
}
这将使用与插件名称匹配的插件名称在wp-content/plugins中安装插件。
此插件在WPPackagist上不可用。
用法
基础
Simple WP HTTP Cache基于WordPress的内部WP_HTTP API。
任何使用WordPress的WP_HTTP的请求,都可以传递一个额外的参数来控制Simple HTTP缓存和日志的功能。以下是如何缓存网络请求的示例
$response = wp_safe_remote_get( 'https://edwincromley.com/wp-json/wp/v2/posts?per_page=1', [ 'simple_wp_http_cache' => [ 'active' => true, ], ] );
这将默认存储响应值5分钟,防止在该时间段内重复调用再次通过网络访问。
可以向simple_wp_http_cache添加多个参数。
'active'
将缓存设置为激活,这意味着它将缓存任何未缓存的或绕过的请求。这将只缓存成功的请求,这些请求达到网络。默认情况下,内部WP失败不会缓存。active
参数只负责是否设置新的缓存,而不是是否获取。如果active设置为除true之外的任何值,或者根本未设置,并且存在相同请求的先前缓存结果,则仍将返回缓存值。
[ 'simple_wp_http_cache' => [ 'active' => true, ], ]
'expiration'
缓存的过期时间,以秒为单位。默认为300;5分钟。
要无限期存储值,请传递0,如下所示
[ 'simple_wp_http_cache' => [ 'active' => true, 'expiration' => 0, ], ]
可以通过'simple_wp_http_cache_expiration'
过滤器更改过期时间。
apply_filters( 'simple_wp_http_cache_expiration', $request['simple_wp_http_cache']['expiration'] ?? 300, $response, $request, $url );
'bypass'
将绕过设置为true,您可以通过绕过缓存命中。在下面的示例中,每次都会设置新的缓存值。缓存查找被绕过,请求通过,并且由于激活标志,新的响应值被缓存。这实际上仅用于调试,不建议用于其他任何原因
[ 'simple_wp_http_cache' => [ 'active' => true, 'bypass' => true, ], ]
'log_errors'
log_errors
用于确保任何内部WP失败或网络请求失败都传递给记录器。默认情况下,如果您同时启用了WP_DEBUG和WP_DEBUG_LOG,则消息将输出到WordPress的错误日志。
可以根据请求或响应实现基于请求的或其他替代日志解决方案。
[ 'simple_wp_http_cache' => [ 'log_errors' => true, ], ]
响应是否应该记录为错误可以通过simple_wp_http_cache_is_error
过滤器处理。该过滤器期望一个布尔值。
apply_filters( 'simple_wp_http_cache_is_error', $is_error, $response, $request, $url );
'log_request_times'
log_request_times
用于记录所有到达网络的请求,并显示响应时间(以毫秒为单位)。对于缓存的请求,默认情况下在缓存命中时不会记录,只有在缓存未命中时才会记录。
[ 'simple_wp_http_cache' => [ 'log_request_times' => true, ], ]
缓存
Simple WP HTTP Cache 使用 WordPress 内置的缓存 API。如果 WP_CACHE
设置为 true
,则将使用为 WordPress 配置的 对象缓存。如果 WP_CACHE
未启用,则将使用 Transients API 作为后备。
请求通过请求参数和 URL 进行缓存。任何请求参数或 URL 的变化都会导致缓存未命中。缓存键使用 sha1 算法生成哈希值,任何碰撞的可能性极低。
您可以直接将缓存参数传递给任何 HTTP 函数调用,例如 wp_safe_remote_get
,或者利用 WordPress 的内部钩子为 Simple WP HTTP Cache 设置参数。
以下示例将缓存 WordPress 发出的每个请求 10 分钟
add_filter( 'http_request_args', function( $request ) { $request['simple_wp_http_cache'] = [ 'active' => true, 'expiration' => 600, ]; return $request; }, 1, 1 );
以上做法不推荐,但可以感受到此插件的功能灵活性。
日志记录
Simple WP HTTP Cache 还具有可插拔的 Logger 类。您可以使用自己的匹配类覆盖基本 Logger。当前 Logger 基于 PSR-3 接口,并且可能在未来进行更改。
如果您有一个自定义 Logger 类,请确保它具有以下签名的 log
方法
public function log( $level, $message, array $context = array() ) { // Log my stuff here. }
一个基本的自定义日志类可能看起来像这样
/** * My custom logger. */ class MyLogger { /** * Constructs log object. * * @return void */ public function __construct() { return; } /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return void */ public function log( $level, $message, array $context = array() ) { // Log your message to wherever it should go. } }
然后您可以使用您的自定义类如下
add_filter( 'simple_wp_http_cache_log_class', function( $class ) { $class = '\MyLogger'; return $class; } );
现在,每当任何响应被记录时,它们将通过您的自定义类。您可以使用 'simple_wp_http_cache_log_class'
钩子来实现您的日志策略。与其为单个类编写复杂的 log
方法,建议您创建多个具有适合特定用例的 log
方法的类。
总结
Simple WP HTTP Cache 是对 WordPress 现有 HTTP API 的基本增强,使您能够快速缓存特定的请求或记录错误和响应时间。这些功能实际上非常灵活和可插拔,因为它们利用了 WordPress 中的各种钩子。