dwnload / wp-rest-api-object-cache
启用WordPress REST API的对象缓存。有助于提高应用程序端点的响应时间。
    1.4.0
    2019-04-17 22:49 UTC
Requires
- php: >=7.0.4
- composer/installers: ~1.0
- thefrosty/wp-utilities: ^1.2.2
Requires (Dev)
- phpunit/phpunit: 6.*
- squizlabs/php_codesniffer: ^3.2
README
启用WordPress REST API的对象缓存。有助于提高应用程序端点的响应时间。
包安装(通过Composer)
要安装此包,请编辑您的composer.json文件
{
    "require": {
        "dwnload/wp-rest-api-object-cache": "^1.3.0"
    }
}
现在运行
$ composer install dwnload/wp-rest-api-object-cache
操作
如何使用操作
use Dwnload\WpRestApi\RestApi\RestDispatch; add_action( RestDispatch::ACTION_CACHE_SKIPPED, function( $result, \WP_REST_Server $server, \WP_REST_Request $request ) { // Do something here, like create a log entry using Wonolog. }, 10, 3 );
use Dwnload\WpRestApi\WpAdmin\Admin; add_action( Admin::ACTION_REQUEST_FLUSH_CACHE, function( $message, $type, WP_User $user ) { // Do something here, like create a log entry using Wonolog. }, 10, 3 );
过滤器
如何使用过滤器
发送头信息。
use Dwnload\WpRestApi\RestApi\RestDispatch; add_filter( RestDispatch::FILTER_CACHE_HEADERS, function( array $headers ) : array { $headers['Cache-Control'] = 'public, max-age=3600'; return $headers; } );
更改缓存过期时间。
use Dwnload\WpRestApi\RestApi\RestDispatch; add_filter( RestDispatch::FILTER_CACHE_EXPIRE, function() : int { // https://codex.wordpress.org/Transients_API#Using_Time_Constants return ( HOUR_IN_SECONDS * 5 ); } );
use Dwnload\WpRestApi\WpAdmin\Admin; add_filter( Admin::FILTER_CACHE_OPTIONS, function( array $options ) : array { if ( ! isset( $options['timeout'] ) ) { $options['timeout'] = array(); } // https://codex.wordpress.org/Transients_API#Using_Time_Constants $options['timeout']['length'] = 15; $options['timeout']['period'] = DAY_IN_SECONDS; return $options; } );
当?context=edit时验证用户认证。
use Dwnload\WpRestApi\RestApi\RestDispatch; add_filter( RestDispatch::FILTER_CACHE_VALIDATE_AUTH, function( bool $auth, WP_REST_Request $request ) : bool { // If you are running the Basic Auth plugin. if ( $GLOBALS['wp_json_basic_auth_error'] === true ) { $authorized = true; } // Otherwise, maybe do some additional logic on the request for current user... return $authorized; }, 10, 2 );
跳过缓存
use Dwnload\WpRestApi\RestApi\RestDispatch; add_filter( RestDispatch::FILTER_CACHE_SKIP, function( bool $skip, string $request_uri ) : bool { if ( ! $skip && stripos( 'wp-json/dwnload/v2', $request_uri ) !== false ) { return true; } return $skip; }, 10, 2 );
删除缓存
软删除:将RestDispatch::QUERY_CACHE_DELETE添加到您的查询参数:add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1' ], '<url>' )。
软删除将在当前请求完成后(在WordPress关闭时)删除缓存。
硬删除:将RestDispatch::QUERY_CACHE_DELETE和RestDispatch::QUERY_CACHE_FORCE_DELETE添加到您的查询参数:add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1', RestDispatch::QUERY_CACHE_FORCE_DELETE, '1' ], '<url>' )。
硬删除将在请求之前删除缓存,强制重新填充。
在保存帖子时清空所有缓存 这不是理想的做法
如果您希望在保存帖子时清空所有缓存,可以使用WordPress过滤器save_post。
use Dwnload\WpRestApi\RestApi\RestDispatch; add_action( 'save_post', function( $post_id ) { if ( class_exists( RestDispatch::class ) ) { call_user_func( [ ( WpRestApiCache::getRestDispatch(), 'wpCacheFlush' ] ); } } );
也许使用transition_post_status会更好
add_action( 'transition_post_status', function( string $new_status, string $old_status, \WP_Post $post ) { if ( 'publish' === $new_status || 'publish' === $old_status ) { \wp_cache_flush(); } }, 99, 3 );