{"id":19854598,"url":"https://github.com/dwnload/wp-rest-api-object-cache","last_synced_at":"2025-05-02T01:30:33.485Z","repository":{"id":56973624,"uuid":"119722386","full_name":"dwnload/wp-rest-api-object-cache","owner":"dwnload","description":"Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.","archived":false,"fork":false,"pushed_at":"2019-04-17T22:49:38.000Z","size":76,"stargazers_count":23,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T20:35:12.069Z","etag":null,"topics":["memcached","object-cache","packagist","php7","wordpress","wordpress-plugin","wp-api","wp-json"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dwnload.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELONG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-31T17:53:00.000Z","updated_at":"2024-06-07T11:22:57.000Z","dependencies_parsed_at":"2022-08-21T07:40:29.187Z","dependency_job_id":null,"html_url":"https://github.com/dwnload/wp-rest-api-object-cache","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwnload%2Fwp-rest-api-object-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwnload%2Fwp-rest-api-object-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwnload%2Fwp-rest-api-object-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwnload%2Fwp-rest-api-object-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dwnload","download_url":"https://codeload.github.com/dwnload/wp-rest-api-object-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251969245,"owners_count":21673182,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memcached","object-cache","packagist","php7","wordpress","wordpress-plugin","wp-api","wp-json"],"created_at":"2024-11-12T14:09:54.934Z","updated_at":"2025-05-02T01:30:33.150Z","avatar_url":"https://github.com/dwnload.png","language":"PHP","readme":"# WordPress REST API Object Cache\n\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/dwnload/wp-rest-api-object-cache.svg)]()\n[![Latest Stable Version](https://img.shields.io/packagist/v/dwnload/wp-rest-api-object-cache.svg)](https://packagist.org/packages/dwnload/wp-rest-api-object-cache)\n[![Total Downloads](https://img.shields.io/packagist/dt/dwnload/wp-rest-api-object-cache.svg)](https://packagist.org/packages/dwnload/wp-rest-api-object-cache)\n[![License](https://img.shields.io/packagist/l/dwnload/wp-rest-api-object-cache.svg)](https://packagist.org/packages/dwnload/wp-rest-api-object-cache)\n[![Build Status](https://travis-ci.org/dwnload/wp-rest-api-object-cache.svg?branch=master)](https://travis-ci.org/dwnload/wp-rest-api-object-cache)\n\nEnable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.\n\n## Package Installation (via Composer)\n\nTo install this package, edit your `composer.json` file:\n\n```json\n{\n    \"require\": {\n        \"dwnload/wp-rest-api-object-cache\": \"^1.3.0\"\n    }\n}\n```\n\nNow run:\n\n`$ composer install dwnload/wp-rest-api-object-cache`\n\n-----\n\n- [Actions](#actions)\n- [How to use actions](#how-to-use-actions)\n- [Filters](#filters)\n- [How to use filters](#how-to-use-filters)\n\n\nActions\n====\n| Action    | Argument(s) |\n|-----------|-----------|\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::ACTION_CACHE_SKIPPED | mixed **$result**\u003cbr\u003eWP_REST_Server **$server**\u003cbr\u003eWP_REST_Request **$request** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::ACTION_REQUEST_FLUSH_CACHE | string **$message**\u003cbr\u003estring **$type**\u003cbr\u003eWP_User **$user** |\n\nHow to use actions\n----\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_action( RestDispatch::ACTION_CACHE_SKIPPED, function( $result, \\WP_REST_Server $server, \\WP_REST_Request $request ) {\n\t// Do something here, like create a log entry using Wonolog.\n}, 10, 3 );\n```\n\n```php\nuse Dwnload\\WpRestApi\\WpAdmin\\Admin;\nadd_action( Admin::ACTION_REQUEST_FLUSH_CACHE, function( $message, $type, WP_User $user ) {\n\t// Do something here, like create a log entry using Wonolog.\n}, 10, 3 );\n```\n\nFilters\n====\n| Filter    | Argument(s) |\n|-----------|-----------|\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_CACHE_HEADERS | array **$headers**\u003cbr\u003estring **$request_uri**\u003cbr\u003eWP_REST_Server **$server**\u003cbr\u003eWP_REST_Request **$request**\u003cbr\u003eWP_REST_Response **$response (`rest_pre_dispatch` only)** |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_CACHE_SKIP | boolean **$skip** ( default: WP_DEBUG )\u003cbr\u003estring **$request_uri**\u003cbr\u003eWP_REST_Server **$server**\u003cbr\u003eWP_REST_Request **$request** |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_API_KEY | string **$request_uri**\u003cbr\u003eWP_REST_Server **$server**\u003cbr\u003eWP_REST_Request **$request** |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_API_GROUP | string **$cache_group** |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_CACHE_EXPIRE | int **$expires** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::FILTER_CACHE_UPDATE_OPTIONS | array **$options** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::FILTER_CACHE_OPTIONS | array **$options** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::FILTER_SHOW_ADMIN | boolean **$show** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::FILTER_SHOW_ADMIN_MENU | boolean **$show** |\n| Dwnload\\WpRestApi\\WpAdmin\\Admin::FILTER_SHOW_ADMIN_BAR_MENU | boolean **$show** |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_ALLOWED_CACHE_STATUS | array **$status** HTTP Header statuses (defaults to `array( 200 )` |\n| Dwnload\\WpRestApi\\RestApi\\RestDispatch::FILTER_CACHE_VALIDATE_AUTH | boolean **$authenticated**\u003cbr\u003eWP_REST_Request $request |\n\nHow to use filters\n----\n**Sending headers.**\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_filter( RestDispatch::FILTER_CACHE_HEADERS, function( array $headers ) : array {\n\t$headers['Cache-Control'] = 'public, max-age=3600';\n\t\n\treturn $headers;\n} );\n```\n\n**Changing the cache expire time.**\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_filter( RestDispatch::FILTER_CACHE_EXPIRE, function() : int {\n\t// https://codex.wordpress.org/Transients_API#Using_Time_Constants\n\treturn ( HOUR_IN_SECONDS * 5 );\n} );\n```\n\n```php\nuse Dwnload\\WpRestApi\\WpAdmin\\Admin;\nadd_filter( Admin::FILTER_CACHE_OPTIONS, function( array $options ) : array {\n\tif ( ! isset( $options['timeout'] ) ) {\n\t\t$options['timeout'] = array();\n\t}\n\n\t// https://codex.wordpress.org/Transients_API#Using_Time_Constants\n\t$options['timeout']['length'] = 15;\n\t$options['timeout']['period'] = DAY_IN_SECONDS;\n\t\n\treturn $options;\n} );\n```\n\n**Validating user auth when `?context=edit`**\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_filter( RestDispatch::FILTER_CACHE_VALIDATE_AUTH, function( bool $auth, WP_REST_Request $request ) : bool {\n\t// If you are running the Basic Auth plugin.\n\tif ( $GLOBALS['wp_json_basic_auth_error'] === true ) {\n        $authorized = true;\n    }\n    // Otherwise, maybe do some additional logic on the request for current user...\n\n    return $authorized;\n}, 10, 2 );\n```\n\n**Skipping cache**\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_filter( RestDispatch::FILTER_CACHE_SKIP, function( bool $skip, string $request_uri ) : bool {\n\tif ( ! $skip \u0026\u0026 stripos( 'wp-json/dwnload/v2', $request_uri ) !== false ) {\n\t\treturn true;\n\t}\n\n\treturn $skip;\n}, 10, 2 );\n```\n\n**Deleting cache**\n\n*Soft delete:*\nAppend `RestDispatch::QUERY_CACHE_DELETE` to your query param: `add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1' ], '\u003curl\u003e' )`.  \n_soft delete will delete the cache after the current request completes (on WordPress shutdown)._ \n\n*Hard delete:* Append `RestDispatch::QUERY_CACHE_DELETE` \u0026\u0026 `RestDispatch::QUERY_CACHE_FORCE_DELETE` to your query param:\n`add_query_arg( [ RestDispatch::QUERY_CACHE_DELETE, '1', RestDispatch::QUERY_CACHE_FORCE_DELETE, '1' ], '\u003curl\u003e' )`.  \n_hard delete will delete the cache before the request, forcing it to repopulate._\n\n\n**empty ALL cache on post-save** _this is not ideal_\n\nYou can use the WordPress filter `save_post` if you would like to empty **ALL** cache on post save.\n\n```php\nuse Dwnload\\WpRestApi\\RestApi\\RestDispatch;\nadd_action( 'save_post', function( $post_id ) {\n  if ( class_exists( RestDispatch::class ) ) {\n    call_user_func( [ ( WpRestApiCache::getRestDispatch(), 'wpCacheFlush' ] );\n  }\n} );\n```\n\n**Maybe better to use `transition_post_status`**\n\n```php\nadd_action( 'transition_post_status', function(  string $new_status, string $old_status, \\WP_Post $post ) {\n  if ( 'publish' === $new_status || 'publish' === $old_status ) {\n    \\wp_cache_flush();\n  }\n}, 99, 3 );\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwnload%2Fwp-rest-api-object-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdwnload%2Fwp-rest-api-object-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwnload%2Fwp-rest-api-object-cache/lists"}