Skip to content

Commit 9079bba

Browse files
authored
Merge pull request #6 from dwnload/develop
Fixes ballooned response sizes.
2 parents e8e98f6 + 35fd481 commit 9079bba

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

CHANGELONG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 1.2.2 - 2018-04-30
8+
### Fixed
9+
- When endpoints have multiple posts, the request bubbles up and appends the results which leads to a body size X's the
10+
requests. In other words, it's bad. This adds static property cache to break out of the possible loop.
11+
712
## 1.2.1 - 2018-04-30
813
### Updated
914
- Fixes PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback , cannot access protected method Dwnload\WpRestApi\WpAdmin\Admin::renderPage().

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "dwnload/wp-rest-api-object-cache",
33
"description": "Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.",
44
"type": "wordpress-plugin",
5-
"version": "1.2.1",
5+
"version": "1.2.2",
66
"license": "MIT",
77
"authors": [
88
{

src/RestApi/RestDispatch.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ class RestDispatch implements WpHooksInterface
4343
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete';
4444
const QUERY_CACHE_REFRESH = 'rest_cache_refresh';
4545

46-
const VERSION = '1.2.0';
46+
const VERSION = '1.2.2';
47+
48+
/**
49+
* Has the current request been cached? Avoids the multi loop calls where
50+
* multiple objects are in one endpoint.
51+
*
52+
* @var bool[] $cached If the current requested dispatch has been cached.
53+
*/
54+
private static $cached;
4755

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

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

@@ -202,6 +212,7 @@ protected function getCachedResult(
202212
bool $force = false
203213
) {
204214
$result = \wp_cache_get($this->cleanKey($key), $group, $force);
215+
self::$cached[$this->cleanKey($key)] = $result !== false;
205216
if ($result === false) {
206217
$result = $this->dispatchRequest($server, $request);
207218
$defaults = [
@@ -222,7 +233,12 @@ protected function getCachedResult(
222233
($options[Settings::EXPIRATION][Settings::PERIOD] * $options[Settings::EXPIRATION][Settings::LENGTH]),
223234
$options[Settings::EXPIRATION]
224235
);
225-
\wp_cache_set($this->cleanKey($key), $result, $group, \absint($expire));
236+
self::$cached[$this->cleanKey($key)] = \wp_cache_set(
237+
$this->cleanKey($key),
238+
$result,
239+
$group,
240+
\absint($expire)
241+
);
226242

227243
return $result;
228244
}

wp-rest-api-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Description: Enable object caching for WordPress' REST API. Aids in increased response times of your applications endpoints.
55
* Author: Austin Passy
66
* Author URI: http://github.com/thefrosty
7-
* Version: 1.2.1
7+
* Version: 1.2.2
88
* Requires at least: 4.9
99
* Tested up to: 4.9
1010
* Requires PHP: 7.0

0 commit comments

Comments
 (0)