Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.

License

NotificationsYou must be signed in to change notification settings

dwnload/wp-rest-api-object-cache

Repository files navigation

PHP from PackagistLatest Stable VersionTotal DownloadsLicenseBuild Status

Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.

Package Installation (via Composer)

To install this package, edit yourcomposer.json file:

{"require":{"dwnload/wp-rest-api-object-cache":"^1.0"}}

Now run:

$ composer install dwnload/wp-rest-api-object-cache


Actions

ActionArgument(s)
Dwnload\WpRestApi\RestApi\RestDispatch::ACTION_CACHE_SKIPPEDmixed$result
WP_REST_Server$server
WP_REST_Request$request
Dwnload\WpRestApi\WpAdmin\Admin::ACTION_REQUEST_FLUSH_CACHEstring$message
string$type
WP_User$user

How to use actions

useDwnload\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 );
useDwnload\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 );

Filters

FilterArgument(s)
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_CACHE_HEADERSarray$headers
string$request_uri
WP_REST_Server$server
WP_REST_Request$request
WP_REST_Response$response (rest_pre_dispatch only)
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_CACHE_SKIPboolean$skip ( default: WP_DEBUG )
string$request_uri
WP_REST_Server$server
WP_REST_Request$request
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_API_KEYstring$request_uri
WP_REST_Server$server
WP_REST_Request$request
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_API_GROUPstring$cache_group
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_CACHE_EXPIREint$expires
Dwnload\WpRestApi\WpAdmin\Admin::FILTER_CACHE_UPDATE_OPTIONSarray$options
Dwnload\WpRestApi\WpAdmin\Admin::FILTER_CACHE_OPTIONSarray$options
Dwnload\WpRestApi\WpAdmin\Admin::FILTER_SHOW_ADMINboolean$show
Dwnload\WpRestApi\WpAdmin\Admin::FILTER_SHOW_ADMIN_MENUboolean$show
Dwnload\WpRestApi\WpAdmin\Admin::FILTER_SHOW_ADMIN_BAR_MENUboolean$show
Dwnload\WpRestApi\RestApi\RestDispatch::FILTER_ALLOWED_CACHE_STATUSarray$status HTTP Header statuses (defaults toarray( 200 )

How to use filters

Sending headers.

useDwnload\WpRestApi\RestApi\RestDispatch;add_filter( RestDispatch::FILTER_CACHE_HEADERS,function(array$headers ) :array {$headers['Cache-Control'] ='public, max-age=3600';return$headers;} );

Changing the cache expire time.

useDwnload\WpRestApi\RestApi\RestDispatch;add_filter( RestDispatch::FILTER_CACHE_EXPIRE,function() :int {// https://codex.wordpress.org/Transients_API#Using_Time_Constantsreturn (HOUR_IN_SECONDS *5 );} );
useDwnload\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;} );

Skipping cache

useDwnload\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 ) {returntrue;}return$skip;},10,2 );

Deleting cache

Soft delete:AppendRestDispatch::QUERY_CACHE_DELETE to your query param:add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1' ], '<url>' ).
soft delete will delete the cache after the current request completes (on WordPress shutdown).

Hard delete: AppendRestDispatch::QUERY_CACHE_DELETE &&RestDispatch::QUERY_CACHE_FORCE_DELETE to your query param:add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1', RestDispatch::QUERY_CACHE_FORCE_DELETE, '1' ], '<url>' ).
hard delete will delete the cache before the request, forcing it to repopulate.

empty ALL cache on post-savethis is not ideal

You can use the WordPress filtersave_post if you would like to emptyALL cache on post save.

useDwnload\WpRestApi\RestApi\RestDispatch;add_action('save_post',function($post_id ) {if (class_exists( RestDispatch::class ) ) {call_user_func( [ (newWpRestApiCache() )->getRestDispatch(),'wpCacheFlush' ] );  }} );

About

Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp