Skip to content

Commit

Permalink
Merge pull request #6 from dwnload/develop
Browse files Browse the repository at this point in the history
Fixes ballooned response sizes.
  • Loading branch information
thefrosty authored May 14, 2018
2 parents e8e98f6 + 35fd481 commit 9079bba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELONG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.2.2 - 2018-04-30
### Fixed
- When endpoints have multiple posts, the request bubbles up and appends the results which leads to a body size X's the
requests. In other words, it's bad. This adds static property cache to break out of the possible loop.

## 1.2.1 - 2018-04-30
### Updated
- Fixes PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback , cannot access protected method Dwnload\WpRestApi\WpAdmin\Admin::renderPage().
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dwnload/wp-rest-api-object-cache",
"description": "Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.",
"type": "wordpress-plugin",
"version": "1.2.1",
"version": "1.2.2",
"license": "MIT",
"authors": [
{
Expand Down
24 changes: 20 additions & 4 deletions src/RestApi/RestDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ class RestDispatch implements WpHooksInterface
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';

const VERSION = '1.2.0';
const VERSION = '1.2.2';

/**
* Has the current request been cached? Avoids the multi loop calls where
* multiple objects are in one endpoint.
*
* @var bool[] $cached If the current requested dispatch has been cached.
*/
private static $cached;

/**
* Add class hooks.
Expand All @@ -70,8 +78,10 @@ protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request
$group = $this->getCacheGroup();
$key = $this->getCacheKey($request_uri, $server, $request);

// Don't cache non-readable (GET) methods.
if ($request->get_method() !== WP_REST_Server::READABLE) {
// Return the result if it's a non-readable (GET) method or it's been cached.
if ($request->get_method() !== WP_REST_Server::READABLE ||
(! empty(self::$cached[$this->cleanKey($key)]) && self::$cached[$this->cleanKey($key)] === true)
) {
return $result;
}

Expand Down Expand Up @@ -202,6 +212,7 @@ protected function getCachedResult(
bool $force = false
) {
$result = \wp_cache_get($this->cleanKey($key), $group, $force);
self::$cached[$this->cleanKey($key)] = $result !== false;
if ($result === false) {
$result = $this->dispatchRequest($server, $request);
$defaults = [
Expand All @@ -222,7 +233,12 @@ protected function getCachedResult(
($options[Settings::EXPIRATION][Settings::PERIOD] * $options[Settings::EXPIRATION][Settings::LENGTH]),
$options[Settings::EXPIRATION]
);
\wp_cache_set($this->cleanKey($key), $result, $group, \absint($expire));
self::$cached[$this->cleanKey($key)] = \wp_cache_set(
$this->cleanKey($key),
$result,
$group,
\absint($expire)
);

return $result;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-rest-api-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.
* Author: Austin Passy
* Author URI: http://github.com/thefrosty
* Version: 1.2.1
* Version: 1.2.2
* Requires at least: 4.9
* Tested up to: 4.9
* Requires PHP: 7.0
Expand Down

0 comments on commit 9079bba

Please sign in to comment.